所以我在java上练习了一下,创建了一个文本冒险游戏,最后得到了一个我很好奇的问题。
据我所知,在一个方法中,声明的所有变量都是局部的,但它是否可以使它们对类本身可见?
例如,查看此代码,
public class GameMain {
public static void main(String[] args) {
GameMain game = new GameMain();
game.itemInit();
Entity test = new Entity("Test", 10, 5, 5, 10, helm_nothing, body_nothing, weapon_nothing, offhandweapon_nothing);
}
public void itemInit() {
helm_s1_init();
body_s2_init();
main_s3_init();
offhand_s4_init();
}
public void helm_s1_init() {
Armor helm_nothing = new Armor("Nothing", 0, 0, 1, 0, 0);
//more here
}
public void body_s2_init() {
Armor body_nothing = new Armor("Nothing", 0, 0, 2, 0, 0);
//more here
}
public void main_s3_init() {
Weapon weapon_nothing = new Weapon("Nothing", 0, 0, 3, 0, 0);
//more here
}
public void offhand_s4_init() {
Weapon offhandweapon_nothing = new Weapon("Nothing", 0, 0, 4, 0, 0);
//more here
}
}
我知道如果我将初始化方法的内容移动到main中,这段代码就可以工作了,但是我希望能够将我的数据组织成易于查看的单独方法。
此代码中的问题是,在声明新实体时,一旦itemInit()完成,helm_nothing,body_nothing,weapon_nothing和offhandweapon_nothing都将被删除,但我正在寻找一种方法来防止这种情况发生,因此实体可能会成功创建。
其次,这是实现这类信息的最佳方式吗?我怎么能以更好的方式做到这一点?
谢谢!
答案 0 :(得分:3)
实际上你应该将这些变量作为类的属性移动,这将是GameMain
。通过这种方式,这些属性将表示对象GameMain
的状态
您调用的init方法对对象的状态进行操作。
执行此操作时,只要game
中的main
变量存在,属性就会存在。
对于测试,您可以使用 getter()方法,这些方法将为GameMain
对象外部提供所需的属性。
public class GameMain {
private Armor helm_nothing;
private Armor body_nothing
private Weapon weapon_nothing;
private Weapon offhandweapon_nothing;
public Armor getHelmNothing(){
return this.helm_nothing();
}
// put getters for the other attributes here
public static void main(String[] args) {
GameMain game = new GameMain();
game.itemInit();
Entity test = new Entity("Test", 10, 5, 5, 10, game.getHelmNothing(), game.getBodyNothing(), game.getWeaponNothing(), game.getOffhandWeaponNothing());
}
public void itemInit() {
helm_s1_init();
body_s2_init();
main_s3_init();
offhand_s4_init();
}
public void helm_s1_init() {
helm_nothing = new Armor("Nothing", 0, 0, 1, 0, 0);
//more here
}
public void body_s2_init() {
body_nothing = new Armor("Nothing", 0, 0, 2, 0, 0);
//more here
}
public void main_s3_init() {
weapon_nothing = new Weapon("Nothing", 0, 0, 3, 0, 0);
//more here
}
public void offhand_s4_init() {
offhandweapon_nothing = new Weapon("Nothing", 0, 0, 4, 0, 0);
//more here
}
}
答案 1 :(得分:0)
您的方法也可以返回一个对象:
public class GameMain {
public static void main(String[] args) {
GameMain game = new GameMain();
Entity test = game.itemInit();
}
public Entity itemInit() {
Armor helm_nothing = helm_s1_init();
Armor body_nothing = body_s2_init();
Weapon weapon_nothing = main_s3_init();
Weapon offhandweapon_nothing = offhand_s4_init();
return new Entity("Test", 10, 5, 5, 10, helm_nothing, body_nothing,
weapon_nothing, offhandweapon_nothing);
}
public Armor helm_s1_init() {
Armor helm_nothing = new Armor("Nothing", 0, 0, 1, 0, 0);
//more here
return helm_nothing;
}
public Armor body_s2_init() {
Armor body_nothing = new Armor("Nothing", 0, 0, 2, 0, 0);
//more here
return body_nothing;
}
public Weapon main_s3_init() {
Weapon weapon_nothing = new Weapon("Nothing", 0, 0, 3, 0, 0);
//more here
return weapon_nothing;
}
public Weapon offhand_s4_init() {
Weapon offhandweapon_nothing = new Weapon("Nothing", 0, 0, 4, 0, 0);
//more here
return offhandweapon_nothing;
}
}