gpt4 book ai didi

java - 如果您不知道 Java 中的对象来自哪个类,如何从 vector 中的对象引用方法?

转载 作者:行者123 更新时间:2023-12-01 17:00:44 24 4
gpt4 key购买 nike

我的程序中有一系列对象,我想将它们保存在 vector 中,如下所示:

public class Submarine{ //class that describes submarines

//...some code

int x, y;

public Submarine(){

x = 10;
y = 93;

}

public int getX(){
return x;
}

public int getY(){
return y;
}

//...some code

}



public class Dog extends animal{ //class that describes dogs

//...some code

public Dog(){

x = 12;
y = 54;

}

public String getBark(){

return"Bark!"
}

public int getX(){
return x;
}

public int getY(){
return y;
}

//...some code

}


public class Cat extends animal{ //class that describes cats

//...some code

public Cat(){

x = 25;
y = 532;

}

public String getMeow(){

return"Meow!"
}

public int getX(){
return x;
}

public int getY(){
return y;
}

//...some code

}


public class animal { // the superclass of the animals Dogs and Cats

//some code

int x, y; // the x and y coordinates of the animal

//some code

}

在我的主类中,我还有一个 Vector,我想将这些对象保存在其中:

import java.util.Vector;


public class MainClass { //the main class that runs the program

public static void main(String[] args){

Cat c = new Cat();
Submarine s = new Submarine();
Dog d = new Dog();

Vector<Object> v = new Vector<Object>(); //Vector that holds random objects

v.add(c);
v.add(s);
v.add(d);

}

}

现在我希望能够引用每个对象的方法,因此我尝试将以下代码放入 MainClass 的末尾:

System.out.println(v.get(1).getMeow()); //should print "Meow!"
System.out.println(v.get(2).getBark()); //Should print "Bark!"
System.out.ptintln(v.get(3).getX()); //should print 10 from the submarine object

甚至:

for(int i = 0; i <= 2; i++){
System.out.println("X coordinate: " +v.get(i).getX + ", Y coordinate: " + v.get(i).getY);
}

但它却让我摇摇欲坠,并喊出异常。如何将对象放入 Vector(不以任何方式相关的对象)并在不知道我引用的对象的类的情况下引用每个类内部的方法? - 这是我的主要问题!

我希望: v.get(index).whateverMethodIWant();可以,但它想知道我从哪个类调用方法,我该如何解决这个问题?

最佳答案

要在编码时做您想做的事情,反射肯定是您最好的(但不是最明智的)选择。您提供的示例使用对象的方法名称,这表明您已经知道它们是什么类型,正如OP的注释中所指出的。

对象多态性背后的想法是,您可以定义和调用对象的不同行为,而无需在编译时知道它们的类型。

更常见且也许“正确”的方法是定义一个接口(interface)或一个抽象基类来描述您尝试存储在 vector 中的对象系列。

该接口(interface)将定义并公开一组常用方法/操作,您可以在实现该接口(interface)的任何对象上调用这些方法/操作,具体实现由实现类决定。

例如,假设您定义了一个接口(interface) Enemy 并且它定义了一个方法 getAction()

public interface Enemy {
public void getAction();
}

您可以让您的“敌人”类(我们将使用您的 Dog、Cat 和 Submarine 示例)实现此接口(interface),并将特定函数包装在 getAction 方法中。

public Cat extends Animal implements Enemy {
//....
public void getAction() {
getMeow(); // call cat's specific action
}
}

public Dog extends Animal implements Enemy {
//....
public void getAction() {
getBark(); // call dogs's specific action
}
}

public Submarine implements Enemy {
// ....
public void getAction() {
// for sake of argument, let's just getX();
getX();
}
}

然后,您可以创建 Enemy 类型的 Vector,并简单地对它们调用 getAction(),而无需实际知道类型。

public static void main(String[] args){

Cat c = new Cat();
Submarine s = new Submarine();
Dog d = new Dog();

List<Enemy> v = new Vector<>(); //Vector that holds random Enemy objects

v.add(c);
v.add(s);
v.add(d);

System.out.println(v.get(0).getAction()); //should print "Meow!"
System.out.println(v.get(1).getAction()); //should print 10 from the submarine object
System.out.ptintln(v.get(2).getAction()); //should print "Bark!"
}

关于java - 如果您不知道 Java 中的对象来自哪个类,如何从 vector 中的对象引用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27772040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com