• 父类 子类 调用 考点


    1、实例的属性的值取父类还是子类并不取决于创建对象的类型,而是取决于变量定义的类型。

    例:

    public class Father{

      public String value="Father";

    }

    public class Son extends Father{

      public String value="Son ";

      public static void main(String args[]){

        Father f=new Son();

        System.out.println(f.value);//输出"Father"

      }

    }

    2、在成员函数中访问成员变量时,成员变量的值取父类还是子类取决于是调用父类还是子类的成员函数。

    public class Father{

      public String value="Father";

      public String getValule(){

        return this.value;

      }

      public String getValule2(){

        return this.value;

      }

    }

    public class Son extends Father{

      public String value="Son ";

      public String getValule2(){

        return this.value;

      }

      public static void main(String args[]){

        Son s=new Son();

        System.out.println(s.value);//输出"Son"

        System.out.println(s.getValue());//输出"Father"

        System.out.println(s.getValue2());//输出"Son"

      }

    }

  • 相关阅读:
    道路和航线
    Sorting It All Out
    Sightseeing Cows(0/1分数规划+Spfa判负环)
    【模板】缩点
    间谍网络
    Tarjan算法专练
    数论知识点总结
    博客迁移到博客园
    第一届CCPC河南省赛
    find程序实现
  • 原文地址:https://www.cnblogs.com/children/p/2711614.html
Copyright © 2020-2023  润新知