ArrayList containing different objects of the same superclass - how to access method of a subclass

前端 未结 4 1531
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 09:51

Hi I\'m wondering if there is a simple solution to my problem,

I have an ArrayList:

ArrayList  animalList = new          


        
相关标签:
4条回答
  • 2021-01-18 10:34

    You dont need to do any casting. The overridden method should get called [simple polymorphism]

    Animal aAnimal==  this.animalList.get(animalIndex);
    aAnimal.move();
    

    Above code should call bird method if object is of bird, isn't it?

    And casting is not a solution , how will you decide which object to cast? You will have to use instanceOf.

    0 讨论(0)
  • 2021-01-18 10:42

    There really isn't a nice way to do this from the superclass, since the behavior of each subclass will be different.

    To ensure that you're actually calling the appropriate move method, change Animal from a superclass to an interface. Then when you call the move method, you'll be able to ensure that you're calling the appropriate move method for the object you want.

    If you're looking to preserve common fields, then you can define an abstract class AnimalBase, and require all animals to build off of that, but each implementation will need to implement the Animal interface.

    Example:

    public abstract class AnimalBase {
        private String name;
        private int age;
        private boolean gender;
    
        // getters and setters for the above are good to have here
    }
    
    public interface Animal {
        public void move();
        public void eat();
        public void sleep();
    }
    
    // The below won't compile because the contract for the interface changed.
    // You'll have to implement eat and sleep for each object.
    
    public class Reptiles extends AnimalBase implements Animal {
        public void move() {
            System.out.println("Slither!");
        }
    }
    
    public class Birds extends AnimalBase implements Animal {
        public void move() {
            System.out.println("Flap flap!");
        }
    }
    
    public class Amphibians extends AnimalBase implements Animal {
        public void move() {
            System.out.println("Some sort of moving sound...");
        }
    }
    
    // in some method, you'll be calling the below
    
    List<Animal> animalList = new ArrayList<>();
    
    animalList.add(new Reptiles());
    animalList.add(new Amphibians());
    animalList.add(new Birds());
    
    // call your method without fear of it being generic
    
    for(Animal a : animalList) {
        a.move();
    }
    
    0 讨论(0)
  • 2021-01-18 10:50
    Bird getMyBird(Integer aniInteger) {
            Bird b = new Bird();
            //Do somthig with bird object...
    
            return b;
            //get your modifeid bird object
        }
    
        Bird myBird = animalList.get(animalIndex);
    
        myBird.move();
    
    0 讨论(0)
  • 2021-01-18 10:51

    In your case,the following could work,but time complexity is O(n):

    public void moveBird(){
        for(Animal aminal:animalList){
            if(animal instanceof Bird){
                aninmal.move();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题