修饰符 | 类内部 | 同一包内 | 不同包子类 | 同一工程 |
---|
private | yes | | | | default(缺省) | yes | yes | | | protected | yes | yes | yes | | public | yes | yes | yes | yes |
对于class的权限修饰符只可以用public和default(缺省) public类可以在任意地方被访问 default类只可以被同一包内部的类访问
包:com.pan 类:Order.java
package com.pan;
public class Order {
private int orderPrivate;
int orderDefault;
public int orderPublic;
protected int orderProtected;
private void methodPrivate() {
orderPrivate = 1;
orderDefault = 2;
orderProtected = 3;
orderPublic = 4;
}
void methodDefault(){
orderPrivate = 1;
orderDefault = 2;
orderProtected = 3;
orderPublic = 4;
}
protected void methodProtected(){
orderPrivate = 1;
orderDefault = 2;
orderProtected = 3;
orderPublic = 4;
}
public void methodPublic(){
orderPrivate = 1;
orderDefault = 2;
orderProtected = 3;
orderPublic = 4;
}
}
包:com.pan 类:OrderTest.java
package com.pan;
public class OrderTest {
public static void main(String[] args) {
Order order = new Order();
order.orderDefault = 1;
order.orderProtected = 2;
order.orderProtected = 3;
order.methodPublic();
order.methodDefault();
order.methodProtected();
}
}
包:com.hai 类:SubOrder.java
package com.hai;
import com.pan.Order;
public class SubOrder extends Order {
public void method(){
orderPublic = 1;
orderProtected = 2;
methodPublic();
methodProtected();
}
}
包:com.hai 类:OrderTest.java
package com.hai;
import com.pan.Order;
public class OrderTest {
public static void main(String[] args) {
Order order = new Order();
order.orderPublic = 1;
order.methodPublic();
}
}
|