-2

基本上我有两个班级国家和城市。在城市中,我有一个名为 getSize 的方法。我想在 Country 类中使​​用此方法,因此我尝试使用此方法:

City c = City.getSize(); 

要将方法存储为局部变量,但我收到有关不兼容类型或静态方法的错误?

4

2 回答 2

2

你能显示错误吗?

你为什么不创造

City c = new City(); 
int size = c.getSize();
于 2012-11-28T13:38:34.667 回答
1

如果您的方法getSize不是静态的,您应该这样做:

City c = new City();
int size = c.getSize(); // use the returned value (assuming it is returning an integer)

如果该方法是静态的,那么您可以直接将其与类名一起使用:

int size = City.getSize();

但我认为你需要第一个,因为所有城市实例都有不同的大小。

对于这个问题:To store the method as a local variable

答:你不能在java中做这样的事情。您应该首先创建该类的一个实例,然后您可以使用该实例多次调用该方法。

于 2012-11-28T13:38:13.463 回答