【问题标题】:do I need every method in a class to be in the constructor for subclasses to inherit?我是否需要类中的每个方法都在子类继承的构造函数中?
【发布时间】:2015-08-19 01:45:05
【问题描述】:

所以,我有一个我自己制作的课程,叫做战斗机。我让它工作了一分钟。然后,我添加了这个方法:

private void roll(double block){
    block = (int)(Math.random() * 100);
}

这听起来可能很愚蠢,但是我需要将它添加到构造函数中以继承类吗?我想用一个子类来做这个

public void attacked(int baseDamage){
  if (human.roll()==block){
     return "Nothing happens";
  }
  else{
     a = human.roll();
     human.health()-= a;
  }
  if (human.health > 0){
     System.out.println(health);
  }

}

那么,我是在构造函数中添加 roll() 还是有其他方法呢?

【问题讨论】:

  • 回答您的问题:没有。但我实际上不明白你在问什么......
  • 您是否尝试过使用 extend 关键字来共享所有方法和变量?
  • 您不能“向构造函数添加方法”。但是,也许这已经回答了你的问题:私有方法不是继承的,也不能从类外部调用
  • 实际上是否允许继承你的基类并不取决于你的方法,而是final关键字和你的类的范围
  • 我认为您需要发布更多代码。你的问题没有多大意义。您是否尝试从派生类调用 roll()。在这种情况下,您需要将 roll() 公开。

标签: java inheritance parameters arguments


【解决方案1】:

您可以做的是在子类中使用extend 关键字,并将void 方法attacked 变成一个单独的类。现在它们都将共享完全相同的方法,并且您仍然可以将东西返回给彼此。

解决此问题的另一种方法是再次将这两个方法分为两个单独的类,并将子类嵌套在最后一个括号上方的超类下方。如果这是较大代码的一部分,您可以将多个序列嵌套在一起,或者只是将多个方法放在一起。不过,您不需要将其添加到构造函数中,以上两个原因只是您可以尝试的事情,它们会给您相同的答案。

因为你正在返回一些你不需要将该方法添加到构造函数的东西!

【讨论】:

  • 谢谢。嗯,这告诉我一些关于我的子类有什么问题的事情。现在我知道这不是问题所在,我现在将继续尝试找出我的子类的实际问题。
  • 很高兴我能帮上忙。我可以帮你找到子类的问题,只要告诉我这段代码的目标是什么,我会尝试找到尽可能多的不同方法来解决问题!
【解决方案2】:

好好回答问题

我是否需要类中的每个方法都在构造函数中 要继承的子类?

答案是否定的。只需在构造函数中调用你想要的。如果您想使用子类中的某个方法,则只需创建该类的实例并调用该方法

Object object = new Object("your constructor info"); 
object.newMethod. 

这是你的要求吗?

子类继承了超类的所有字段和方法,您可以添加字段/变量和方法来设置它。构造函数除外。

 /* Name of the class has to be "Main" only if the class is public. */
public class Fighter {

    // the Fighter class has three fields
    public int health;
    public int attack;
    public int speed;

    // the Fighter class has one constructor
    public Fighter(int health, int attack, int speed) {
        this.health = health;
        this.attack = attack;
        this.speed = speed;
    }
     // the Figher class has one method, so far...    
    public void roll(double block){
        block = (int)(Math.random() * 100);
    }
}

public class Attacker extends Fighter {

    // the Attacker subclass adds one field
    public int damage;

    // the Attacker subclass has one constructor
    public Attacker(int damage,
                        int health,
                        int attack,
                        int speed) {
        super(health, attack, speed);
        this.damage = damage;
    }   

    // the Attacker subclass adds one method
    public void attacked(int baseDamage){
        // Is human another class , then Fighter or did you mean Figher ? 
        if (super.roll()==block){
             return "Nothing happens";
        }
        else{
          int a = super.roll();
          human.health()-= damage;
        }
        if (super.health > 0){
           System.out.println(health);
        }  
    }
}
}

您也可以通过使用关键字 super 来调用被覆盖的方法。

super.roll();

这更好的帮助?

【讨论】:

  • 谢谢。这回答了我的问题。
  • 嗯,我想这个答案一定让你感到困惑。这是不正确的。您可以从子类方法访问超类方法,如果它是受保护的或公共的……不涉及实例化。
  • 是的,如果您从子类调用超级方法,那是真的。
猜你喜欢
  • 2022-11-03
  • 1970-01-01
  • 2018-06-06
  • 2015-06-01
  • 1970-01-01
  • 2012-09-02
  • 2013-01-24
  • 2014-04-23
  • 1970-01-01
相关资源
最近更新 更多