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

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

  • 相关阅读:
    浅谈.NET和JAVA的跨平台
    ADO.NET获取TIPTOP存储过程的返回值
    Hide DataGrid Columns via HeaderText
    笑话一则:开车的最高境界
    [推薦]面试时最常问的15问题
    美国小学生守则 VS 中国小学生守则
    Embedded UserControls: Revisited
    SOA认识存误区 详解SOA企业部署的六大关键要素
    Java、.NET,为什么不合二为一?
    [轉]informix语句祥解
  • 原文地址:https://www.cnblogs.com/rolly-yan/p/4009919.html
Copyright © 2011-2022 走看看