错误:在类complex中,构造函数complex不能适用于给定的类型;

huangapple go评论70阅读模式
英文:

error: constructor complex in class complex cannot be applied to given types;

问题

class complex {
    int a, b;

    void complex(int x, int y) {
        a = x;
        b = y;
    }

    void complex() {
        System.out.println(a + "+ i" + b);
    }
}
...

class Main {
    public static void main(String[] args) {
        complex n1 = new complex(10, 20);
        complex n2 = new complex(30, 40);
        n1.complex();
        n2.complex();
    }
}

上述代码引发以下构建错误

> 在类 complex 中的构造函数不能应用于给定的类型
英文:
class complex {
    int a,b;

    void complex(int x,int y)
    {
        a=x;
        b=y;
    }
    void complex()
    {
        System.out.println(a+"+ i"+b);
    }
}

...

class Main {
 public static void main (String[] args) 
 {
    complex n1 = new complex(10,20);
    complex n2 = new complex(30,40);
    n1.complex();
    n2.complex();
 }
}

The above code raises the following build error:

> constructor complex in class complex cannot be applied to given types;

答案1

得分: 2

构造函数不应该 'return' 一个类型。更新访问级别。在方法中使用与类名不同的名称。

例如:

class complex
{
  int a, b;

  public complex(int x, int y)
  {
    a = x;
    b = y;
  }

  public void display()
  {
    System.out.println(a + "+ i" + b);
  }
}

...

class Main {

  public static void main(String[] args) {
    complex n1 = new complex(10, 20);
    complex n2 = new complex(30, 40);
    n1.display();
    n2.display();
  }
}
英文:

The constructor does should not 'return' a type. Update the access level. Use a different name than the class name for methods.

For example:

class complex
{
  int a,b;

  public complex(int x,int y)
  {
    a=x;
    b=y;
  }

  public void display()
  {
    System.out.println(a+"+ i"+b);
  }
}

...

class Main {

  public static void main (String[] args) {
    complex n1 = new complex(10, 20);
    complex n2 = new complex(30, 40);
    n1.display();
    n2.display();
  }
}

huangapple
  • 本文由 发表于 2020年3月15日 16:32:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/60691030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定