所以我有大约 10-15 个类(随着时间的推移,这个数量可能会增长到更多),并且它们都具有非常相似的变量:
temp
conditions
humidity
load
..诸如此类的东西。我希望实现一个父类(抽象)来更好地管理它,因为它们都是可运行的。
有一部分我为它们每个人调用了一个构造函数,但这......很糟糕。
public ThreadHandler(NH.NHandler NSH, int threadNum){
this.threadNum=threadNum;
this.NSH = NSH;
}
public ThreadHandler(OPA.OpaHandler SgeSH, int threadNum){
this.threadNum=threadNum;
this.OpaSH = OpaSH;
}
public ThreadHandler(SGE.SgeHandler SgeSH, int threadNum){
this.threadNum=threadNum;
this.SgeSH = SgeSH;
}
... 持续 15
我如何实现一个父类来简单地做
public ThreadHandler(objectType name, int threadNum){
//Do stuff
}
感谢您的帮助。
最佳答案
您需要创建一个接口(interface),例如具有通用方法的 IHandler,并且所有处理程序都应实现该接口(interface)
public interface IHandler {
.... declare public methods
}
public NHandler implements IHandler {
.... implement all the methods declared in IHandler..
}
现在您可以在 ThreadHandler 中添加以下内容:
public ThreadHandler(IHandler handler, int threadNum){
.... call the methods
}
关于java - 实现子类的好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10887813/