示例1class Person {
protected void print()
{ System.out.println("The print() in class Person."); }
public class DemoSuper extends Person
{ public void print() { System.out.println("The print() in class DemoSuper."); super.print();// 调用父类的方法 }
public static void main(String[] args) { DemoSuper ds = new DemoSuper(); ds.print(); }
}
结果:The print() in class DemoSuper.The print() in class Person.
示例2
public class DemoThis { private String name;
public void setName(String name)
{ this.name = name;// 前一个name是private name;后一个name是setName中的参数。 }}
示例4
class Person {
public static void prt(String s) { System.out.println(s); }
Person() { prt("A Person."); }
Person(String name) { prt("A person name is:" + name); }}
public class Chinese extends Person { Chinese() { super();// 调用父类构造函数。 prt("A chinese."); }
Chinese(String name) { super(name);// 调用父类具有相同形参的构造函数。 prt("his name is:" + name); }
public static void main(String[] args) { Chinese cn = new Chinese(); cn = new Chinese("kevin"); }
}
结果:A Person.A chinese.A person name is:kevinhis name is:kevin