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、仅仅只有非静态方法才具备多态特性

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

  • 相关阅读:
    Python并发编程-concurrent包
    Python并发编程-多进程
    Python并发编程-GIL全局解释器锁
    .net解析csv(C#导表工具)
    为游戏适配刘海屏
    Lua rawget rawset newindex 函数定义和例子
    lua_local变量在new时不会被清空
    Lua查找表元素过程(元表、__index方法是如何工作的)
    为什么不使用github的wiki而是使用mkdocs做文档管理?
    Unity重置Animator到初始状态和重复播放同一个Animation
  • 原文地址:https://www.cnblogs.com/rolly-yan/p/4009919.html
Copyright © 2011-2022 走看看