day11-四种权限修饰符
This commit is contained in:
10
day11/src/com/inmind/limit02/Class1.java
Normal file
10
day11/src/com/inmind/limit02/Class1.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package com.inmind.limit02;
|
||||||
|
|
||||||
|
public class Class1 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//System.out.println(LimitDemo04.num1);//同一个包中不同类,私有权限不能访问
|
||||||
|
System.out.println(LimitDemo04.num2);
|
||||||
|
System.out.println(LimitDemo04.num3);
|
||||||
|
System.out.println(LimitDemo04.num4);
|
||||||
|
}
|
||||||
|
}
|
||||||
22
day11/src/com/inmind/limit02/LimitDemo04.java
Normal file
22
day11/src/com/inmind/limit02/LimitDemo04.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.inmind.limit02;
|
||||||
|
/*
|
||||||
|
四种权限修饰符:权限修饰符,可以用来修饰类,方法,成员变量
|
||||||
|
public > protected > (default) > private
|
||||||
|
在同一个类中 YES YES YES YES
|
||||||
|
在同一包中 YES YES YES NO
|
||||||
|
在不同包中(父子类关系) YES YES NO NO
|
||||||
|
在不同包中(!父子类关系) YES NO NO NO
|
||||||
|
*/
|
||||||
|
public class LimitDemo04 {
|
||||||
|
static private int num1 = 1;
|
||||||
|
static int num2 = 1;
|
||||||
|
static protected int num3 = 1;
|
||||||
|
static public int num4 = 1;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(LimitDemo04.num1);
|
||||||
|
System.out.println(LimitDemo04.num2);
|
||||||
|
System.out.println(LimitDemo04.num3);
|
||||||
|
System.out.println(LimitDemo04.num4);
|
||||||
|
}
|
||||||
|
}
|
||||||
10
day11/src/com/inmind/limit02/sub/Class2.java
Normal file
10
day11/src/com/inmind/limit02/sub/Class2.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package com.inmind.limit02.sub;
|
||||||
|
|
||||||
|
import com.inmind.limit02.LimitDemo04;
|
||||||
|
|
||||||
|
public class Class2 extends LimitDemo04 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(LimitDemo04.num3);//受保护的权限,在不同的包中,只有子类能访问
|
||||||
|
System.out.println(LimitDemo04.num4);
|
||||||
|
}
|
||||||
|
}
|
||||||
9
day11/src/com/inmind/limit02/sub/Class3.java
Normal file
9
day11/src/com/inmind/limit02/sub/Class3.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package com.inmind.limit02.sub;
|
||||||
|
|
||||||
|
import com.inmind.limit02.LimitDemo04;
|
||||||
|
|
||||||
|
public class Class3 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(LimitDemo04.num4);//public:不同包,没有任何关系的类,都能访问
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user