`
直线曲线
  • 浏览: 47066 次
  • 性别: Icon_minigender_1
  • 来自: 河北
社区版块
存档分类
最新评论

再次分清访问权限

    博客分类:
  • java
 
阅读更多
package com.chl.pone;

public class Father {
	private int privateValue;
	int friendlyInt ;
	protected int protectedValue;
	public int publicValue;
}

package com.chl.pone;

public class ClassSamePackage {
	public void publicMethod(){
		Father father = new Father();
		father.friendlyInt = 1;
		father.protectedValue = 1;
		father.publicValue = 1;
	}	
}

package com.chl.pone;

public class ChildSamePackage extends Father{
	public void publicMethod(){
		this.friendlyInt = 1;
		this.protectedValue = 1;
		this.publicValue = 1;
	}
}

package com.chl.ptwo;

import com.chl.pone.Father;

public class ChildNotSamePackage extends Father{
	public void publicMethod(){
		this.protectedValue = 1;
		this.publicValue = 1;
	}
}

package com.chl.ptwo;

import com.chl.pone.Father;

public class ClassNotSamePackage {
	public void publicMethod(){
		Father father = new Father();
		father.publicValue = 1;
	}
}


总结为:

权限                           可以访问

           本类内部  相同包(子类或其他类)  不同包子类     任何地方
private       OK                  
friendly      OK          OK         
protected     OK          OK                   OK
public        OK          OK                   OK           OK


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics