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);
}
}
上一篇: 农村创业好项目:一台电脑一根网线做自媒体
下一篇: 4个小本投资创业项目!更适合白手起家