📄 ex4.java
字号:
// interfaces/Ex4.java
// TIJ4 Chapter Interfaces, Exercise 4, page 315
/* Create an abstract class with no methods. Derive a class and add a method.
* Create a static method that takes a reference to the base class, downcasts
* it to the derived class, and calls the method. In main(), demonstrate that it
* works. Now put the abstract declaration for the method in the base class, thus
* eliminating the need for the downcast.
*/
import static org.greggordon.tools.Print.*;
abstract class Dad {
}
class Son extends Dad {
protected void print() { println("Son"); }
}
abstract class SecondDad {
abstract protected void print();
}
class SecondSon extends SecondDad {
protected void print() { println("SecondSon"); }
}
public class Ex4 {
public static void testPrint(Dad d) {
((Son)d).print();
}
public static void secondTestPrint(SecondDad sd) {
sd.print();
}
public static void main(String[] args) {
Son s = new Son();
Ex4.testPrint(s);
SecondSon ss = new SecondSon();
Ex4.secondTestPrint(ss);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -