![](/img/trans.png)
[英]How can I use a Variable from a Class Runner to use in a Method in another Class? (BlueJ, Java)
[英]How can I use a local variable to use a method from another class in BlueJ?
基本上我有两个班级国家和城市。 在城市中,我有一个名为 getSize 的方法。 我想在 Country 类中使用这个方法,所以我尝试使用这个:
City c = City.getSize();
要将方法存储为局部变量,但我收到有关不兼容类型或静态方法的错误?
你能显示错误吗?
你为什么不创造
City c = new City();
int size = c.getSize();
如果您的方法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中做这样的事情。 您应该首先创建该类的一个实例,然后您可以使用该实例多次调用该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.