多态


public class Demo5 {

    public static void main(String[] args) {
    /*Cat cat1=new Cat();
    Dog dog1=new Dog();
    cat1.cry();*/
        //多态性
        /*Animal an=new Cat();
        Food f=new Food();
        an.cry();
        an=new Dog();
        an.cry();
        an.eat();
        f.showName();*/
        Master master=new Master();
        master.feed(new Dog(),new Bond());
    }
}

class Food{
    String name;
    public void showName(){
        
    }
}

class Fish extends Food{
    public void showName(){
        System.out.println("I'm fish");
    }
}
class Bond extends Food{
    public void showName(){
        System.out.println("I'm bond");
    }
}
class Master{
    public void feed(Animal an,Food f){
        an.eat();
        f.showName();
    }
}
class  Animal
{
     String name;
     int age;
    public void cry(){
        System.out.println("I don't know cry");
    }
    public void eat(){
        System.out.println("I don't know eat");
    }
}
class Dog extends Animal{
    public void cry(){
        System.out.println("wang wang");
    }    
    public void eat(){
        System.out.println("I eat bone");
    }
}
class Cat extends Animal{
    public void cry(){
        System.out.println("miao miao");
    }    
    public void eat(){
        System.out.println("I eat fish");
    }
}
    




posted @   头痛不头痛  阅读(209)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示
主题色彩