此构造函数引用在 Java 中不起作用

this constructor citation not working in java

提问人:Andres Ordonez 提问时间:8/6/2023 更新时间:8/7/2023 访问量:83

问:

我尝试应用 this() 方法在 java 中调用相同的构造函数,以尝试使构造函数 wehere 只是提供名称和类,它将选择字符的参数,但它不起作用,我是在 java 中使用它的新手。

type public class Jugador{
    //campos
    private String nombre;
    private String clase;
    private int vida;
    private int ad;
    private int ap;
    private int def;
    private int mr;

    //constructores
    private Jugador(String nombre, String clase, int vida, int ad, int ap, int def, int mr){
        this.nombre = nombre;
        if (clase.equals("hunter") || clase.equals("warrior") || clase.equals("assasin") || clase.equals("tank")){
            this.clase = clase;
        }
        else{
            this.clase = "normal";
        }
        this.vida = vida;
        this.ad = ad;
        this.ap = ap;
        this.def = def;
        this.mr = mr;
    }

    public Jugador(String nombre, String clase){
        if (clase.equals("hunter")){
            this(nombre, clase, 120, 50, 0, 40, 0);
        }
        else if (clase.equals("warrior")){
            this(nombre, clase, 100, 70, 0, 80, 0);
        }
        else if (clase.equals("assasin")){
            this(nombre, clase, 60, 120, 0, 20, 0);
        }
        else if (clase.equals("tank")){
            this(nombre, clase, 200, 20, 0, 80, 0);
        }
        else if(clase.equals("normal")){
            this(nombre, clase, 80, 80, 0, 80, 0);
        }
    }


    }here
java 对象 这个

评论

2赞 davidalayachew 8/6/2023
请发布错误消息。这将是必要的,以便我们确保我们看到的是相同的错误。
2赞 markspace 8/6/2023
使用 时,它必须是第一个语句,所以不能使用语句然后 。this()ifthis()

答:

2赞 Thiyagu 8/6/2023 #1

不能在 if 语句中使用 (或 )。对 (or ) 的调用必须是构造函数中的第一个语句。this()super()this()super()

请参阅为什么 this() 和 super() 必须是构造函数中的第一个语句?1

有几种方法可以解决此问题。您可以决定不为该类设置公共构造函数,而是通过公共静态方法启用创建该类的实例。

public static class Jugador {
    //campos
    private String nombre;
    private String clase;
    private int vida;
    private int ad;
    private int ap;
    private int def;
    private int mr;

    private Jugador(String nombre, String clase, int vida, int ad, int ap, int def, int mr) {
        this.nombre = nombre;
        if (clase.equals("hunter") || clase.equals("warrior") || clase.equals("assasin") || clase.equals("tank")) {
            this.clase = clase;
        } else {
            this.clase = "normal";
        }
        this.vida = vida;
        this.ad = ad;
        this.ap = ap;
        this.def = def;
        this.mr = mr;
    }

    public static Jugador buildJugador(String nombre, String clase) {
        if (clase.equals("hunter")) {
            return new Jugador(nombre, clase, 120, 50, 0, 40, 0);
        } else if (clase.equals("warrior")) {
            return new Jugador(nombre, clase, 100, 70, 0, 80, 0);
        } else if (clase.equals("assasin")) {
            return new Jugador(nombre, clase, 60, 120, 0, 20, 0);
        } else if (clase.equals("tank")) {
            return new Jugador(nombre, clase, 200, 20, 0, 80, 0);
        } else if (clase.equals("normal")) {
            return new Jugador(nombre, clase, 80, 80, 0, 80, 0);
        } else {
            throw new RuntimeException("Invalid value passed for clase");
        }
    }
}

然后,调用者不能直接调用构造函数(因为它是私有的),而是将方法用作:buildJugador

Jugador jugador = Jugador.buildJugador("some-value", "hunter");

1 JEP 447 可能会改变这一点:super() 之前的语句。但截至目前,它还没有计划成为任何未来 Java 版本的一部分。

评论

0赞 Stephen C 8/6/2023
JEP 447 很有趣,但根据问题跟踪器,预览版还没有目标版本。IMO,预览版极不可能在 Java 22 之前发布。
0赞 Thiyagu 8/6/2023
@StephenC是的。它甚至可能根本无法成功(由于某种原因被丢弃)。这就是为什么我说它在未来可能会发生变化,这取决于 JEP 的进展情况
0赞 Stephen C 8/6/2023
同意。只是为那些过于渴望和/或不了解 Java 中创新如何发生的读者填写虚线。
0赞 Thiyagu 8/6/2023
@StephenC 我已经改写了它,以便更清楚
0赞 Thiyagu 8/6/2023
@user16320675 JEP 标题中包含“预览”一词。我们可以说JEP正在审查中。无论如何,我已经删除了预览一词以避免歧义。
0赞 Reilas 8/7/2023 #2

"...一个构造函数我们只是提供名称和类,它将选择字符的参数......”

因此,与其使用私有构造函数,

private Jugador(String nombre, String clase, int vida, int ad, int ap, int def, int mr)

使用私有方法添加值、vidaad 等。

private void values(int vida, int ad, int ap, int def, int mr) {
    this.vida = vida;
    this.ad = ad;
    this.ap = ap;
    this.def = def;
    this.mr = mr;
}

然后,从公共构造函数中,只需相应地应用即可。

我在这里合并了来自私有构造函数的代码。

public Jugador(String nombre, String clase){
    this.nombre = nombre;
    this.clase = switch (clase) {
        case "hunter", "warrior", "assasin", "tank" -> clase;
        default -> "normal";
    };
    switch (clase) {
        case "hunter" -> values(120, 50, 0, 40, 0);
        case "warrior" -> values(100, 70, 0, 80, 0);
        case "assasin" -> values(60, 120, 0, 20, 0);
        case "tank" -> values(200, 20, 0, 80, 0);
        case "normal" -> values(80, 80, 0, 80, 0);
    }
}