英文:
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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论