多态1

package com.excer;

public class Animal {
  public String name;

  public void eat(){
    System.out.println("吃东西");
  }

  public static void main(String[] args){
    Animal a = new Dog();
    a.name = "毛毛";
    System.out.println(a.name);
    a.eat();

    a = new Cat();
    a.name = "十元";
    System.out.println(a.name);
    a.eat();
  }
}
/**狗类*/
class Dog extends Animal{
  public String age;

  @Override
  public void eat(){
    System.out.println("吃骨头");
  }
}

/**猫类*/
class Cat extends Animal{
  public String color;

  @Override
  public void eat(){
    System.out.println("吃鱼");
  }
}

posted on 2016-04-04 22:50  哈尔超的移动城堡  阅读(113)  评论(0编辑  收藏  举报

导航