Java 从子类实例访问父类实例变量

Java 从子类实例访问父类实例变量,java,Java,现在,我有一个预处理器类,它生成了一堆实例变量映射,还有一个服务类,它有一个setPreprocessor(Preprocessor x)方法,这样服务类的一个实例就可以访问预处理器生成的映射 目前,我的Service类需要连续调用三个方法;为了简单起见,我们将它们称为executePhaseOne,executePhaseTwo,和executePhaseTwo。这三种方法都实例化/修改服务实例变量,其中一些是指向服务实例的预处理器对象的指针 我的代码现在具有以下结构: Preprocesso

现在,我有一个
预处理器
类,它生成了一堆实例变量映射,还有一个
服务
类,它有一个
setPreprocessor(Preprocessor x)
方法,这样
服务
类的一个实例就可以访问预处理器生成的映射

目前,我的
Service
类需要连续调用三个方法;为了简单起见,我们将它们称为
executePhaseOne
executePhaseTwo
,和
executePhaseTwo
。这三种方法都实例化/修改
服务
实例变量,其中一些是指向
服务
实例的
预处理器
对象的指针

我的代码现在具有以下结构:

Preprocessor preprocessor = new Preprocessor();
preprocessor.preprocess();
Service service = new Service();
service.setPreprocessor(preprocessor);
service.executePhaseOne();
service.executePhaseTwo();
service.executePhaseThree();
为了更好地组织代码,我想将每个
executePhaseXXX()
调用放在
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
    }
}
编辑:

问题是,如何在
服务
父类中编写
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();
}

但是,我的
childOne
childTwo
childThree
对象无法访问父类
服务
中的
预处理器
实例变量。。。我如何克服这个问题?

您的
预处理器应该受到
保护
公共
才能从孩子那里访问。 您可以阅读有关修饰符的内容

更新

new ServiceChildOne(new Preprocessor());
.....

class ServiceChildOne extends Service {

    public ServiceChildOne(Preprocessor preprocessor) {
       super.preprocessor = preprocessor;
    }

    public void executePhaseOne() {
        // Do stuff
    }
}

服务
预处理器
实例变量使用
受保护
修饰符,如下所示:

public class Service {
    protected Preprocessor preprocessor;
}

然后
服务的每个子类
都有一个
这个。预处理器

你可以在你的
服务
类中提供一些类似
GetPrepreprocessInstance()
的方法,它返回
预处理器

实例,但是
服务的四个不同实例
——每个实例都有自己的基类变量的未初始化副本

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

编辑: 举个简单的例子,
服务
类可以如下所示:

class Service
{
    public Service() { ... }
    public Preprocessor getPreprocessor() { ... }
    public void setPreprocessor(Preprocessor preprocessor { ... }
    public Type2 getVariable2() { ... }
    public void setVariable2(Type2 variable2) { ... }
        ...
}
public void execute()
{
    ServicePhaseOne phaseOne = new ServicePhaseOne(this);
    ServicePhaseTwo phaseTwo = new ServicePhaseTwo(this);
    ServicePhaseThree phaseThree = new ServicePhaseThree(this);
    phaseOne .executePhaseOne();
    phaseTwo .executePhaseTwo();
    phaseThree .executePhaseThree();
}
阶段类可以类似于:

class ServicePhaseOne
{
    private Service m_dataHost;

    public ServicePhaseOne(Service dataHost)
    {
        m_dataHost = dataHost;
    }

    public void executePhaseOne()
    {
        // Do phase 1 stuff
    }
}
。。。第二阶段和第三阶段也是如此

execute()方法将如下所示:

class Service
{
    public Service() { ... }
    public Preprocessor getPreprocessor() { ... }
    public void setPreprocessor(Preprocessor preprocessor { ... }
    public Type2 getVariable2() { ... }
    public void setVariable2(Type2 variable2) { ... }
        ...
}
public void execute()
{
    ServicePhaseOne phaseOne = new ServicePhaseOne(this);
    ServicePhaseTwo phaseTwo = new ServicePhaseTwo(this);
    ServicePhaseThree phaseThree = new ServicePhaseThree(this);
    phaseOne .executePhaseOne();
    phaseTwo .executePhaseTwo();
    phaseThree .executePhaseThree();
}

它已受
保护
,但仍为空。请参阅原始帖子中的编辑。谢谢。不过,还有一个后续问题:我需要能够访问
服务
父类中的所有实例变量,而不仅仅是预处理器。除了将它们全部传递到子类的构造函数之外,还有其他方法可以获得此功能吗?也许是实例化子类实例的某种方法?有什么想法吗?谢谢。它已经被保护了,但仍然是空的。请参阅原帖中的编辑。