• Java中变量之局部变量、本类成员变量、父类成员变量的访问方法


    变量:局部变量、本类成员变量、父类成员变量

    如何访问:如果变量名相同,则采用就近原则,哪个变量离所要调用的访问最近,那就么就输出,优先顺序为:局部变量 > 本类成员变量 > 父类成员变量

    如果要跳过局点变量而直接访问本类成员变量或者父类成员变量怎么办?

    1、使用 this 关键字可以跳过局部变量而直接访问本类成员变量;

    2、使用 super 关键字可以跳过局部和本类而直接访问父类成员变量;

     1 package debug;
     2 
     3 class Father{
     4     int num = 10;
     5 }
     6 
     7 class Son1 extends Father{
     8     int num = 20;
     9     int num2 = 30;
    10     
    11     public void show() {
    12         int num = 40;
    13         System.out.println(num);    //采用变量名相同,就近原则,该处输出为 40
    14         System.out.println(this.num);   //本类成员变量访问,该处输出为 20
    15         System.out.println(super.num);    //父类成员变量访问,该处输出为 10
    16         System.out.println(num2);
    17     }
    18 }
    19 
    20 
    21 public class Demo14 {
    22     public static void main(String[] args) {
    23         Son1 s = new Son1();
    24         s.show();
    25     }
    26 
    27 }
  • 相关阅读:
    [uoj418]三角形
    [atARC142F]Paired Wizards
    [loj6746]区间众数
    Can't create component 'xx.xx.xxAppService' as it has dependencies to be satisfied.
    ASP.NET Zero Power Tool 使用报错 Config file not found
    RXJS 5.5以上finally()转变为finalize()
    线程交换数据
    Tomcat系统架构
    工具
    MySQL 执行流程
  • 原文地址:https://www.cnblogs.com/aziji/p/10097375.html
Copyright © 2020-2023  润新知