![](/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.