public static void main(String[] args){
DemoThis dt=new DemoThis("Kevin","22");
这段代码很简单,不用解释你也应该能看明白。在构造函数中你看到用this.print(),
你完全可以用print()来代替它,两者效果一样。下面我们修改这个程序,来演示super的用法。
class Person{
public int c;
private String name;
private int age;
protected void setName(String name){
this.name=name;
}
protected void setAge(int age){
this.age=age;
}
protected void print(){
System.out.println("Name="+name+" Age="+age);
}
}
public class DemoSuper extends Person{
public void print(){
System.out.println("DemoSuper:");
super.print();
}
public static void main(String[] args){
DemoSuper ds=new DemoSuper();
ds.setName("kevin");
ds.setAge(22);
ds.print();
}
}