day09-继承中成员变量的访问特点
This commit is contained in:
19
day09/src/com/inmind/extends_member02/Demo02.java
Normal file
19
day09/src/com/inmind/extends_member02/Demo02.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.inmind.extends_member02;
|
||||
/*
|
||||
.继承中成员变量的访问特点
|
||||
1.直接通过对象去访问,看对象创建语句=左边的内容,是谁就优先使用谁的成员变量,如果没有则向上找
|
||||
2.间接通过对象调用成员方法去访问,调用时,方法属于谁,就优先使用谁的成员变量,如果没有则向上找
|
||||
*/
|
||||
public class Demo02 {
|
||||
public static void main(String[] args) {
|
||||
//直接创建子类对象
|
||||
Zi zi = new Zi();
|
||||
System.out.println(zi.numFu);//200
|
||||
System.out.println(zi.numZi);//100
|
||||
System.out.println(zi.num);//101
|
||||
System.out.println("----------");
|
||||
zi.methodZi();
|
||||
System.out.println("----------");
|
||||
zi.methodFu();
|
||||
}
|
||||
}
|
||||
10
day09/src/com/inmind/extends_member02/Fu.java
Normal file
10
day09/src/com/inmind/extends_member02/Fu.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.inmind.extends_member02;
|
||||
//父类
|
||||
public class Fu {
|
||||
int numFu = 200;
|
||||
int num = 201;
|
||||
public void methodFu(){
|
||||
System.out.println(numFu);
|
||||
System.out.println(num);
|
||||
}
|
||||
}
|
||||
12
day09/src/com/inmind/extends_member02/Zi.java
Normal file
12
day09/src/com/inmind/extends_member02/Zi.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.inmind.extends_member02;
|
||||
//子类
|
||||
public class Zi extends Fu {
|
||||
int numZi = 100;
|
||||
int num = 101;
|
||||
|
||||
public void methodZi(){
|
||||
System.out.println(numFu);
|
||||
System.out.println(numZi);
|
||||
System.out.println(num);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user