zoukankan      html  css  js  c++  java
  • JAVA类的继承之多态特性

      父类可以接收子类的实例,方法的覆盖,属性的隐藏,这些都使我非常疑惑,今天有点时间记录之。

      话不多说,直接上代码上结果

    1.

    public class TestDto{
        public static void main(String[] args) {
            ClassInitializeChild child=new ClassInitializeChild();
            System.out.println(child.name);
        }
    }
    class ClassInitializeParent{
        public String name="parent";
        public void print(){
            System.out.println("parent="+name);
        }
        public ClassInitializeParent(){
            print();
        }
    }
    class ClassInitializeChild extends ClassInitializeParent{
        public String name="child";
        public void print(){
            System.out.println("child="+name);
        }
        public ClassInitializeChild(){
            print();
        }
    }

      结果:

      

    child=null
    child=child
    child

    2.

    public class TestDto{
        public static void main(String[] args) {
            ClassInitializeParent parent=new ClassInitializeChild();
            System.out.println(parent.name);
        }
    }
    class ClassInitializeParent{
        public String name="parent";
        public void print(){
            System.out.println("parent="+name);
        }
        public ClassInitializeParent(){
            print();
        }
    }
    class ClassInitializeChild extends ClassInitializeParent{
        public String name="child";
        public void print(){
            System.out.println("child="+name);
        }
        public ClassInitializeChild(){
            print();
        }
    }

      结果:

      

    child=null
    child=child
    parent

    3.

    public class TestDto{
        public static void main(String[] args) {
            ClassInitializeParent parent=new ClassInitializeChild();
            parent.print();
        }
    }
    class ClassInitializeParent{
        public String name="parent";
        public void print(){
            System.out.println("parent="+name);
        }
    }
    class ClassInitializeChild extends ClassInitializeParent{
        public String name="child";
        public void print(){
            System.out.println("child="+name);
        }
    }

      结果:

      

    child=child

    4.

    public class TestDto{
        public static void main(String[] args) {
            ClassInitializeParent parent=new ClassInitializeParent();
            parent.print();
        }
    }
    class ClassInitializeParent{
        public String name="parent";
        public void print(){
            System.out.println("parent="+name);
        }
    }
    class ClassInitializeChild extends ClassInitializeParent{
        public String name="child";
        public void print(){
            System.out.println("child="+name);
        }
    }

      结果:

    parent=parent
  • 相关阅读:
    CSRF小结
    代码注入小结
    文件上传漏洞小结
    解决Burpsuite_pro_v1.6破解版https证书导入问题
    Java HTTP 组件库选型看这篇就够了
    趣图:我正在演示一个功能,但没有达到预期效果
    阅读源码的利器——Intellij-IDEA-Replace-in-Path-使用技巧
    分享一些好用的 Chrome 插件!
    趣图:程序员发量的变化过程
    Spring循环依赖的三种方式
  • 原文地址:https://www.cnblogs.com/wuyouwei/p/6274190.html
Copyright © 2011-2022 走看看