//重点理解:父类对象由子类实例化即可
class Person { //创建父类Person
public void talk() {
System.out.println("Person.talk()");
}
public void foot() {
System.out.println("Person.foot");
}
}
class Woman extends Person {
public void talk() {
System.out.println("Woman.talk()");
}
public void baby() {
System.out.println("Woman.baby()");
}
}
public class Override {
public static void main(String[] args) {
Person person = new Woman(); //父类对象由子类实例化
person.foot();
person.talk();
Person p = new Person();
p.talk();
}
}
结果为:
Person.foot
Woman.talk()
Person.talk()
继承实例
最新推荐文章于 2021-07-25 16:44:40 发布