java继承多态(多个继承)

package demo1;

public class Animal {
        public String name;
        public int age;

        public Animal(String name, int age) {
            this.name = name;
            this.age = age;
        }
        public void eat() {
            System.out.println(this.name +" 正在吃饭.....");
        }
    }
package demo1;
    public class Bird extends Animal{
        public Bird(String name, int age) {
            super(name, age);
        }

        @Override
        public void eat() {
            System.out.println(this.name +" 正在吃鸟粮...");
        }
        public void fly() {
            System.out.println(this.name +" 正在飞.....");
        }
    }
package demo1;
    public class Dog extends Animal{
        public Dog(String name, int age) {
            super(name, age);
        }

        public void bark() {
            System.out.println(this.name+" 正在汪汪汪叫...");
        }

        @Override
        public void eat() {
            System.out.println(this.name +" 正在吃狗粮...");
        }

    }
package demo1;
    public class Test {

        public static void main(String[] args) {
            Animal animal = new Bird("小鸟",2);

            Bird bird = (Bird)animal;

            bird.fly();

            System.out.println("=============");

            Animal animal2 = new Dog("单身狗",10);
            //animal2 这个引用 是否引用了 Bird这个对象
            if(animal2 instanceof Bird) {
                Bird bird2 = (Bird) animal2;

                bird2.fly();
            }else {
                System.out.println("animal2 instanceof Bird not!!!!");
            }
        }

        public static void func(Animal animal) {
            animal.eat();
        }

        public static int add(int a,int b) {
            return a+b;
        }

        public static int add(int a,int b,int c) {
            return a+b+c;
        }

        public static void main3(String[] args) {
            add(1,2);
            add(1,2,3);
        }

        public static void main2(String[] args) {
            Dog dog = new Dog("旺财",10);
            func(dog);
            System.out.println("======");
            Bird bird = new Bird("小鸟",2);
            func(bird);
        }
        public static void main1(String[] args) {
            Animal animal = new Dog("旺财",10);
            animal.eat();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值