如果可能的话,如何从另一个方法的返回
中调用一个方法
?
例如……
class Example {
public static void main(String[] args) {
Point t1 = new Point(0,0);
Point t2 = new Point(0,1);
ArrayList pointContainer = new ArrayList();
pointContainer.add(0,t1);
pointContainer.add(0,t2); // We now have an ArrayList containing t1 & t2
System.out.println(pointContainer.get(1).getLocation()); // The problem area
}
}
在这个写得不好的例子中,我试图在 的索引项 1 上调用
。getLocation()
方法(java.swing.awt
的一部分) >点容器
当尝试编译程序时,出现以下错误...
HW.java:20: error: cannot find symbol
System.out.println(test.get(1).getLocation());
^
symbol: method getLocation()
location: class Object
有人可以帮我解决这个问题吗
最佳答案
首先,键入您的 ArrayList,以便 Java 可以知道从中产生了哪些对象。
List<Point> pointContainer = new ArrayList<Point>();
然后,您从 ArrayList 中检索到的任何对象都将属于 Point
类型,因此您可以对它们执行操作。
关于Java method().method() 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11129957/