假设我想装饰一个继承受保护的可观察字段的类。如何访问该受保护变量,以便扩展所述超类的功能?
请参阅下面更具体的示例。
Class SuperSuper - 最初包含匿名观察者的类
package javafxapplication1;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class SuperSuper extends Application {
protected Button button = new Button();
@Override
public void start(Stage stage) throws Exception {
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
doSomething();
}
});
}
public String doSomething() {
return "1";
}
}
Class Super - 继承匿名观察者的超类
package javafxapplication2;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafxapplication1.SuperSuper;
public class Super extends SuperSuper {
@Override
public void start(Stage stage) throws Exception {
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
doSomething();
}
});
}
@Override
public String doSomething() {
return "2";
}
}
Class SuperDecorator - Super 的装饰器类
package javafxapplication3;
import javafxapplication2.Super;
class SuperDecorator extends Super {
Super zuper;
public SuperDecorator(Super zuper){
this.zuper = zuper;
}
@Override
public String doSomething() {
return zuper.doSomething()+"3";
}
//What should I put here in order to make zuper's button print 3?
}