繁体   English   中英

在构造函数中使用公共最终成员变量和可覆盖方法

[英]Using Public Final Member Variables and Overridable Methods in a Constructor

我对我在设计 class 时使用的几种技术有疑问。 我已经将它的一些成员声明为 public final 而不是 private,并且构造函数调用可覆盖的方法。 我知道这些通常被认为是不好的做法,但我认为在我的情况下它们可能是合理的,我想知道其他人的想法。

这是我的 class:

/** 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;
    }
}

我将成员变量声明为 public final,因为我计划经常使用它们并且创建方法来控制对它们的访问会很乏味。 例如,我计划在整个程序中编写这样的行:
creature.stats.health.getCurrent();
creature.stats.health.resetMax();
避免让公众访问统计数据和健康需要在整个 Creature class 中编写getCurrentHealth()resetMaxHealth()等方法。 CurMax class 除了构造函数外还有 10 个方法,而统计数据 class 有 12 个与 CurMax 相似的类型成员,因此需要在 Creature ZA2F2ED4F8EBC2CBB4C21A29 中编写超过 100 个附加函数。 鉴于此,我使用公共最终成员的方式是否合适? 如果不是,那么另一种更令人满意的技术是什么?

如果使用公共最终成员没问题,那么我在构造函数中使用可覆盖方法呢? 我想让每个 Creature 子类确定自己的算法来创建统计数据和攻击数组。 例如,一个生物可能会从列表中选择一些随机攻击,而另一个选择对另一个特定生物有效的攻击。 但是,由于statsattacks是最终变量,它们必须在 Creature 的构造函数中定义。 我的解决方法是让构造函数调用可覆盖的方法,以允许子类确定statsattacks的值,同时将实际赋值留在构造函数中。

我知道在构造函数中使用可覆盖方法的主要风险是在子类有机会定义自己的数据成员之前将调用被覆盖的方法。 我认为在我的情况下可以避免这种情况,因为 generateStats() 和 generateAttacks() 方法只能在构造函数中使用。 此外,我还添加了另一个抽象方法 initMembers,它首先在 Creature 构造函数中调用。 在调用 generateStats() 和 generateAttacks() 之前,子类可以在此 function 中定义任何成员变量。

Stats 和 Attack 的构造函数很大,所以我不能简单地将 Stats object 和 Attacks 数组传递给构造函数。 在子类的构造函数中对 super() 的调用会非常长。

我在 Creature 构造函数中使用可覆盖方法的方式是否合理? 如果不是,我还应该怎么做?

为什么不为StatsHealth等提供 getter 呢? 然后您不需要使用公共实例变量,并且调用并没有太大的不同:

creature.getStats().getHealth().getCurrent();

如果您使用 IDE,那么它将为您创建 getter 和 setter,因此我认为没有真正的借口不限制对实例变量的访问。 这也是约定俗成的问题。 人们只是不习惯这种事情,其他使用您的代码的人会更容易混淆。

关于在构造函数中调用可覆盖的方法:如果您将某种抽象工厂object 从子类传递给父类,则始终可以规避这一点。 你说你的子类知道如何选择他们自己的统计和攻击——而不是使用(抽象的)可覆盖方法的实现,你将工厂接口的实例从具体实现传播到父类:

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());
    }
}

您可以根据需要在工厂方法中定义参数,并根据需要自定义具体的生物类。

为什么不能将 generateStats() 和 generateAttack() 方法抽象化?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM