Java Multiple Inheritance

前端 未结 17 1458
猫巷女王i
猫巷女王i 2020-11-22 09:36

In an attempt to fully understand how to solve Java\'s multiple inheritance problems I have a classic question that I need clarified.

Lets say I have class Ani

17条回答
  •  野性不改
    2020-11-22 10:25

    I think it depends very much on your needs, and how your animal classes are to be used in your code.

    If you want to be able to make use of methods and features of your Horse and Bird implementations inside your Pegasus class, then you could implement Pegasus as a composition of a Bird and a Horse:

    public class Animals {
    
        public interface Animal{
            public int getNumberOfLegs();
            public boolean canFly();
            public boolean canBeRidden();
        }
    
        public interface Bird extends Animal{
            public void doSomeBirdThing();
        }
        public interface Horse extends Animal{
            public void doSomeHorseThing();
        }
        public interface Pegasus extends Bird,Horse{
    
        }
    
        public abstract class AnimalImpl implements Animal{
            private final int numberOfLegs;
    
            public AnimalImpl(int numberOfLegs) {
                super();
                this.numberOfLegs = numberOfLegs;
            }
    
            @Override
            public int getNumberOfLegs() {
                return numberOfLegs;
            }
        }
    
        public class BirdImpl extends AnimalImpl implements Bird{
    
            public BirdImpl() {
                super(2);
            }
    
            @Override
            public boolean canFly() {
                return true;
            }
    
            @Override
            public boolean canBeRidden() {
                return false;
            }
    
            @Override
            public void doSomeBirdThing() {
                System.out.println("doing some bird thing...");
            }
    
        }
    
        public class HorseImpl extends AnimalImpl implements Horse{
    
            public HorseImpl() {
                super(4);
            }
    
            @Override
            public boolean canFly() {
                return false;
            }
    
            @Override
            public boolean canBeRidden() {
                return true;
            }
    
            @Override
            public void doSomeHorseThing() {
                System.out.println("doing some horse thing...");
            }
    
        }
    
        public class PegasusImpl implements Pegasus{
    
            private final Horse horse = new HorseImpl();
            private final Bird bird = new BirdImpl();
    
    
            @Override
            public void doSomeBirdThing() {
                bird.doSomeBirdThing();
            }
    
            @Override
            public int getNumberOfLegs() {
                return horse.getNumberOfLegs();
            }
    
            @Override
            public void doSomeHorseThing() {
                horse.doSomeHorseThing();
            }
    
    
            @Override
            public boolean canFly() {
                return true;
            }
    
            @Override
            public boolean canBeRidden() {
                return true;
            }
        }
    }
    

    Another possibility is to use an Entity-Component-System approach instead of inheritance for defining your animals. Of course this means, that you will not have individual Java classes of the animals, but instead they are only defined by their components.

    Some pseudo code for an Entity-Component-System approach could look like this:

    public void createHorse(Entity entity){
        entity.setComponent(NUMER_OF_LEGS, 4);
        entity.setComponent(CAN_FLY, false);
        entity.setComponent(CAN_BE_RIDDEN, true);
        entity.setComponent(SOME_HORSE_FUNCTIONALITY, new HorseFunction());
    }
    
    public void createBird(Entity entity){
        entity.setComponent(NUMER_OF_LEGS, 2);
        entity.setComponent(CAN_FLY, true);
        entity.setComponent(CAN_BE_RIDDEN, false);
        entity.setComponent(SOME_BIRD_FUNCTIONALITY, new BirdFunction());
    }
    
    public void createPegasus(Entity entity){
        createHorse(entity);
        createBird(entity);
        entity.setComponent(CAN_BE_RIDDEN, true);
    }
    

提交回复
热议问题