提问人:Will Blanchard 提问时间:11/7/2023 更新时间:11/7/2023 访问量:24
我对已初始化的对象使用 getter 方法时遇到问题,但与 getter 方法不在同一类中
I'm having trouble using a getter method for an object that has been initialized, but is not in the same class as the getter method
问:
我正在为我的计算机科学课编写一个基于文本的游戏(我是一个初学者,几乎每天都在学习新东西),我正在尝试制作一种方法来打印出四名党员的信息;但是,我在尝试让 getter 方法获取有关主类中特定播放器对象的信息时遇到了一些麻烦。我正在尝试在不同的类中使用 getter。
以下是使用相关方法的三个类:
public class Main {
public static void main(String[] args) {
UIFunctions.startMenu();
}//End of main method
Characters smallMan = new Characters("Stuart Little", 2, 1.2, 0.45, false);
Characters tallMan = new Characters("Big Mike", 6, 2.0, 0.30, false);
Characters strongMan = new Characters("Barney Buff", 10, 1.5, 0.10, true);
Characters deepPockets = new Characters("Guilo Baggin", 3, 1.6, 0.65, false);
}//End of main
public class Characters {
private static String name;
private static int strength;
private static double height;
private static double luck;
private static boolean isGullible;
public Characters() {}
public Characters(String characterName, int characterStrength, double characterHeight, double characterLuck, boolean isStupid){
name = characterName;
strength = characterStrength;
height = characterHeight;
luck = characterLuck;
isGullible = isStupid;
}
//--------------------------------------------------------
// Getter methods
//--------------------------------------------------------
public static String getCharacterName() {
return name;
}
public static int getCharacterStrength() {
return strength;
}
public static double getCharacterHeight() {
return height;
}
public static double getCharacterLuck() {
return luck;
}
}
public class UIFunctions{
public static void groupMenu(smallMan.getCharacterName) {
}
}
我尝试在方法声明前面排除字符名称,然后直接将 getter 放在 groupMenu 方法中,但是当我测试程序时什么也没发生,它就完成了。
然后我尝试了同样的事情,但这样做:
public static void groupMenu(){
smallMan.getCharacterName();
}
但这也失败了。
答: 暂无答案
评论
groupMenu(smallMan.getCharacterName)
Main
smallMan
static
Main
static