我有一个抽象类,relation
包装内database.relation
和它的一个子类,Join
,在包中database.operations
. relation
有一个名为的受保护成员mStructure
.
In Join
:
public Join(final Relation relLeft, final Relation relRight) {
super();
mRelLeft = relLeft;
mRelRight = relRight;
mStructure = new LinkedList<Header>();
this.copyStructure(mRelLeft.mStructure);
for (final Header header :mRelRight.mStructure) {
if (!mStructure.contains(header)) {
mStructure.add(header);
}
}
}
On lines
this.copyStructure(mRelLeft.mStructure);
and
for (final Header header : mRelRight.mStructure) {
我收到以下错误:
Relation.mStructure 字段不可见
如果我将这两个类放在同一个包中,则效果非常好。谁能解释一下这个问题吗?
它有效,但只有你孩子们尝试访问它own变量,而不是其他实例的变量(即使它属于同一继承树)。
请参阅此示例代码以更好地理解它:
//in Parent.java
package parentpackage;
public class Parent {
protected String parentVariable = "whatever";// define protected variable
}
// in Children.java
package childenpackage;
import parentpackage.Parent;
class Children extends Parent {
Children(Parent withParent ){
System.out.println( this.parentVariable );// works well.
//System.out.print(withParent.parentVariable);// doesn't work
}
}
如果我们尝试使用withParent.parentVariable
我们有:
Children.java:8: parentVariable has protected access in parentpackage.Parent
System.out.print(withParent.parentVariable);
它是可以访问的,但只能访问它自己的变量。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)