源于论坛里提出此问题,我看了半个多小时,都没弄明白是什么原因,在ITeye上找到了答案!内容比较长,细心看!
Java中的访问控制修饰符有四个级别,但属protected最复杂。如果问大家,protected修饰符作用,大家会说“在子类与同包中可以访问这个成员”,当然在自己类中也是可的,但你真的理解了吗?不信,先看看下面这些实例,看你是否也是这样想的(其中注释掉的是不能编译的,大家最好把这些代码放在eclipse中再看,不然你会晕的^_^):
package pk1.a;
public class Base {
protected int i = 1;
protected void protect() {
System.out.println("Base::protect");
}
}
package pk1.a;
import pk1.b.Sub;
public class SubSub extends Sub {
void g() {
Sub s = new SubSub();
//!! s.protect();//规则2.c.i
System.out.println(s.i);//规则2.c.ii
}
}
package pk1.b;
import pk1.a.Base;
public class Sub extends Base {
private void prt() {}
protected void protect() {
System.out.println("Base::protect");
}
void f() {
//规则2.a
this.protect();
this.i = 2;
//规则2.b
Base a2 = new Sub();
//!! a2.protect();
//!! System.out.println(a2.i);
//规则1
Sub b = new Sub();
b.protect();
b.i = 1;
b.prt();
}
}
package pk1.b;
public class SubSub extends Sub {
void g() {
Sub s = new SubSub();
s.protect();//规则2.c.i
//!! System.out.println(s.i);//规则2.c.ii
}
}
package pk1.c;
import pk1.a.Base;
import pk1.b.Sub;
public class SubSub extends Sub {
void g() {
this.protect();//规则2.a
//规则2.b
Base b = new SubSub();
//!! b.protect();
//!! System.out.println(b.i);
//规则2.b
Sub s = new SubSub();
//!! s.protect();
//!! System.out.println(s.i);
}
}
Java代码
package pk2.a;
public class Base {
protected int i = 1;
protected void protect() {
System.out.println("Base::protect");
}
}
package pk2.a;
import pk2.b.Sub;
public class Other {
void g() {
//规则3.a
Base b = new Sub();
b.protect();
System.out.println(b.i);
//规则3.b.ii
Sub s = new Sub();
s.protect();
System.out.println(s.i);
}
}
package pk2.b;
import pk2.a.Base;
public class Other {
void g() {
//规则3.a
Base b = new Sub();
//!! b.protect();
//!! System.out.println(b.i);
//规则3.b.ii
Sub s = new Sub();
//!! s.protect();
//!! System.out.println(s.i);
}
}
package pk2.b;
import pk2.a.Base;
public class Sub extends Base {}
Java代码
package pk3.a;
import pk3.b.Sub;
public class Base {
protected int i = 1;
protected void protect() {
System.out.println("Base::protect");
}
static protected int i_s = 1;
static protected void protect_s() {
System.out.println("Static:Base::protect");
}
void f() {
//!! Sub.i_s = 2; //规则3.b.i
Sub.protect_s();//规则3.b.ii
}
}
package pk3.a;
import pk3.b.Sub;
public class Other {
void g() {
Sub s = new Sub();
//!! s.protect();//规则3.b.i
System.out.println(s.i);//规则3.b.ii
}
void f() {
//!! Sub.i_s = 2; //规则3.b.i
Sub.protect_s();//规则3.b.ii
Base.i_s = 2;//规则3.a
Base.protect_s();//规则3.a
}
}
package pk3.b;
import pk3.a.Base;
public class Other {
void f() {
Sub.i_s = 2;//规则3.b.i
//!! Sub.protect1();//规则3.b.ii
//!! Base.i1 = 2;//规则3.a
//!! Base.protect1();//规则3.a
}
}
package pk3.b;
import pk3.a.Base;
public class Sub extends Base {
protected void protect() {
System.out.println("Base::protect");
}
static protected int i_s = 2;
void f() {
/*
* 在子类中可能通过子类类型或父类类型来来访问父类中protected静态
* 成员,而不管子类与父类是否在同一包中,或是子类重新定义了这些成员
*
* 注,在父类或子类中访问时后面的规则不再适用
*/
System.out.println(Sub.i_s);//2
Sub.protect_s();
System.out.println(Base.i_s);//1
Base.protect_s();
}
}
|