- class Test_PloymorphicAbstractInterface {
- public static void main(String[] args) {
- //创建子类C的对象;
- WolfMan wm=new WolfMan("Miky",25,180);
- wm.speed();
- System.out.println(wm.getName()+"---"+wm.getAge()+"---"+wm.getHigh());
- //父类引用B指向子类C对象;
- Michael m=new WolfMan("BaBaRo",26,175);
- m.sleep();
- m.eat();
- System.out.println(m.getName()+"---"+m.getAge()+"---"+m.getHigh());
- }
- }
- //抽象类A,集合了共性功能;
- abstract class Person{
- private String name;
- private int age;
- private int high;
- public Person(){}
- public Person(String name,int age,int high){
- this.name=name;
- this.age=age;
- this.high=high;
- }
- public void setName(String name){
- this.name=name;
- }
- public String getName(){
- return name;
- }
- public void setAge(int age){
- this.age=age;
- }
- public int getAge(){
- return age;
- }
- public void setHigh(int high){
- this.high=high;
- }
- public int getHigh(){
- return high;
- }
- public void show(){
- System.out.println("NameIs"+name+"---"+"AgeIs"+age+"---"+"HighIs"+high);
- }
- public abstract void sleep();
- public abstract void eat();
- }
- //接口,集合了扩展功能;
- interface Inter{
- public abstract void speed();
- }
- //类A继承抽象类A,重写A的抽象方法;
- class Michael extends Person{
- public Michael(){}
- public Michael(String name,int age,int high){
- super(name,age,high);
- }
- public void sleep(){
- System.out.println("NeverSleep");
- }
-
- public void eat(){
- System.out.println("DrinkAnimal'sBlood");
- }
- }
- //类C继承类B,类实现接口Inter;
- class WolfMan extends Michael implements Inter{
- public WolfMan(){}
- public WolfMan(String name,int age,int high){
- super(name,age,high);
- }
- public void speed(){
- System.out.println("SpeedIsVeryFast");
- }
- }
复制代码 |
|