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