zoukankan      html  css  js  c++  java
  • java多态中哪些成员具备多态特性

    在多态的学习中,当子类继承父类时,子类中的变量哪些具备多态特性,哪些不具备多特特性。

    代码:

    class Father{
        public static int x=10;
        public int y=11;
        public Father(){
            System.out.println("Father");
        }
        public static void info(){
            System.out.println("Father's static info method!");
        }
        public void test(){
            System.out.println("Father's  test method!");
        }
    }
    
    class Child extends Father{
        public static int x=20;
        public int y=21;
        public Child(){
            System.out.println("Childer");
        }
        public static void info(){
            System.out.println("Childer's static info method!");
        }
        public void test(){
            System.out.println("Childer's  test method!");
        }
    }
    public class HaHa {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Father father = new Father();       //父类的声明
            System.out.println(father.x);        //父类中的静态成员变量
            System.out.println(father.y);        //父类中的非静态变量
            father.info();                        //父类中的静态方法
            father.test();                       //父类中的非静态方法
            
            Child child = new Child();        //子类的声明
            System.out.println(child.x);
            System.out.println(child.y);
            child.info();
            child.test();
            
            Father fc = new Child();        //将子类的对象赋值给父类的对象
            System.out.println(fc.x);        
            System.out.println(fc.y);
            fc.info();
            fc.test();
        }
    
    }

    运行结果如下图所示:

    结论:

    从上面的结果可以看出:1、对象的中的成员变量(不论是不是静态变量)都不具备多态的特性

               2、对象中的静态方法也不具备多态的特性

               3、仅仅只有非静态方法才具备多态特性

    如有发现遗漏之处,请给我留言!小弟不甚感激!

  • 相关阅读:
    vue使用Echarts图表
    在vue中子组件修改props引发的对js深拷贝和浅拷贝的思考
    团队开发前端VUE项目代码规范
    Vue项目开发最新、最全代码规范文档
    Material Icons 查找的替代办法
    Material icons 全图标一览
    VueCropper 图片裁剪
    分区
    linux 安装图行界面
    Spotlight LGWR1 一直告警
  • 原文地址:https://www.cnblogs.com/rolly-yan/p/4009919.html
Copyright © 2011-2022 走看看