Java - array of different objects that have the same method(s)

前端 未结 4 1059
一生所求
一生所求 2020-12-17 05:57

I am practicing inheritance.

I have two similar classes that I\'d like to assimilate into one array, so I thought to use the Object class as a superclass since ever

相关标签:
4条回答
  • 2020-12-17 06:16

    You need to typecast your object references to appropriate class to call their method..

    For each reference you fetch from your array, you need to check using instanceof operator, of which is the instance referred to by your object reference.. Accordingly you can typecast the reference to that class..

    But Typecasting is an ugly thing.. You should avoid it as far as possible.. If you have to choose which method to invoke based on exact sub class, you should probably go with an Interface.. It is the best way you can achieve what you want here...

    And I think you have got enough information about how to implement it..

    0 讨论(0)
  • 2020-12-17 06:17

    If both classes implement the same method(s), you should consider creating an interface.

    Interfaces are very powerful and easy to use.

    You could call your interface Shootable.

    You can create an array of different objects that implement Shootable and treat them all the same.

    // Define a VERY simple interface with one method.
    
    interface Shootable {
        public void beingShot();
    }
    
    // Any class that implements this interface can be treated interchangeably
    
    class Revolver implements Shootable {
        public void beingShot() {
            System.out.println("Revolver: firing 1 round");
    }
    
    class MachineGun implements Shootable {
        public void beingShot() {
            System.out.println("Machine Gun: firing 50 rounds");
        }
    }
    
    class HockeyPuck implements Shootable {
        public void beingShot() {
            System.out.println("Hockey Puck: 80 MPH slapshot");
        }
    }
    
    class RayBourquePuck implements Shootable {
        public void beingShot() {
            System.out.println("Hockey Puck: 110 MPH slapshot");
        }
    }
    
    class OunceOfWhiskey implements Shootable {
        public void beingShot() {
            System.out.println("Whiskey Shot: 1 oz down the hatch...");
        }
    }
    
    // You can declare an array of objects that implement Shootable
    
    Shootable[] shooters = new Shootable[4];
    
    // You can store any Shootable object in your array:
    
    shooters[0] = new MachineGun();
    shooters[1] = new Revolver();
    shooters[2] = new HockeyPuck();
    shooters[3] = new OunceOfWhiskey();
    
    // A Shootable object can reference any item from the array
    
    Shootable anyShootableItem;
    
    // The same object can to refer to a MachineGun OR a HockeyPuck
    
    anyShootableItem = shooters[0];
    anyShootableItem.beingShot();
    
    anyShootableItem = shooters[2];
    anyShootableItem.beingShot();
    
    // You can call beingShot on any item from the array without casting
    
    shooters[0].beingShot();
    shooters[1].beingShot();
    
    // Let's shoot each object for fun:
    
    for (Shootable s : shooters) {
        s.beingShot();
    }
    

    Here's a great related question and answer.

    0 讨论(0)
  • 2020-12-17 06:17

    You cant do it...since Java does not support extension method. (C# does)

    READ THE LINK BELOW:

    Java equivalent to C# extension methods

    0 讨论(0)
  • 2020-12-17 06:39

    Object doesn't have the method beingShot. If all of the objects in array are of the same class, then your array should be of that same class. Otherwise they all should have same interface implemented or extend the same class. I can't imagine why would you want explicitly extend Object here, it doesn't add any functionality whatsoever.

    0 讨论(0)
提交回复
热议问题