一例看懂封装继承多态

本示例仅从属性及方法角度考虑

没有展示静态代码块、构造代码块、构造函数层面的关系

 

  • 父类:
public class Father {

    //1、非同名属性
    String father_normalField = "father_normalField";

    static String father_staticField = "father_staticField";

    final String father_FINAL_FIELD = "father_FINAL_FIELD";

    static final String father_STATIC_FINAL_FIELD = "father_STATIC_FINAL_FIELD";

    //2、非同名方法
    public String father_publicMeth() {
        return "父类的father_publicMeth方法执行了";
    }

    String father_defaultMeth() {
        return "父类的father_defaultMeth方法执行了";
    }

    protected String father_protectedMeth() {
        return "父类的father_protectedMeth方法执行了";
    }

    private String father_privateMeth() {
        return "父类的father_privateMeth方法执行了";
    }

    //3、同名属性
    String rewrite_normalField = "father_rewrite_normalField";

    static String rewrite_staticField = "father_rewrite_staticField";

    final String rewrite_FINAL_FIELD = "father_rewrite_FINAL_FIELD";

    static final String rewrite_STATIC_FINAL_FIELD = "father_rewrite_STATIC_FINAL_FIELD";

    //4、同名方法
    public String rewrite_publicMeth() {
        return "父类的father_rewrite_publicMeth方法执行了";
    }

    String rewrite_defaultMeth() {
        return "父类的father_rewrite_defaultMeth方法执行了";
    }

    protected String rewrite_protectedMeth() {
        return "父类的father_rewrite_protectedMeth方法执行了";
    }

    private String rewrite_privateMeth() {
        return "父类的father_rewrite_privateMeth方法执行了";
    }
}
  • 子类:
class Son extends Father {

    //1、非同名属性
    String son_normalField = "son_normalField";

    static String son_staticField = "son_staticField";

    final String son_FINAL_FIELD = "son_FINAL_FIELD";

    static final String son_STATIC_FINAL_FIELD = "son_STATIC_FINAL_FIELD";

    //2、非同名方法
    public String son_publicMeth() {
        return "子类的son_publicMeth方法执行了";
    }

    String son_defaultMeth() {
        return "子类的son_defaultMeth方法执行了";
    }

    protected String son_protectedMeth() {
        return "子类的son_protectedMeth方法执行了";
    }

    private String son_privateMeth() {
        return "子类的son_privateMeth方法执行了";
    }

    //3、同名属性
    String rewrite_normalField = "son_rewrite_normalField";

    static String rewrite_staticField = "son_rewrite_staticField";

    final String rewrite_FINAL_FIELD = "son_rewrite_FINAL_FIELD";

    static final String rewrite_STATIC_FINAL_FIELD = "son_rewrite_STATIC_FINAL_FIELD";

    //4、同名方法
    public String rewrite_publicMeth() {
        return "子类的son_rewrite_publicMeth方法执行了";
    }

    String rewrite_defaultMeth() {
        return "子类的son_rewrite_defaultMeth方法执行了";
    }

    protected String rewrite_protectedMeth() {
        return "子类的son_rewrite_protectedMeth方法执行了";
    }

    private String rewrite_privateMeth() {
        return "子类的son_rewrite_privateMeth方法执行了";
    }

}
  • 测试:
class ExtendsTest {
    public static void main(String[] args) {
        Father father = new Father();
        Son son = new Son();

        System.out.println("==============================封装==============================");
        //1.1、父类调用自己的属性
        //非同名
        System.out.println(father.father_normalField);
        System.out.println(father.father_staticField);
        System.out.println(father.father_FINAL_FIELD);
        System.out.println(father.father_STATIC_FINAL_FIELD);
        //同名
        System.out.println(father.rewrite_normalField);
        System.out.println(father.rewrite_staticField);
        System.out.println(father.rewrite_FINAL_FIELD);
        System.out.println(father.rewrite_STATIC_FINAL_FIELD);

        //1.2、父类调用自己的非私有方法
        //非同名
        System.out.println(father.father_publicMeth());
        System.out.println(father.father_defaultMeth());
        System.out.println(father.father_protectedMeth());
        //同名
        System.out.println(father.rewrite_publicMeth());
        System.out.println(father.rewrite_defaultMeth());
        System.out.println(father.rewrite_protectedMeth());

        //2.1、子类调用自己的属性
        //非同名
        System.out.println(son.son_normalField);
        System.out.println(son.son_staticField);
        System.out.println(son.son_FINAL_FIELD);
        System.out.println(son.son_STATIC_FINAL_FIELD);
        //同名
        System.out.println(son.rewrite_normalField);
        System.out.println(son.rewrite_staticField);
        System.out.println(son.rewrite_FINAL_FIELD);
        System.out.println(son.rewrite_STATIC_FINAL_FIELD);

        //2.2、子类调用自己的非私有方法
        //非同名
        System.out.println(son.son_publicMeth());
        System.out.println(son.son_defaultMeth());
        System.out.println(son.son_protectedMeth());
        //同名
        System.out.println(son.rewrite_publicMeth());
        System.out.println(son.rewrite_defaultMeth());
        System.out.println(son.rewrite_protectedMeth());

        System.out.println("==============================继承==============================");
        //1、子类继承父类属性
        //非同名
        System.out.println("子类继承父类:" + son.father_normalField);
        System.out.println("子类继承父类:" + son.father_staticField);
        System.out.println("子类继承父类:" + son.father_FINAL_FIELD);
        System.out.println("子类继承父类:" + son.father_STATIC_FINAL_FIELD);
        //同名
        System.out.println("子类继承父类:" + son.rewrite_normalField);
        System.out.println("子类继承父类:" + son.rewrite_staticField);
        System.out.println("子类继承父类:" + son.rewrite_FINAL_FIELD);
        System.out.println("子类继承父类:" + son.rewrite_STATIC_FINAL_FIELD);

        //2、子类继承父类方法
        //非同名
        System.out.println("子类继承父类:" + son.father_publicMeth());
        System.out.println("子类继承父类:" + son.father_defaultMeth());
        System.out.println("子类继承父类:" + son.father_protectedMeth());
        //同名
        System.out.println("子类继承父类:" + son.rewrite_publicMeth());
        System.out.println("子类继承父类:" + son.rewrite_defaultMeth());
        System.out.println("子类继承父类:" + son.rewrite_protectedMeth());

        System.out.println("==============================多态==============================");
        Father son2Father = son;//向上转型
        System.out.println("--------------------向上转型--------------------");

        //1.1父类
        System.out.println("父类:" + son2Father.father_normalField);
        System.out.println("父类:" + son2Father.father_staticField);
        System.out.println("父类:" + son2Father.father_FINAL_FIELD);
        System.out.println("父类:" + son2Father.father_STATIC_FINAL_FIELD);

        System.out.println("父类:" + son2Father.father_publicMeth());
        System.out.println("父类:" + son2Father.father_defaultMeth());
        System.out.println("父类:" + son2Father.father_protectedMeth());

        //1.2子类 父类类型不能访问子类特有属性

        //1.3重写
        System.out.println("动态绑定:" + son2Father.rewrite_normalField);
        System.out.println("动态绑定:" + son2Father.rewrite_staticField);
        System.out.println("动态绑定:" + son2Father.rewrite_FINAL_FIELD);
        System.out.println("动态绑定:" + son2Father.rewrite_STATIC_FINAL_FIELD);

        System.out.println("重写:" + son2Father.rewrite_publicMeth());
        System.out.println("重写:" + son2Father.rewrite_defaultMeth());
        System.out.println("重写:" + son2Father.rewrite_protectedMeth());

        Son son2Father_2Son = (Son) son2Father;
        System.out.println("--------------------向下转型--------------------");

        //2.1父类
        System.out.println("父类:" + son2Father_2Son.father_normalField);
        System.out.println("父类:" + son2Father_2Son.father_staticField);
        System.out.println("父类:" + son2Father_2Son.father_FINAL_FIELD);
        System.out.println("父类:" + son2Father_2Son.father_STATIC_FINAL_FIELD);

        System.out.println("父类:" + son2Father_2Son.father_publicMeth());
        System.out.println("父类:" + son2Father_2Son.father_defaultMeth());
        System.out.println("父类:" + son2Father_2Son.father_protectedMeth());

        //2.2子类
        System.out.println("子类:" + son2Father_2Son.son_normalField);
        System.out.println("子类:" + son2Father_2Son.son_staticField);
        System.out.println("子类:" + son2Father_2Son.son_FINAL_FIELD);
        System.out.println("子类:" + son2Father_2Son.son_STATIC_FINAL_FIELD);

        System.out.println("子类:" + son2Father_2Son.son_publicMeth());
        System.out.println("子类:" + son2Father_2Son.son_defaultMeth());
        System.out.println("子类:" + son2Father_2Son.son_protectedMeth());

        //2.3重写
        System.out.println("动态绑定:" + son2Father_2Son.rewrite_normalField);
        System.out.println("动态绑定:" + son2Father_2Son.rewrite_staticField);
        System.out.println("动态绑定:" + son2Father_2Son.rewrite_FINAL_FIELD);
        System.out.println("动态绑定:" + son2Father_2Son.rewrite_STATIC_FINAL_FIELD);

        System.out.println("重写:" + son2Father_2Son.rewrite_publicMeth());
        System.out.println("重写:" + son2Father_2Son.rewrite_defaultMeth());
        System.out.println("重写:" + son2Father_2Son.rewrite_protectedMeth());
    }
}
  • 运行结果:

一例看懂封装继承多态_第1张图片

一例看懂封装继承多态_第2张图片

一例看懂封装继承多态_第3张图片

一例看懂封装继承多态_第4张图片

 

你可能感兴趣的:(JavaSE)