诡秘的protected权限

http://837062099.iteye.com/blog/1609340

 

protected修改的属性和方法:

相同:在同一个包中访问的时候和public、default修饰是一样的

不同:在包外访问的时候要遵循一个原则

But a subclass in another package can access the protected members in the super-class via only the references of subclass or its subclasses. A subclass in the same package doesn’t have this restriction. This ensures that classes from other packages are accessing only the members that are part of their inheritance hierarchy.

1、在包外要访问protected修饰的member,那么这个包外要试图去访问protected修饰的member的类一定要是member所在类的子类或者子类的子类...

2、只有通过这个包外类的引用或者其子孙类的引用才能访问到那个protected修饰的member

这个引用包括this.method()

posted on 2014-06-25 17:50  jingyunyb  阅读(178)  评论(0编辑  收藏  举报