//1.普通的直接引用 class test ...{ privateint x,y; public test(int x,int y) ...{ setX(x);//也可以写为this.setX(x);这种情况下this可以省略. } } //2.方法中的某个形参名与当前对象的某个成员有相同的名字.为了不混淆,使用this区分有this引用的是成员,没有this是形参 class test ...{ privateint x,y; public test(int x,int y) ...{ setX(x);//也可以写为this.setX(x);这种情况下this可以省略. } setX(int x)...{ this.x = x;//this.x是引用的对象,x是setX(int x)中的行参x } } //3.引用构造函数 class test...{ publicint x,y; public test(int x,int y)...{ this.x = x; this.y = y; } public test()...{ this.(2,0)//构造函数调用其他构造函数的方法,这个例子调用上面那个复用构造函数的方法 } }
super用法
我一度认为只能作用在构造函数中,这种想法是错误的.
//1.super运用在构造函数中 class test1 extend test...{ public test1()...{ super();//调用父类的构造函数 } public test1(int x)...{ super(x);//调用父类的构造函数,因为带形参所以super 也要带行参 } } //2.调用父类中的成员 class test1 extend test...{ public test1()...{} public f()...{//假如父类里有一个成员叫g(); super.g();//super引用当前对象的直接父类中的成员 } }