接口父类变量引用指向子类

package com.udppacket;

public class demo1111 {
public static void main(String[] args) {
    Runnable a = new stu2("张三");
    a.run();
    stu2 b =(stu2) a;
    b.getName();
    }
}
class stu {
    String name;
    public stu(String name) {
        super();
        this.name = name;
    }   
}
class stu2 extends stu implements Runnable{

    public stu2(String name) {
        super(name);
    }
public void getName(){
    System.out.println(name);
}
    @Override
    public void run() {
        System.out.println(name);

    }   
}

posted on 2015-05-04 10:50  MrCharles在cnblogs  阅读(68)  评论(0编辑  收藏  举报

导航