我的prgram中有一系列对象,我希望将其保存在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(指数).whateverMethodIWant(); 会工作,但它想要知道我从哪个类调用方法,我该如何解决这个问题?
答案 0 :(得分:3)
要做你想要的事情,你的编码,反射肯定是你最好的(但不是最明智的)选择。您提供的示例使用对象的方法名称,这些对象建议您已经知道它们是什么类型,如OP的注释所述。
对象多态性背后的想法是,您可以在编译时不知道它们的类型来定义和调用对象的不同行为。
执行所需操作的更常见且可能“正确”的方法是定义一个接口或抽象基类,它描述您尝试存储在向量中的对象族。
此接口将定义并公开一组可在实现该接口的任何对象上调用的常用方法/操作,具体实现由实现类决定。
例如,假设您定义了一个接口Enemy
,并定义了一个方法getAction()
public interface Enemy {
public void getAction();
}
你可以让你的“敌人”类(我们将使用你的Dog,Cat和Submarine的例子)实现这个接口,并用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();
}
}
然后,您可以创建Vector
个Enemy
类型,只需在其上调用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!"
}
答案 1 :(得分:2)
您已将v声明为对象向量。 Object类没有getX或getY方法,这就是编译器给你一个错误的原因。
如果要对v的元素使用getX和getY方法,则需要将其声明为存储具有这些方法的对象。这样做的方法是使用接口:
public interface Coordinated {
int getX();
int getY();
}
public class Submarine implements Coordinated {
....
}
List<Coordinated> v = new Vector<>();
v.add(new Submarine(...));
v.get(1).getX();