package July31th; /** * student \teacher 继承person
学生: * 成员变量 name age * 构造方法 无参 带两个参 * 成员方法 setXxx() getXxx().. * eat() sleep() study() * * 老师: * 成员变量 name age * 构造方法 无参 带两个参 * 成员方法 setXxx() getXxx().. * eat() sleep() teach()
*/ class Person{ public String name; public int age; public Person(){} public Person(String name,int age){ this.name=name; this.age=age; } public void setName(String name,int age){ this.name=name; } public String getName(){ return name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void sleep(){ System.out.println("呼呼"); } public void eat(){ System.out.println("老北京炸酱面"); } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } } class student extends Person { public student() { } public student(String name, int age) { super(name, age); } public void study() { System.out.println("好好学习天天向上~"); } @Override public String toString() { return "student{" + "name='" + name + '\'' + ", age=" + age + '}'; } } class teacher extends Person{ public teacher(){} public teacher(String name, int age) { super(name, age); } public void teach(){ System.out.println("春蚕到死丝方尽,蜡炬成灰泪始干"); } @Override public String toString() { return "teacher{" + "name='" + name + '\'' + ", age=" + age + '}'; } } public class ExtendsTest1 { public static void main(String[] args) { student a=new student("小米",16); a.study(); a.eat(); a.sleep(); System.out.println(a); teacher b=new teacher("老刘",23); b.teach(); b.eat(); b.sleep(); System.out.println(b); } }
案例2.
猫狗继承多态版:
package July31th; class Animal_DuoTai{ public String name; public String color; public Animal_DuoTai(){} public Animal_DuoTai(String name,String color){ this.color=color; this.name=name; } public void setName(String name) { this.name = name; } public void setColor(String color) { this.color = color; } public String getName() { return name; } public String getColor() { return color; } public void eat(){} public void sleep(){} public void show(){ System.out.println("它叫"+name +"它是"+color+"色"); } } class Cat_DuoTai extends Animal_DuoTai{ public Cat_DuoTai(){} public Cat_DuoTai(String name,String color){ super(name, color); } public void eat(){ System.out.println("吃鱼"); } public void sleep(){ System.out.println("家里睡"); } public void play(){ System.out.println("玩毛线球"); } public void show(){ System.out.println("它叫"+this.getName() +"动物的颜色"+this.getColor()); } } class Dog_DuoTai extends Animal_DuoTai{ public Dog_DuoTai(){} public Dog_DuoTai(String name,String color){ super(name, color); } public void eat(){ System.out.println("大骨头"); } public void lookDoor(){ System.out.println("遇到陌生人:汪汪汪"); } public void show(){ System.out.println("它叫"+this.getName() +"动物的颜色"+this.getColor()); } } public class DuoTai { public static void main(String[] arg){ Animal_DuoTai a=new Cat_DuoTai("安蓝","蓝色"); a.eat(); a.sleep(); a.show(); Cat_DuoTai cat=(Cat_DuoTai) a;//将 a强转为Cat_DuoTai类就可以调用子类独有方法了!(但是本质还是cat,不能强转为dog因为类型不匹配) cat.play(); Animal_DuoTai b=new Dog_DuoTai("黑黑","black");//多态 b.sleep();//(可以调用父类有的子类没有的) b.eat(); b.show();//不能引用子类独有的(狗狗看大门) Dog_DuoTai dog=(Dog_DuoTai) b;//将 b强转为Dog_DuoTai类就可以调用子类独有方法了!(但是本质还是dog,不能强转为cat因为类型不匹配) dog.lookDoor(); } }