继承
实例化子类的时候会先调用父类无参的构造方法,
其他的 变量,和方法
会遵循:如果子类重写了,就调用重写后的,否则则调用父类的
class Animal{ //父类成员变量 String name="Animal's Name"; String type="Animal's type"; //父类构造方法 public Animal(){ System.out.println("Animal() method execute"); } public Animal(String animalName){ System.out.println("Animal(String ) method execute"); } //父类方法 public void canEat(){ System.out.println("Animal caneat() method execute"); } public void canBreath(){ System.out.println("Animal canbreath() method execute"); } } public class Fish extends Animal{ //子类的type 成员变量 String type="Fish's type"; //以及两个构造方法 public Fish(){ System.out.println("Fish() method execute"); } public Fish(String fishName){ System.out.println("Fish(String s)method execte"); } //重写父类的canBreath 方法 public void canBreath(){ System.out.println("Fish can breath"); } public static void main(String args[]){ Fish f = new Fish(); System.out.println(f.name); System.out.println(f.type); System.out.println("------------"); new Fish().canEat(); System.out.println("------------"); new Fish("goldFish").canBreath(); } } /*--运行结果 Animal() method execute Fish() method execute Animal's Name Fish's type ------------ Animal() method execute Fish() method execute Animal caneat() method execute ------------ Animal() method execute Fish(String s)method execte Fish can breath */