抽象类

1、用abstract 关键字来修饰一个类时,这个类叫做抽象类:用abstract来修饰一个方法时,该方法叫做抽象方法

2、含有抽象方法的类必须被声明为抽象类,抽象类必须被继承,抽象方法必须被重写

3、抽象类不能被实例化

4、抽象方法只需被声明,而不需实现

 

abstract class Aniaml {

  public String name;

  Animal (String name) {

    this.name = name;

    public abstract void enjoy();

  }

}

 

class Cat extends Animal {

  private String eyeColor;

  Cat(String n,String f) {

    super(n);

    this.eyeColor = eyeColor;

  }

  public void enjoy() {

    System.out.println("猫叫声......");

  }

}

 

class Bird extends Animal {

  private String featherColor;

  Bird(String n,String f) {

    super(n);

    featherColor = f;

  }

  public void enjoy() {

    System.out.println("鸟叫声.....");

   }

}

 

class Lady {

  private String name;

  private Animal pet;

  Lady(String name,Animal pet) {

    this.name = name;

    this.pet = pet;

  }

  public void myEnjoy(){

    pet.enjoy();

  }

}

 

public class Test {

  public static void main(String args[]) {

    //Lady l3 = new Lady("l3",new Bird("birthname","green"));

    Cat c = new Cat("catname","blue");

    Bird d = new Bird("dogname","black");

    Lady l1 = new Lady("l1",c);

    Lady l2 = new Lady("l2",d);

  }

}

posted @ 2018-12-27 10:39  雪地里的Alan  阅读(141)  评论(0编辑  收藏  举报