我试了一下,用jbuilder输出的结果是 this.nm()=more sref.nm()=more super.nm()=that super 提供你访问父类的方法,并不是实例,另外super()代表父类的构造方法。 你用test sref= new test();代替That sref = super; 会输出 this.nm()=more sref.nm()=that super.nm()=that
我觉得你的程序有点问题. 1:首先That sref = super;是不允许这样写的. 如果你要使sref为That的一个实例,你可以这样That sref = new That();就行了. 2:That sref = super;这是错误的程序语句.(至少在jdk1.3是这样的) 试试这个: class That { protected String nm() { return "That"; } }class More extends That { public More(){ } protected String nm() { return "More"; } protected void print() { That sref = new That(); System.out.println("this.nm() = " + this.nm()); System.out.println("sref.nm() = " + sref.nm()); System.out.println("super.nm() = " + super.nm()); } }public class TryIt{ public TryIt(){ }
public static void main(String args[]){ More more = new More(); more.print(); } } 3:这是有关super class 和sub class的解释:public class One { int i; public One(int j){ i = j; System.out.println(i); } } public class Two extends One { int k=20; super(k); } 输出20.
this.nm()=more
sref.nm()=more
super.nm()=that
super 提供你访问父类的方法,并不是实例,另外super()代表父类的构造方法。
你用test sref= new test();代替That sref = super;
会输出
this.nm()=more
sref.nm()=that
super.nm()=that
1:首先That sref = super;是不允许这样写的.
如果你要使sref为That的一个实例,你可以这样That sref = new That();就行了.
2:That sref = super;这是错误的程序语句.(至少在jdk1.3是这样的)
试试这个:
class That {
protected String nm() {
return "That"; }
}class More extends That { public More(){
}
protected String nm() {
return "More";
} protected void print() {
That sref = new That();
System.out.println("this.nm() = " + this.nm());
System.out.println("sref.nm() = " + sref.nm());
System.out.println("super.nm() = " + super.nm());
}
}public class TryIt{
public TryIt(){
}
public static void main(String args[]){
More more = new More();
more.print();
}
}
3:这是有关super class 和sub class的解释:public class One
{
int i;
public One(int j){ i = j;
System.out.println(i); }
} public class Two extends One
{
int k=20;
super(k);
} 输出20.