Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

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

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 everything is a sublcass of Object.

So, for example I put T class and CT class into an array called all like so:

 Object all[] = new Object[6];

    all[0] = T1;

    all[1] = CT2;

    all[2] =T3;

    all[3] = CT1;

    all[4] = T2;

    all[5] = CT3;

I skipped the declarations as thats not my problem.

My real issue becomes when I wish to call a function within the array utilizing a loop:

for (int i = 0; i < 6; i++) {

    all[i].beingShot(randomNum, randomNum, AK47.getAccuracy());
}

The classes involved with T and CT respectively both have the beingShot method, which is public.

Eclipse advises casting them as a quick fix. I'm wondering if there is any logical alternative other than creating my own Object class that holds the beingShot method, or adding this to the class of Object, although I feel either of these choices would cause more problems in the long run.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...