Java向上转型,向下转型

向上转型Parent parent=new Son();

主要作用:参数统一

向下转型(Son)parent.SonMethod();

主要作用:调用子类的特有方法

public class Parent {
    private static final String TAG = "Parent";
    public void ParentMethod(){
        Log.i(TAG, "ParentMethod: ");
    }
}

public class Son extends Parent {
    private static final String TAG = "Son";
    public void SonMethod(){
        Log.i(TAG, "SonMethod: ");
    }
}


Parent parent=new Son();
parent.ParentMethod();
((Son) parent).SonMethod();

猜你喜欢

转载自blog.csdn.net/yh18668197127/article/details/85267569