谢谢! 解决方案 如果两个类都实现了相同的方法,你应该考虑创建一个interface.界面非常强大且易于使用.你可以调用你的界面Shootable.您可以创建一系列不同的对象来实现Shootable,并一视同仁.//用一个方法定义一个非常简单的接口.界面可拍摄{public voidbeingShot();}//任何实现此接口的类都可以互换对待类左轮手枪实现可射击{公共无效beingShot(){System.out.println("左轮手枪:发射1发");}类 MachineGun 实现 Shootable {公共无效beingShot(){System.out.println("机枪:发射 50 发");}}类 HockeyPuck 实现 Shootable {公共无效beingShot(){System.out.println("冰球:80 MPH slapshot");}}类 RayBourquePuck 实现 Shootable {公共无效beingShot(){System.out.println("冰球:110 MPH slapshot");}}类 OunceOfWhiskey 实现 Shootable {公共无效beingShot(){System.out.println("Whisky Shot: 1 oz down the hat...");}}//你可以声明一个实现 Shootable 的对象数组Shootable[] 射手 = 新的 Shootable[4];//您可以在数组中存储任何 Shootable 对象:射手[0] = new MachineGun();射手[1] = 新左轮手枪();射手[2] = new HockeyPuck();射手[3] = new OunceOfWhiskey();//Shootable 对象可以引用数组中的任何项目可拍摄任何可拍摄物品;//同一个对象可以引用 MachineGun 或 HockeyPuckanyShootableItem = 射手[0];anyShootableItem.beingShot();anyShootableItem = 射手[2];anyShootableItem.beingShot();//您可以在不强制转换的情况下对数组中的任何项目调用beingShot射手[0].beingShot();射手[1].beingShot();//让我们拍摄每个物体来玩得开心:对于(可射击的:射手){s.beingShot();}这是一个很棒的相关问答.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! 解决方案 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 interchangeablyclass 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 ShootableShootable[] 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 arrayShootable anyShootableItem;// The same object can to refer to a MachineGun OR a HockeyPuckanyShootableItem = shooters[0];anyShootableItem.beingShot();anyShootableItem = shooters[2];anyShootableItem.beingShot();// You can call beingShot on any item from the array without castingshooters[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. 这篇关于Java - 具有相同方法的不同对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-27 18:52