gpt4 book ai didi

Java - 了解私有(private)最终自定义类型的返回类型

转载 作者:行者123 更新时间:2023-11-29 05:07:56 40 4
gpt4 key购买 nike

我试图理解下面的代码,其中返回类型被视为不兼容。

 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;

}

新的错误信息如下:

在: return new PassLine(one, two, roll);

错误:

     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

最佳答案

这个错误再清楚不过了,

public TheDie PassLine() {

TheDie One = dieOne();
TheDie Two = dieTwo();
TheDie roll = rollTheDie();

return new PassLine(One, Two, roll);
}

您指定该方法应返回一个 TheDie 对象,同时返回一个 PassLine 对象。

根据你的错误信息

incompatible types: PassLine cannot be converted to TheDie

两种类型不兼容,将您的方法更改为

public PassLine PassLine() {

TheDie One = dieOne();
TheDie Two = dieTwo();
TheDie roll = rollTheDie();

return new PassLine(One, Two, roll);
}

也许您真的想通过调用一个在新的 PassLine 对象上真正返回 TheDie 的方法来返回一个 TheDie

关于Java - 了解私有(private)最终自定义类型的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29736721/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com