我想了解下面的代碼,其中返回類型被視爲不兼容。Java - 瞭解私有最終自定義類型的返回類型
return new PassLine(One, Two, roll);
incompatible types: PassLine cannot be converted to TheDie
希望有人可以擺脫一些光線,以便我可以繼續。
我期待得到dieOne + dieTwo的總卷。
public class PassLine {
private int thePoint;
private final TheDie dieOne;
private final TheDie dieTwo;
private final TheDie rollTheDie;
public PassLine(TheDie dieOne, TheDie dieTwo, TheDie rollTheDie) {
super();
this.dieOne = dieOne;
this.dieTwo = dieTwo;
this.rollTheDie = rollTheDie;
}
public TheDie dieOne() {
TheDie obj = new TheDie();
obj.rollTheDie();
return dieOne;
}
public TheDie dieTwo() {
TheDie obj = new TheDie();
obj.rollTheDie();
return dieTwo;
}
public TheDie rollTheDie() {
dieOne();
dieTwo();
return rollTheDie;
}
public PassLine passLine() {
TheDie One = dieOne();
TheDie Two = dieTwo();
TheDie roll = rollTheDie();
return new PassLine(One, Two, roll);
}
public static void main(String[] args) {
System.exit(0);
}
}
從 - 公共最終類TheDie
public int rollTheDie() {
Random rand = new Random();
oneDie = rand.nextInt(6) + 1;
return oneDie;
}
新的錯誤消息如下:
上: 返回新軋製線(一,二,輥);
錯誤:
Incompatible Type: TheDiecannot be converted to String
Incompatible Type: TheDiecannot be converted to String
Introduce...
May split declaration into a declaration and assignment
Flip operands of the binary operator
不錯,感謝您的解釋。 現在返回正確的類型它說 - 不兼容的類型:TheDie不能轉換爲字符串 - 修改錯誤類型 – SFmtl
@S_HereToLearn請張貼您的新的return語句。 –
@S_HereToLearn你的新方法是正確的。也許有一種方法,當你返回一個'PassLine'時,你沒有添加返回字符串的地方? –