欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

super和this

程序员文章站 2022-04-18 16:01:46
...

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);// [b]调用父类具有相同形参的构造函数[/b]
prt("his name is:" + name);
}

Chinese(String name, int age) {
this(name);// [b]调用当前具有相同形参的构造函数[/b]
prt("his age is:" + age);
}

public static void main(String[] args) {
Chinese cn = new Chinese();
cn = new Chinese("kevin");
cn = new Chinese("kevin", 22);
}
}