夜光序言:
你站在桥上看风景,看风景人在楼上看你。我不愿成为那风景,也不会成为那人,我只愿成为,支撑起你的那座桥。
正文:Java的包与访问控制符
访问控制符
package card;
public class X1 { //card包中的类 X1
int x,y;
public X1(int i,int j) {
this.x=i;
this.y=j;
System.out.println("x="+x+" "+"y="+y);
}
public void show() {
System.out.println("this class is a X1!");
}
}
//**************//
package card;
public class X2 { // card包中的类X2
int m,n;
public X2(int i,int j) {
this.m=i;
this.n=j;
System.out.println("m="+m+" "+"n="+n);
}
public void show() {
System.out.println("this class is a X2!");
}
}
//包的使用方式1
import card.X1; // 加载card包中X1类
import card.X2; // 加载card包中X2类
public class Pack1 { //包的使用方式1
public static void main(String args[]) {
X1 aa=new X1(4,5); //不需要再使用包名做前缀
aa.show();
X2 bb=new X2(10,20);
bb.show();
}
}
//包的使用方式2
import card.*; //先加载其它包中所有的类
public class Pack2 { //包的使用方式2
public static void main(String args[]) {
X1 aa=new X1(4,5); // 不需要再使用包名做前缀
aa.show();
X2 bb=new X2(10,20);
bb.show();
}
}
//包的使用方式3
public class Pack3 { //包的使用方式3
public static void main(String args[]) {
card.X1 aa=new card.X1(4,5); //未预先加载,直接用包名
aa.show();
card.X2 bb=new card.X2(10,20);
bb.show();
}
}