package com.qingfeng;
/**
*
* @author Administrator
*功能:多态:一个对引用(类型)在不同情况的不同状态 (关键父类的引用 方法的重写(覆盖))
*/
public class Demo4_1 {
public static void main(String[] args) {
/*
Animal an = new Cat();
an.show();
an = new Dog();
an.show();*/
Master master = new Master();
master.feed(new Cat(), new Fish());
master.feed(new Dog(), new Bone());
}
}
class Animal
{
String name;
int age;
//generate Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void show()
{
System.out.println("我也不知道我是什么");
}
}
class Cat extends Animal
{
String name;
public void show()
{
System.out.println("我是猫猫!");
}
}
class Dog extends Animal
{
String name;
public void show()
{
System.out.println("我是狗狗!");
}
}
class Food
{
String name;
public void showFood()
{
}
}
class Fish extends Food
{
String name;
public void showFood()
{
System.out.println("吃鱼");
}
}
class Bone extends Food
{
String name;
public void showFood()
{
System.out.println("吃骨头!");
}
}
//运用多态 只用写一个方法即可 再增加子类对象 本方法不用改变,只需通过父类的引用即可~
class Master
{
public void feed(Animal an, Food fo)
{
an.show();
fo.showFood();
}
}