day09--继承的中区分子类方法中重名的三种变量
This commit is contained in:
@@ -18,5 +18,7 @@ public class Demo03 {
|
||||
|
||||
zi.methodZi();
|
||||
zi.methodFu();
|
||||
System.out.println("---------------");
|
||||
zi.method();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
package com.inmind.extends_memberv02;
|
||||
/*
|
||||
区分子类方法中重名的三种变量
|
||||
*/
|
||||
|
||||
public class Zi extends Fu{
|
||||
int numZi = 20;
|
||||
@@ -9,4 +12,12 @@ public class Zi extends Fu{
|
||||
System.out.println(numFu);
|
||||
System.out.println(num);
|
||||
}
|
||||
|
||||
public void method(){
|
||||
int num = 50;//局部变量
|
||||
//注意:在java中访问变量时遇到同名变量有个规律:就近原则
|
||||
System.out.println(num);
|
||||
System.out.println(this.num);//获取本类的成员变量
|
||||
System.out.println(super.num);//获取父类的成员变量
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user