Java 为什么会出现“构造函数未定义”的错误?

Java 为什么会出现“构造函数未定义”的错误?,java,constructor,class-design,Java,Constructor,Class Design,在Java中,获取此错误: Error: The constructor MyComplex(MyComplex) is undefined Java代码: public class MyComplex { int realPart, imaginaryPart; public MyComplex(){ } public MyComplex(int realPart, int imaginaryPart) { this.realPart = re

在Java中,获取此错误:

Error: The constructor MyComplex(MyComplex) is undefined
Java代码:

public class MyComplex {
    int realPart, imaginaryPart;
    public MyComplex(){
    }
    public MyComplex(int realPart, int imaginaryPart) {
        this.realPart = realPart;
        this.imaginaryPart = imaginaryPart;
    }
    public void setRealPart(int realPart) {
        this.realPart = realPart;
    }
    public String toString() {
        return realPart + " + " + imaginaryPart +"i";
   }
}
public class MyComplexTester {
    public static void main(String[] args) {
        MyComplex a = new MyComplex(20, 50);
        MyComplex b = new MyComplex(a);        //Error happens here
        b.setRealPart(4);
        System.out.println(b);
    }
}
如果我使用

MyComplex b = a;

但是我不能更改main方法中的代码,因为这是设计类以运行给定方法的家庭作业。

您应该创建相应的复制构造函数。 因此:


您必须有一个接受MyComplex类型的对象的重载构造函数才能使其工作

下面是你的最新课程

public class MyComplex {
    int realPart, imaginaryPart;
    public MyComplex(){
    }
    public MyComplex(int realPart, int imaginaryPart) {
        this.realPart = realPart;
        this.imaginaryPart = imaginaryPart;
    }

   public MyComplex(MyComplex mycomplex) {//this is the constructor you need
        this.realPart = mycomplex.realPart;
        this.imaginaryPart = mycomplex.imaginaryPart;
    }

    public void setRealPart(int realPart) {
        this.realPart = realPart;
    }
    public String toString() {
        return realPart + " + " + imaginaryPart +"i";
   }
}
因为在下线

MyComplex b=新的MyComplex

您正在传递一个MyComplex类型的构造函数,但在MyComplex类中,您使用一个类型为int的参数定义了构造函数。请更正传递的参数。

说明 您没有接受另一个MyComplex副本构造函数的构造函数。您只创建了接受以下内容的构造函数:

没有争论,新的MyComplex 两个int参数,新的MyComplex5,2 解决方案 您需要显式定义要使用的构造函数。Java不会为您生成这样的构造函数。例如:

public MyComplex(MyComplex other) {
    realPart = other.realPart;
    imaginaryPart = other.imaginaryPart;
}
那么它也会起作用

笔记 为了提高代码的可读性,应该对新的副本构造函数,尤其是默认构造函数使用显式构造函数转发

例如,现在您的默认构造函数new MyComplex将导致复杂值0+0i。但这很容易被监督,因为您的代码并没有明确指出这一点

通过转发,目的更加明确:

public MyComplex() {
    this(0, 0);
}

public MyComplex(MyComplex other) {
    this(other.realPart, other.imaginaryPart);
}
然后两者都将转发到接受两个int值的显式构造函数


请注意,Java自动为您生成的唯一构造函数是微不足道的默认构造函数。这是公共MyComplex{}没有参数-什么都不做。而且只有在您自己没有编写任何构造函数的情况下。

因为没有以MyComplex作为参数声明的构造函数。您需要声明以下构造函数:-

 public MyComplex(MyComplex mycomplex) {
    this.realPart = mycomplex.realPart;
    this.imaginaryPart = mycomplex.imaginaryPart;
}

创建一个匹配的构造函数;怎么办?您希望它运行哪个构造函数?您没有一个单参数构造函数。您缺少一个像public MyComplexMyComplex c{…}这样的构造函数。您需要定义自己在Java中的每个构造函数。没有默认的复制构造函数。请注意,这在代码中已过时。变量的名称可以改进,以更好地反映其工作。惯用语是另一种。这解释了问题,但并不试图提供解决方案。请看,谢谢。@Zabuza我没有提供解决方案,因为我不知道他的用例,但是我提到了纠正通过的paramer。
 public MyComplex(MyComplex mycomplex) {
    this.realPart = mycomplex.realPart;
    this.imaginaryPart = mycomplex.imaginaryPart;
}