我重构了一些代码,从一个子类抽象为一个辅助类的功能,但我发现我需要帮助类中的超类的方法。在Java中的继承(设计模式)
我想知道是否有一种设计模式可以帮助我做到这一点。或者其他任何建议。
public class Notification(){
public void printMessage(String k, String str){
//Some code
}
}
public class NotificationImpl1 extends Notification(){
ErrorHelper helper = new ErrorHelper();
helper.showMessage("test");
}
public class ErrorHelper(){
public void showMessage(String msg){
// need to call printMessage() here
}
}
在此先感谢。
一个可能的解决方案是声明子类错误帮手NotificationImpl1之外,只是添加扩展通知上课ErrorHelper这将给你访问父的所有功能,或者如果它只是一个小代码量只需复制并粘贴所需的方法即可。 – holycatcrusher