gpt4 book ai didi

Java method().method() 调用

转载 作者:行者123 更新时间:2023-11-29 08:06:07 28 4
gpt4 key购买 nike

如果可能的话,如何从另一个方法的返回中调用一个方法

例如……

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/

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