我目前正在上java课,今天我的老师说了一些我脑子里没有接受的东西,可能是错的。下面的代码,java这个关键字里面的构造函数
class Ab{
int x;
int b;
public Ab(int x, int b){
this.x = x;
this.b = b;
}
public static void main(String[] args){
Ab x = new Ab(4,5); // this is where my teacher confused me
}
}
我的老师说x Ab = new AB(4,5);
会得到x对象调用构造函数构造函数是指它与this keyword
过去了,什么是匿名类,如new Ab(2,4);
现在会有什么this keyword
是指?在我看来,this
里面的构造函数没有链接引用变量,直到构造函数创建对象并将引用返回给变量。
'this'指的是当前对象。 – Li357
你的类定义了字段'int x',但是在你使用'this.a'的构造函数中。或者你的意思是int a和this.a或者int x和this.x。哪一个是对的? – walen
为什么你的代码格式化? – markspace