public class Demo {
public static void main(String args[]) {
Base b = new Base();
b.Base(100, 200);
System.out.println(b.Base());
}
}
default class Base {
private int x = 10;
private int y = 20;
Base (int x, int y) {
this.x = x;
this.y = y;
}
int Base() {
return x + y;
}
}
class Base {
private int x = 10;
private int y = 20;
public Base(int x, int y) {
this.x = x;
this.y = y;
}
int base() {
return x + y;
}
}
class Demo {
public static void main(String[] args) {
Base b = new Base(100, 200);
System.out.println(b.base());
}
}
参考GPT和自己的思路:
这段程序存在问题。在第5行,试图调用Base类的构造函数,但是它不应该像方法一样被调用,而应该使用new关键字来实例化对象。正确的代码应该是:
Base b = new Base(100, 200);