我正在从教科书上完成一项任务。我不能从同一个类中调出“poisonAttack”方法。如果有人能给我反馈,我将不胜感激。
public class PoisonMatango extends Matango {
PoisonMatango pm = new PoisonMatango ('A');
public PoisonMatango ( char suffix) {
super(suffix);
}
// The method I am trying call.
public void poisonAttack(Hero h) {
super.attack(h);
int poisonCount = 5;
if ( poisonCount >=0 ) {
System.out.println("The enemy had spread poisonous pollons");
int pollenDamage = h.hp / 5;
h.hp-= pollenDamage;
System.out.println("Hero has received " + pollenDamage + "damage from " );
poisonCount --;
}else
{
System.out.println("No additional attack were made since poisonCount= 0");
}}
}
答案 0 :(得分:0)
您必须使用创建的Class对象'pm'来调用方法,并根据方法定义传递所需的参数。您的代码具有Hero类的对象类型参数
<强> pm.poisonAttack(小时); 强>
以下是上述代码的解决方案: -
//类对象在解决方案中用作参数 公共阶级英雄{
int hp = 100;
}
//超级 公共课Matango {
Hero a;
public Matango(Hero suffix) {
this.a =suffix;
}
// Super Class Method
public void attack(Hero h){
System.out.println("\n\nHero hp var value::"+h.hp);
}
}
// PoisonMatango
public class PoisonMatango extends Matango {
public PoisonMatango ( Hero suffix) {
super(suffix);
}
// The method I am trying call.
public void poisonAttack(Hero h) {
super.attack(h);
int poisonCount = 5;
if ( poisonCount >=0 ) {
System.out.println("The enemy had spread poisonous pollons\n");
int pollenDamage = h.hp / 5;
h.hp-= pollenDamage;
System.out.println("Hero has received " + pollenDamage + "damage from \n" );
poisonCount --;
}else
{
System.out.println("No additional attack were made since poisonCount= 0 \n");
}}
public static void main(String args[])
{
// Create Object of Param class, this example passes object but we can pass simple data type as per method definition.
Hero hr = new Hero();
PoisonMatango pm = new PoisonMatango (hr);
pm.poisonAttack(hr);
}
}