Java递归构造函数调用

标签 java

<分区>

public class Person {
    String name;
    int weight;
    int height;

    public Person(String name, int weight, int height){
        this.name=name;
        this.weight=weight;
        this.height=height;
    }

    public Person(String name, int weight){
        this(name, weight);
    }
}

错误:(12, 12) java: 递归构造函数调用

我应该更改什么才能正确编译它?

使用 IntelliJ 2017.1

最佳答案

 public Person(String name, int weight){
        this(name, weight);
    }

是的。那是递归的。调用相同的构造函数。

可能你想给对方打电话

 public Person(String name, int weight){
        this(name, weight,0); // default height 0 
    }

this(name, weight,0); 使用 3 个参数调用另一个构造函数,并将高度作为 0 传递,因为没有可用的高度。或者您可以传递任何默认高度。

关于Java递归构造函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48662916/

相关文章:

Java Spring MVC - 发送 JSON 请求正文错误

java - Chrome Java 中的 Selenium dhtmlxtree NoSuchElementException

java - Maven:远程运行单元测试

java - java.util.LinkedList 中的 addFirst()

java - Android上的Whatsapp数据库解密

java - 如何为 WiFi Direct 连接设置特定的引脚?

java - 如何配置 log4j 以仅记录信息消息和更高级别的消息?

java - 将列表放入 map 中

java - 如何将字符串类型的值的编码与java中的特定编码进行比较?

java - JPARepository findByUserName 方法返回 null 值