Kev*_*ica 5 java constructor overriding scope
我对我在设计课程时使用的几种技术有疑问.我已经将其一些成员声明为public final而不是private,构造函数调用了overridable方法.我知道这些通常被认为是不好的做法,但我认为在我的情况下它们可能是合理的,我想知道其他人的想法.
这是我的班级:
/** A monster that fights other monsters in my program */
public abstract class Creature
{
    /**
     * Keeps track of the different values determining how powerful a Creature
     * is.
     */
    public static class Stats
    {
        //subclass definition here.
        //This subclass contains a lot of public static members like this:
        public final CurMax health;
    }
    public final static Random RANDOM = new Random();
    //These are the public final members that I'm wondering about.
    public final Stats stats;
    public final Attack[] attacks;
    public final Stopwatch turnStopwatch;
    private String name;
    //This is the constructor I'm wondering about.
    public Creature()
    {
    initMembers();
    stats = generateStats();
    name = RandomValues.NAMES[RANDOM.nextInt(RandomValues.NAMES.length)];
    attacks = generateAttacks();
    turnStopwatch = new Stopwatch(false);
    }
    /**
     * Define any member variables for subclasses in Creature in this method,
     * not in the subclasses' constructors.
     *
     * Using the Creature class requires the Constructor to call overridable
     * methods. This is deliberate because the constructor assigns values to
     * final member variables, but I want the values of these variables to be
     * determined by subclasses. This method allows subclasses to assign values
     * to their own member variables while still controlling the values of the
     * final variables in the Creature class.
     *
     * @see #generateAttacks()
     * @see #generateStats()
     */
    protected abstract void initMembers();
    protected abstract Stats generateStats();
    protected abstract Attack[] generateAttacks();
    public boolean isDead()
    {
        return stats.health.getCurrent() <= 0;
    }
    public String getName()
    {
    return name;
    }
}
我将成员变量声明为公共决赛,因为我计划经常使用它们,并且创建控制对它们的访问的方法将是乏味的.例如,我打算在整个程序中写这样的台词:
creature.stats.health.getCurrent();
creature.stats.health.resetMax();
避免让公众获得的统计信息和健康需要的写作方法,如getCurrentHealth()与resetMaxHealth()整个生物类.除了构造函数之外,CurMax类还有10个方法,stats类有12个类似于CurMax的类型成员,因此需要在Creature类中编写100多个附加函数.鉴于此,我使用公共最终成员的方式是否恰当?如果不是,那么另一种更令人满意的技术是什么呢?
如果使用公共最终成员是好的,那么我在构造函数中使用可覆盖的方法呢?我想允许Creature的每个子类确定自己的算法来创建统计数据和攻击数组.例如,一个生物可能从列表中挑选一些随机攻击,而另一个生物选择对另一个特定生物有效的攻击.由于stats和attacks是最终的变量,不过,他们必须在生物的构造函数中定义.我的解决方法是让构造函数调用可覆盖的方法,以允许子类确定构造函数中的实际赋值的值stats和attacks.
我理解在构造函数中使用可覆盖方法的主要风险是,在子类有机会定义自己的数据成员之前,将调用overriden方法.我认为在我的情况下可以避免这种情况,因为generateStats()和generateAttacks()方法只能在构造函数中使用.另外,我添加了另一个抽象方法initMembers,它在Creature构造函数中调用之前的其他方法.子类可以在调用generateStats()和generateAttacks()之前定义此函数中的任何成员变量.
统计和攻击的构造函数很大,所以我不能简单地将Stats对象和一组Attacks传递给构造函数.在子类的构造函数中调用super()会长得令人无法接受.
我在Creature构造函数中使用可覆盖方法的方式是否合理?如果不是,我该怎么办?
为什么不提供干将Stats,Health等等,太多?那么您不需要使用公共实例变量,并且调用也没有太大区别:
creature.getStats().getHealth().getCurrent();
如果您使用IDE,那么它将为您创建getter和setter,因此在我看来,没有真正的借口不保持对实例变量的访问受限制.这也是一个惯例问题.人们不习惯这种事情,其他人使用你的代码会更容易混淆.
关于在构造函数中调用可覆盖的方法:如果将某种Abstract Factory对象从子类传递给父类,则可以始终绕过此方法.您说您的子类知道如何选择自己的统计和攻击 - 而不是使用(抽象的)可重写方法的实现,您将Factory接口的实例从具体实现传播到父级:
public interface CreatureFactory {
    public Stats newStats();
    public Attack newAttack();
}
public class Creature {
    public Creature(CreatureFactory f) {
      this.stats = f.newStats();
      this.attack = f.newAttack();
    }
}
public class Monster extends Creature {
    public Monster() {
        super(new MonsterFactory());
    }
}
您可以根据需要在Factory方法中定义参数,这样可以根据需要自定义具体的生物类.
| 归档时间: | 
 | 
| 查看次数: | 695 次 | 
| 最近记录: |