[英]How to remove an object from an ArrayAdapter that you don't have that object's reference but have another congruent object?
[英]How do you reference a method from an object in a vector, if you don't know what class the object is from in Java?
我的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(index).whateverMethodIWant(); 會起作用,但是它想知道我從哪個類調用方法,如何解決呢?
為了按照編碼進行您想做的事情,反射無疑是您最好的選擇(但不是最明智的選擇)。 如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!"
}
您已將v聲明為Object的向量。 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.