Java笔记(十八)……包
概述
对类文件进行分类管理。
给类提供多层命名空间。
写在程序文件的第一行。
类名的全称的是 包名.类名。
包也是一种封装形式。
访问权限
引用《The Complete Reference》中的经典例子来说明Java包中类成员的访问权限问题
下面的例子显示了访问修饰符的所有组合
该例有两个包和五个类
记住这两个不同包中的类需要被存储在以它们的包p1、p2命名的目录下。
第一个包定义了三个类:
Protection Derived 和 SamePackage:
Protection类以合法的保护模式定义了四个int变量
变量n默认权限
变量n_pri是private权限
变量n_pro是protected权限
变量n_pub是public权限。
Derived类作为Protection同一个包中的子类出现。
SamePackage类作为Protection同一个包中的非子类出现。
第二个包定义了两个类:
Protection2 和 OtherPackage。
Protection2类作为Protection的其他包中的子类出现。
OtherPackage类作为Protection的其他包中的非子类出现。
这些类都试图访问Protection中的所有成员,访问受限的语句被注释掉,并加以注明。
下面是Protection.java文件:
1: package p1;
2:
3: public class Protection
4: {
5: int n = 1;
6: private int n_pri = 2;
7: protected int n_pro = 3;
8: public int n_pub = 4;
9:
10: public Protection()
11: {
12: System.out.println("base constructor");
13: System.out.println("n = "+n);
14: System.out.println("n_pri = "+n_pri);
15: System.out.println("n_pro = "+n_pro);
16: System.out.println("n_pub = "+n_pub);
17: }
18: }
下面是Derived.java文件:
1: package p1;
2:
3:
4: class Derived extends Protection
5: {
6: Derived()
7: {
8: System.out.println("derived constructor");
9: System.out.println("n = "+n);
10:
11: //class only
12: //System.out.println("n_pri = "+n_pri);
13:
14: System.out.println("n_pro = "+n_pro);
15: System.out.println("n_pub = "+n_pub);
16: }
17: }
下面是SamePackage.java文件:
1: package p1;
2:
3: class SamePackage
4: {
5: SamePackage()
6: {
7: Protection p = new Protection();
8: System.out.println("same package constructor");
9: System.out.println("n = "+p.n);
10:
11: //class only
12: //System.out.println("n_pri = "+p.n_pri);
13:
14: System.out.println("n_pro = "+p.n_pro);
15: System.out.println("n_pub = "+p.n_pub);
16: }
17: }
下面是Protection2.java文件:
1: package p2;
2:
3: class Protection2 extends p1.Protection
4: {
5: Protection2()
6: {
7: System.out.println("derived other package constructor");
8:
9: //class or package only
10: //System.out.println("n = "+n);
11:
12: //class only
13: //System.out.println("n_pri = "+n_pri);
14:
15: System.out.println("n_pro = "+n_pro);
16: System.out.println("n_pub = "+n_pub);
17: }
18: }
下面是OtherPackage.java文件:
1: package p2;
2:
3: class OtherPackage
4: {
5: OtherPackage()
6: {
7: p1.Protection p = new p1.Protection();
8: System.out.println("other pacage constructor");
9:
10: //class or package only
11: //System.out.println("n = "+p.n);
12:
13: //class only
14: //System.out.println("n_pri = "+p.n_pri);
15:
16: //class,subclass or package only
17: //System.out.println("n_pro = "+p.n_pro);
18:
19: System.out.println("n_pub = "+p.n_pub);
20: }
21: }
下面试试这两个包,通过以下两个测试文件
包p1的测试文件:
1: /*
2: Demo package p1.
3: */
4:
5: package p1;
6:
7:
8: public class Demo
9: {
10: public static void main(String[] args)
11: {
12: Protection ob1 = new Protection();
13: Derived ob2 = new Derived();
14: SamePackage ob3 = new SamePackage();
15: }
16: }
包p2的测试文件:
1: /*
2: Demo package p1.
3: */
4:
5: package p1;
6:
7:
8: public class Demo
9: {
10: public static void main(String[] args)
11: {
12: Protection ob1 = new Protection();
13: Derived ob2 = new Derived();
14: SamePackage ob3 = new SamePackage();
15: }
16: }
编译:
运行Demo1:
运行Demo2:
由此我们可以得到以下结论: