나는 자바로 게임을 만들고 있으며, 가장 이상한 버그를 가지고있다. 나는 무기라는 계급을 가지고있다. 그런 다음 primary라는 이름의 인스턴스를 만듭니다. 내가 인스턴스를 생성하고 그것을 보조라고 부른 후에. 어떤 이상한 이유 때문에, 1 차가 2 차 값으로 덮어 쓰게됩니다. 내 강사와 나는 그것을 보았고 그것을 이해할 수 없었다. 여기 코드는 : 당신의 Weapon
클래스의자바 클래스의 인스턴스를 선언하는 이상한 런타임 오류
public class weapon {
static String type;
static String name;
static int weight;
static int damage;
static int dodge;
weapon(String c, String n, int w, int da, int dod) {
type = c;
name = n;
weight = w;
damage = da;
dodge = dod;
}
//getters
String getType(){
return type;
}
String getName(){
return name;
}
Integer getWeight(){
return weight;
}
Integer getDamage(){
return damage;
}
Integer getDodge(){
return dodge;
}
//setters
void setType(String c){
c=type;
}
void setName(String n){
n=name;
}
void setWeight(Integer w){
w=weight;
}
void setDamage(Integer da){
damage=da;
}
void setDodge(Integer dod){
dodge=dod;
}
}
/*At the top of my main class I create both instances like this because the instances are created in if statements and I need to access them.*/
weapon primary;
weapon secondary;
//I create primary like this earlier in the code like this
primary = new weapon("primary","sword", 8, 6, -1);
//and then when I run this I get the output "sword" "Heavy Sword".
System.out.println(primary.getName());
secondary = new weapon("secondary", "huge sword", 9, 7, -2);
System.out.println(primary.getName());
를 설명 관련 코드는 여기에 있습니다. 덕분에 – Braj