java - 在构造函数中使用公共(public)最终成员变量和可重写方法

标签 java constructor scope overriding

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

这是我的课:

/** 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 类中编写 getCurrentHealth()resetMaxHealth() 等方法。 CurMax 类除构造函数外还有 10 个方法,而 stats 类有 12 个类型类似于 CurMax 的成员,因此需要在 Creature 类中编写 100 多个附加函数。鉴于此,我使用 public final 成员的方式是否合适?如果不是,那么另一种更令人满意的技术是什么?

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

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

Stats 和 Attack 的构造函数很大,所以我不能简单地将 Stats 对象和 Attacks 数组传递给构造函数。在子类的构造函数中调用 super() 的时间长得令人无法接受。

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

最佳答案

为什么不也为 StatsHealth 等提供 getter?然后你不需要使用公共(public)实例变量并且调用没有太大区别:

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

如果您使用 IDE,那么它会为您创建 getter 和 setter,因此在我看来,没有理由不限制对实例变量的访问。这也是约定俗成的问题。人们只是不习惯这种事情,其他使用您的代码的人会更容易感到困惑。

关于在构造函数中调用可重写的方法:如果你传递某种 Abstract Factory,你总是可以绕过这个对象从子类到父类。你说你的子类知道如何选择他们自己的统计数据和攻击——而不是使用一个(抽象的)可覆盖方法的实现,你将一个工厂接口(interface)的实例从具体实现传播到父类:

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

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

关于java - 在构造函数中使用公共(public)最终成员变量和可重写方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7059482/

相关文章:

无法反序列化访问实例字段和方法的 Java 8 lambda

c# - 设置变量 - 构造函数/获取/设置 C#

c++使用枚举创建类会产生错误

java - 使用 super() 时,构造函数调用必须是构造函数中的第一条语句;

java - 为 @ExceptionHandler 编写 JUnit 测试

Java Swing 应用程序意外终止

java - 无法从 SDN4 检索一组父类(super class)型对象

iphone - 强制范围栏位于 UISearchBar 下方

c++ - 指向未在范围内声明的节点的指针数组

c# - 使用 ProtectedData.Protect 时的 DataProtectionScope