- class Fu {
- public void show() {
- System.out.println("fu show");
- }
- }
- class Zi extends Fu { //继承
- public void show() {
- System.out.println("zi show");//重写
- }
- public void method() { //子类特有
- System.out.println("zi method");
- }
- }
- class Test1Demo {
- public static void main(String[] args) {
- //Fu f = new Zi(); //父类引用指向子类对象
- //f.method(); //报错,父类不能直接使用子类特有方法
- //需要向下强转.才能使用子类属性或方法
- Zi a = new Zi();
- Fu f = (Fu)a;
- //f.method();
- f.show(); //编译,看父类有没有show()方法,有则子类show()方法进栈.
- }
- }
复制代码 |
|