猿问

我可以在不继承它们的类的情况下调用对象方法吗

我在 Zoo.java 中实现了一个不继承任何其他类的数组列表。我将向其中添加一些对象


ArrayList al = new ArrayList(); 

al.add(new Cat());

al.add(new Dog()); 

我无法编写逻辑来告诉我调用哪种类型的对象并使用该类的方法而不使用继承和泛型。


所以我可以在这里使用:


                   for(Animal a:al)

                   class <?> X = obj.getClass(a);

                   ((X)a).somemethod();


隔江千里
浏览 314回答 3
3回答

江户川乱折腾

这里要学习的教训是*多态性”。类,Cat并且Dog应该实现一个公共接口,声明可用于它们的方法:interface Animal {&nbsp; &nbsp;void eat(SomeOtherInterface food);&nbsp; &nbsp;void giveSound(BufferedWriter output);&nbsp; &nbsp;void move();}class Cat implements Animal {&nbsp; &nbsp;@Override public void eat(SomeOtherInterface food){&nbsp; &nbsp; &nbsp; &nbsp; // consume the food&nbsp; &nbsp;}&nbsp; &nbsp;@Override public void giveSound(BufferedWriter output){&nbsp; &nbsp; &nbsp; &nbsp; output.write("Meaw");&nbsp; &nbsp;}&nbsp; &nbsp;@Override public void move(){&nbsp; &nbsp; &nbsp; // move within the world&nbsp; &nbsp;}}class Dog implements Animal {&nbsp; &nbsp;@Override public void eat(SomeOtherInterface food){&nbsp; &nbsp; &nbsp; &nbsp; // consume the food&nbsp; &nbsp;}&nbsp; &nbsp;@Override public void giveSound(BufferedWriter output){&nbsp; &nbsp; &nbsp; &nbsp; output.write("Woof");&nbsp; &nbsp;}&nbsp; &nbsp;@Override public void move(){&nbsp; &nbsp; &nbsp; // move within the world&nbsp; &nbsp;}}然后你给你的collecton一个最通用的通用类型的泛型参数,提供你想要访问的方法:Collection<Animal> myAnimals = new ArrayList();myAnimals.add(new Cat());myAnimals.add(new Dog());try( BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out))){&nbsp; &nbsp;for(Animal animal : animals)&nbsp; &nbsp; &nbsp; animal.giveSound(output);&nbsp;}

哆啦的时光机

我做了一些工作。要访问数组列表中对象的方法,我们可以使用 java.lang.reflect 包。我可以先获取所有声明的方法,然后使用参数调用它们。所以我的程序会是这样的:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public class Methodcall{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ArrayList <Object> al =&nbsp; new ArrayList<Object>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; al.add(new Cat());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; al.add(new Dog());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(Object a:al)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;class ax = a.getClass();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Method[] methods=ax.getdeclaredMethods();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(Method m:methods)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(method.getName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }在此之后,我们可以通过使用反射的其他成员来访问方法。

蛊毒传说

您可以创建超类类型的数组列表。你可以这样做。ArrayList<Zoo>&nbsp;arrayList=new&nbsp;ArrayList<Zoo>();
随时随地看视频慕课网APP

相关分类

Java
我要回答