编程语言
首页 > 编程语言> > 通过内部类的方式-JAVA变相实现多继承

通过内部类的方式-JAVA变相实现多继承

作者:互联网

package com.algorithm;

public class Animal {

    class Cat {
       public void say(){
           System.out.println("小猫喵喵叫");
       }
       public void run(){
           System.out.println("小猫跑跑跑");
       }
       public void jump(){
           System.out.println("小猫跳跳跳");
       }



    }
    class Dog {
        public void say(){
            System.out.println("小狗旺旺叫");
        }
        public void run(){
            System.out.println("小狗跑跑跑");
        }
        public void jump(){
            System.out.println("小狗跳跳跳");
        }

    }

}

  

package com.algorithm;

public class Mammal extends Animal  {
    public static void main(String[] args) {
        Mammal mammal = new Mammal();
        Dog dog = mammal.new Dog();
        dog.say();
        dog.run();
        dog.jump();
        Cat cat = mammal.new Cat();
        cat.say();
        cat.run();
        cat.jump();
    }
}

  

运行结果:

小狗旺旺叫
小狗跑跑跑
小狗跳跳跳
小猫喵喵叫
小猫跑跑跑
小猫跳跳跳

标签:JAVA,继承,void,System,变相,println,小猫,public,out
来源: https://www.cnblogs.com/dongyaotou/p/15598667.html