📄 transmogrify16.java
字号:
// polymorphism/Transmogrify16.java
// TIJ4 Chapter Polymorphism, Exercise 16, page 306
/* Following the example in Transmogrify.java, create a Starship class
* containing an AlertStatus reference that can indicate three different states.
* Include methods to change the states.
*/
import static org.greggordon.tools.Print.*;
class AlertStatus {
public void alert() {}
}
class NormalAlert extends AlertStatus {
public void alert() { println("AlertStatus Normal"); }
}
class HighAlert extends AlertStatus {
public void alert() { println("AlertStatus High"); }
}
class MaximumAlert extends AlertStatus {
public void alert() { println("AlertStatus Maximum"); }
}
class Starship {
private AlertStatus alertStatus = new NormalAlert();
public void normalAlert() { alertStatus = new NormalAlert(); }
public void highAlert() { alertStatus = new HighAlert(); }
public void maximumAlert() { alertStatus = new MaximumAlert(); }
public void checkAlertStatus() { alertStatus.alert(); }
}
public class Transmogrify16 {
public static void main(String[] args) {
Starship ss = new Starship();
ss.checkAlertStatus();
ss.highAlert();
ss.checkAlertStatus();
ss.maximumAlert();
ss.checkAlertStatus();
ss.normalAlert();
ss.checkAlertStatus();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -