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

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

  • 相关阅读:
    (dp)LeetCode Weekly Contest 34 -Non-negative Integers without Consecutive Ones
    (后缀数组)poj 3581 Sequence
    (最小生成树)Codeforces 76 A Gift
    (最小生成树)Codeforces Educational Codeforces Round 9 Magic Matrix
    (AC自动机)UVALive
    (trie)BUAAOJ 371
    CCF 201312-3 最大的矩形
    http错误代码含义
    数据结构-5-二叉树的实现以及递归遍历的实现
    数据结构-3.4-栈与队列
  • 原文地址:https://www.cnblogs.com/rolly-yan/p/4009919.html
Copyright © 2020-2023  润新知