再次分清访问权限
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