java——父类对象由子类实例化

2019年2月16日09:33:56 发表评论 1,456 views
/**
 * Created by xabcd on 2019/2/16.
 */
public class Java_shilihua {
    public void fun1() {
        System.out.println("1.Oerson{fun1()}");
    }

    public void fun2() {
        System.out.println("2.Oerson{fun2()}");

    }
}
    //Student类拓展自Person类,也就继承了Person类中的fun1()、fun2()方法
    class Student3 extends Java_shilihua
    {
        //在这里覆写Person类中的fun1()方法
        public void fun1()
        {
            System.out.println("3.Oerson{fun1()}");
        }
        public void fun3()
        {
            System.out.println("4.Oerson{fun3()}");
        }
    }






/**
 * Created by xabcd on 2019/2/16.
 */
public class Testshilihua
{
    public static void main(String[] args)
    {
        //此处,父类对象由子类实例化
        Java_shilihua p = new Student3();
        p.fun1();
        p.fun2();
    }
}






3.Oerson{fun1()}
2.Oerson{fun2()}



所谓的多态性,即子类实例化对象可以转化为父类实例化对象

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: