【问题标题】:Accessing parent class instance variables from child class instances从子类实例访问父类实例变量
【发布时间】:2016-02-14 10:49:18
【问题描述】:

所以现在,我有一个 Preprocessor 类,它生成一堆实例变量映射,还有一个 Service 类,它有一个 setPreprocessor(Preprocessor x) 方法,所以 Service 类的实例能够访问预处理器生成的映射。

目前我的Service类需要依次调用三个方法;为了简单起见,我们称它们为executePhaseOneexecutePhaseTwoexecutePhaseThree。这三个方法中的每一个都实例化/修改Service 实例变量,其中一些是指向Service 实例的Preprocessor 对象的指针。

我的代码现在有这样的结构:

Preprocessor preprocessor = new Preprocessor();
preprocessor.preprocess();
Service service = new Service();
service.setPreprocessor(preprocessor);
service.executePhaseOne();
service.executePhaseTwo();
service.executePhaseThree();

为了更好地组织我的代码,我想将每个executePhaseXXX() 调用放在它自己的Service 的单独子类中,并将所有阶段的公共数据结构保留在父类Service 中。然后,我想在Service 父类中有一个execute() 方法连续执行所有三个阶段:

class ServiceChildOne extends Service {
    public void executePhaseOne() {
        // Do stuff
    }
}

class ServiceChildTwo extends Service {
    public void executePhaseTwo() {
        // Do stuff
    }
}

class ServiceChildThree extends Service {
    public void executePhaseThree() {
        // Do stuff
    }
}

编辑:

问题是,我如何在Service 父类中编写我的execute() 方法?我有:

public void execute() {
    ServiceChildOne childOne = new ServiceChildOne();
    ServiceChildTwo childTwo = new ServiceChildTwo();
    ServiceChildThree childThree = new ServiceChildThree();
    System.out.println(childOne.preprocessor); // prints null
    childOne.executePhaseOne();
    childOne.executePhaseTwo();
    childOne.executePhaseThree();
}

但是,我的 childOnechildTwochildThree 对象无法访问位于父类 Service 中的 preprocessor 实例变量...我该如何解决这个问题?

【问题讨论】:

    标签: java


    【解决方案1】:

    ServicePreprocessor 实例变量使用protected 修饰符,如下所示:

    public class Service {
        protected Preprocessor preprocessor;
    }
    

    那么Service 的每个子类都有一个this.preprocessor

    【讨论】:

    • 它已经受到保护,但它仍然为空。请参阅原始帖子中的编辑。
    【解决方案2】:

    您可以在您的 Service 类中提供一些类似 getPreprocessorInstance() 的方法,该类返回 Preprocessor 实例

    【讨论】:

      【解决方案3】:

      您的preprocessor 应该是protectedpublic 才能让孩子访问。 你可以阅读修饰符here

      更新

      new ServiceChildOne(new Preprocessor());
      .....
      
      class ServiceChildOne extends Service {
      
          public ServiceChildOne(Preprocessor preprocessor) {
             super.preprocessor = preprocessor;
          }
      
          public void executePhaseOne() {
              // Do stuff
          }
      }
      

      【讨论】:

      • 它已经是protected,但它仍然为空。请参阅原始帖子中的编辑。
      • 谢谢。后续行动:我需要能够访问 all 存在于 Service 父类中的实例变量,而不仅仅是预处理器。除了将它们全部传递给子类的构造函数之外,还有其他方法可以获得此功能吗?也许是某种实例化子类实例的方法?有任何想法吗?谢谢。
      【解决方案4】:

      看起来您的问题是您没有一个,而是四个不同的 Service 实例 - 每个实例都有自己的未初始化的基类变量副本。

      目前我能想到的唯一解决方案是,首先,将 Service 成员变量设为静态的设计相当糟糕 - 实际上,这意味着您一次只能拥有一个 Service 版本。在我看来,更好的解决方案是将处理阶段设为子类,而是将它们设为独立的类,将Service 的实例作为参数。

      编辑: 举个简单的例子,Service 类可能如下所示:

      class Service
      {
          public Service() { ... }
          public Preprocessor getPreprocessor() { ... }
          public void setPreprocessor(Preprocessor preprocessor { ... }
          public Type2 getVariable2() { ... }
          public void setVariable2(Type2 variable2) { ... }
              ...
      }
      

      阶段类可能类似于:

      class ServicePhaseOne
      {
          private Service m_dataHost;
      
          public ServicePhaseOne(Service dataHost)
          {
              m_dataHost = dataHost;
          }
      
          public void executePhaseOne()
          {
              // Do phase 1 stuff
          }
      }
      

      ...对于第 2 阶段和第 3 阶段,依此类推。

      execute() 方法如下所示:

      public void execute()
      {
          ServicePhaseOne phaseOne = new ServicePhaseOne(this);
          ServicePhaseTwo phaseTwo = new ServicePhaseTwo(this);
          ServicePhaseThree phaseThree = new ServicePhaseThree(this);
          phaseOne .executePhaseOne();
          phaseTwo .executePhaseTwo();
          phaseThree .executePhaseThree();
      }
      

      【讨论】:

        猜你喜欢
        • 2011-02-21
        • 2010-10-23
        • 1970-01-01
        • 2021-11-07
        • 1970-01-01
        • 2021-08-28
        • 2019-02-01
        • 2012-08-20
        • 1970-01-01
        相关资源
        最近更新 更多