Item
의 하위 클래스 인 Weapon
클래스의 메서드를 호출하려고합니다. 필드에서 볼 수 있듯이 객체의 객체로 currentWeapon
객체를 선언했으며 setCurrentWeapon
메서드에서는 getClass()
메서드를 사용하여 Item
이 실제로 하위 클래스의 무기인지 확인합니다.클래스의 하위 클래스에서 객체에 대한 메서드 호출
Weapon
클래스에서 내 Item 객체 (실제로는 Weapon 클래스 임)의 메서드를 성공적으로 호출 할 수있는 방법이 있습니까?
backpack
은 Item
개체를 포함하는 해시 맵입니다. 필드에 currentWeapon
을 Weapon
으로 설정하면 Weapon
개체가 backpack
에 추가되지 않습니다.
컴파일에 실패있어서 는 (기호를 찾을 수 없습니다 - 방법 getMinDamage())
public int attack(Imperial currentEnemy) {
int damage = Utils.random(currentWeapon.getMinDamage(), currentWeapon.getMaxDamage()+1);
currentEnemy.changeHealth(-damage);
return damage;
}
필드 :
public boolean setCurrentWeapon(String itemToEquip) {
if(useItem(itemToEquip) == true) {
currentWeapon = backpack.get(itemToEquip.toLowerCase());
if(currentWeapon.getClass() == Weapon.class) {
System.out.println(getNick() + " has equipped " + currentWeapon.getName() + " as current weapon");
equipped = true;
return true;
}
else {
System.out.println(itemToEquip + " is not a weapon");
currentWeapon = null;
return false;
}
}
else System.out.println(itemToEquip + " is not owned by " + getNick());
return false;
}
: currentWeapon을 설정하는
이
private Item currentWeapon;
방법
이 질문이 너무 혼란스럽지 않기를 바래요, 명확하게하는 방법에 대한 조언을주십시오. 이것이 문제인 경우
당신의 필드는 'currentWeapon'이라고 불리지 만 Weapon이 아닌 Item 타입을 주어야합니다. 무엇이든 당신의 보트를 떠 다닙니 다. :) – vptheron
아마도 추상적 인 방법을 사용하면 ... 질문이 매우 명확하지 않습니다 ... – DGomez