前面一节我们讲了对象,对java的也有了一些了解,今天我们将了解什么叫访问权限
在java中,访问权限主要看修饰符:私有的(private),受保护的(protected),公共的(public),还有一种不用修饰符的访问权限(友好的)。
私有的:private修饰的类或者方法,变量,只能自己类内部使用。私有的吗, 就是完全属于自己,别人是不能弄的,如果类相当于房子,被标明了私有,别人肯定不能去访问它,只有内部可以访问;
友好的:不用修饰符的类或者方法,变量,他的权限稍微比private的大一点,在同一个包的类都可以访问它。包相当于一个大院,大院的房子相当于类,如果一个房子的人很友好,自然欢迎大院的其他人来访问;
受保护的:protected修饰的类或者方法,变量;用protected标记的房子更加友好,不仅同一个大院的人可以访问,而且同一个大院的亲戚也可以访问;
公有的:用public修饰的类或者方法,变量:最友好的就属于它了,所有的人都可以访问,不管你是不是属于同一个大院,或者其他大院的。
将上面讲的总结以下变成以下部分:
权限 同一个类 同一个包 不同包的子类 不同包的非子类
public 可访问 可访问 可访问 可访问
protected 可访问 可访问 可访问
友好的 可访问 可访问
private 可访问
老规矩上代码:
第一个类:
package com.myjava.quanxianA;
import com.myjava.quanxianB.QuanXianASon;
import com.myjava.quanxianB.Test;
/**
* @author zhouxiaoyun
* @date 2012-7-12 下午10:54:57
* @function 测试同一个包下的同一个类的访问
*/
public class QuanXianAFather {
//私有的测试方法
private void playPrivateBySamePachageFather(){
System.out.println("BySamePachageFather私有的测试方法");
}
//友好的测试方法
void playBySamePachageFather(){
System.out.println("BySamePachageFather友好的测试方法");
}
//受保护的测试方法
protected void playProtectedBySamePachageFather(){
System.out.println("BySamePachageFather受保护的测试方法");
}
//公共的测试方法
public void playPublicBySamePachageFather(){
System.out.println("BySamePachageFather公共的测试方法");
}
//以下测试方法最好将所有类写完在测试,注释掉的方法表示不可访问
public static void main(String[] args) {
//同一个包下的同一个类
QuanXianAFather father=new QuanXianAFather();
father.playPrivateBySamePachageFather();
father.playBySamePachageFather();
father.playProtectedBySamePachageFather();
father.playPublicBySamePachageFather();
//同一个包下的不同类,其私有方法不能访问
QuanXianAOther other=new QuanXianAOther();
//other.playPrivateBySamePachageOther();
other.playBySamePachageOther();
other.playProtectedBySamePachageOther();
other.playPublicBySamePachageOther();
//不同包下的子类
QuanXianASon son=new QuanXianASon();
//son.playPrivateByOtherPachageSon();
//son.playByOtherPachageSon();
//son.playProtectedByOtherPachageSon();
son.playPublicByOtherPachageSon();
//通过上面发现只有public可以访问,不同包下的子类其实指的是子类调用父类的方法、
//因为私有的方法或者变量是不能被继承的
//son.playPrivateBySamePachageFather();
//son.playBySamePachageFather();
son.playProtectedBySamePachageFather();
son.playPublicBySamePachageFather();
//不同包下的不同类
Test test=new Test();
//test.playPrivateByOtherPachageOther();
//test.playByOtherPachageOther();
//test.playProtectedByOtherPachageOther();
test.playPublicByOtherPachageOther();
}
}
第二个类
package com.myjava.quanxianA;
/**
* @author zhouxiaoyun
* @date 2012-7-12 下午11:09:53
* @function 同一个包下的另一个类
*/
public class QuanXianAOther {
private void playPrivateBySamePachageOther(){
System.out.println("BySamePachageOther私有的测试方法");
}
//友好的测试方法
void playBySamePachageOther(){
System.out.println("BySamePachageOther友好的测试方法");
}
//受保护的测试方法
protected void playProtectedBySamePachageOther(){
System.out.println("BySamePachageOther受保护的测试方法");
}
//公共的测试方法
public void playPublicBySamePachageOther(){
System.out.println("BySamePachageOther公共的测试方法");
}
}
第三个类:
package com.myjava.quanxianB;
import com.myjava.quanxianA.QuanXianAFather;
/**
* @author zhouxiaoyun
* @date 2012-7-12 下午11:06:00
* @function 其父类不在同一个包内的子类
*/
public class QuanXianASon extends QuanXianAFather{
//私有的测试方法
private void playPrivateByOtherPachageSon(){
System.out.println("ByOtherPachageSon私有的测试方法");
}
//友好的测试方法
void playByOtherPachageSon(){
System.out.println("ByOtherPachageSon友好的测试方法");
}
//受保护的测试方法
protected void playProtectedByOtherPachageSon(){
System.out.println("ByOtherPachageSon受保护的测试方法");
}
//公共的测试方法
public void playPublicByOtherPachageSon(){
System.out.println("ByOtherPachageSon公共的测试方法");
}
}
第四个类:
package com.myjava.quanxianB;
import com.myjava.quanxianA.QuanXianAFather;
/**
* @author zhouxiaoyun
* @date 2012-7-12 下午11:13:14
* @function 不同包的非子类
*/
public class Test {
//私有的测试方法
private void playPrivateByOtherPachageOther(){
System.out.println("ByOtherPachageOther私有的测试方法");
}
//友好的测试方法
void playByOtherPachageOther(){
System.out.println("ByOtherPachageOther友好的测试方法");
}
//受保护的测试方法
protected void playProtectedByOtherPachageOther(){
System.out.println("ByOtherPachageOther受保护的测试方法");
}
//公共的测试方法
public void playPublicByOtherPachageOther(){
System.out.println("ByOtherPachageOther公共的测试方法");
}
}
也许细心的你发现在上面出现了这么两个关键字:extends import
import:用于引入其他的类,在java中java.lang是默认引入的,其他的要显示的引入
extends继承的关键字,即儿子 继承父亲 翻译成java模式:子类 extends 父类
子类可以继承父类的的非私有方法/变量,如果不在同一个包中,只能继承protected与public修饰的方法与变量
ok,今天就讲的这些,明天我们将继续java的其他性质讲解