java - 使用基类实例在派生类中访问受保护的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3148600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java - protected members accessed in derived class using base class instance
提问by Tarun
I created instance of base class in derived class and tried to access protected members.
我在派生类中创建了基类的实例并尝试访问受保护的成员。
I can directly access protected members in a derived class without instantiating the base class.
我可以直接访问派生类中的受保护成员,而无需实例化基类。
Base class:
基类:
package com.core;
public class MyCollection {
protected Integer intg;
}
A derived class in the same package -
同一个包中的派生类 -
package com.core;
public class MyCollection3 extends MyCollection {
public void test(){
MyCollection mc = new MyCollection();
mc.intg=1; // Works
}
}
A derived class in a different package -
不同包中的派生类 -
package secondary;
import com.core.MyCollection;
public class MyCollection2 extends MyCollection{
public void test(){
MyCollection mc = new MyCollection();
mc.intg = 1; //!!! compile time error - change visibility of "intg" to protected
}
}
How it is possible to access a protected member of a base class in a derived class using instance of base class when derived class is also in same package but not when derived class is in different package?
当派生类也在同一个包中但派生类在不同的包中时,如何使用基类的实例访问派生类中基类的受保护成员?
If I mark protected member as "static" then I am able to access protected member of base class using instance of base class in a derived class which resides in a different package.
如果我将受保护成员标记为“静态”,那么我就可以使用位于不同包中的派生类中的基类实例访问基类的受保护成员。
回答by Andrzej Doyle
You're right that you can't do this. The reason why you can't access the field, is that you're not in the same package as the class, nor are you accessing an inherited member of the same class.
你是对的,你不能这样做。无法访问该字段的原因是,您与该类不在同一个包中,也没有访问同一类的继承成员。
The last point is the critical one - if you'd written
最后一点是关键的一点——如果你写了
MyCollection2 mc = new MyCollection2();
mc.intg = 1;
then this would work, as you're changing a protected member of your ownclass (which is present in that class through inheritance). However, in your case you're trying to change a protected member of a differentclass in a different package. Thus it should come as no surprise that you're denied access.
那么这会起作用,因为您正在更改您自己的类的受保护成员(通过继承存在于该类中)。但是,在您的情况下,您正在尝试更改不同包中不同类的受保护成员。因此,您被拒绝访问也就不足为奇了。
回答by Ram Patra
If a class member is protectedthen there are 2 cases:
如果是类成员,protected则有两种情况:
- If subclass is in same package
- If subclass is in different package
I. Same package :
- Can access through inheritance
- Can access by creating an instance of parent class
II. Different package :
- Can onlyaccess through inheritance
- 如果子类在同一个包中
- 如果子类在不同的包中
I. 同包:
- 可以通过继承访问
- 可以通过创建父类的实例来访问
II。不同的包:
-只能通过继承访问
See the table below for all use cases:
有关所有用例,请参阅下表:


Source: SCJP book.
资料来源:SCJP 书籍。
回答by Bozho
The Java tutorialsays:
在Java教程说:
The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
protected 修饰符指定该成员只能在它自己的包中访问(如包私有),此外,它的类的子类在另一个包中可以访问。
And in your case, you are accessing the variable in another object. By coincidence it has a class that's the same as the current one, but the visibility checks wouldn't check that.
在您的情况下,您正在访问另一个对象中的变量。巧合的是,它有一个与当前类相同的类,但可见性检查不会检查它。
So, the second time you are denied access, because you are in a different package, and the first time you are given access because you are in the same package (and not because it's a subclass)
因此,您第二次被拒绝访问,因为您在不同的包中,而第一次被授予访问权限是因为您在同一个包中(而不是因为它是子类)
回答by Chris
In short, it's not really possible. It seems like you should reconsider your design.
简而言之,这不太可能。看来您应该重新考虑您的设计。
However, there's a work around, if you're sure that's what you want to do. You can add a protected methodto MyCollectionwhich takes an instance and sets the value of intgon your behalf:
但是,如果您确定这就是您想要做的,则有一种变通方法。您可以添加一个受保护的方法,MyCollection该方法接受一个实例并intg代表您设置 的值:
package com.core;
public class MyCollection {
protected Integer intg;
protected void setIntg(MyCollection collection, Integer newIntg) {
collection.intg = newIntg;
}
}
Now your subclasses can access this method:
现在您的子类可以访问此方法:
package secondary;
import com.core.MyCollection;
public class MyCollection2 extends MyCollection{
public void test(){
MyCollection mc = new MyCollection();
setIntg(mc, 1);
}
}
But please note that this is a verystrange way of doing it. I'd suggest again that your design needs to be rethought before you go down this route.
但请注意,这是一种非常奇怪的做法。我再次建议您在走这条路之前需要重新考虑您的设计。
回答by hsabbagh
You cant access a protected variable in a derived class (which is in different package) if accessed using a new object of class MyCollection. you can just write intg = 1; directly without making ( new MyCollection ) like this:
如果使用 MyCollection 类的新对象访问,则无法访问派生类(位于不同包中)中的受保护变量。你可以只写 intg = 1; 直接不制作 ( new MyCollection ) 像这样:
package secondary;
import com.core.MyCollection;
public class MyCollection2 extends MyCollection{
public void test(){
intg = 1;
}
}
回答by User 1034
According to the member accessibility rule of Java you cannot access protected member of a class without extending it.
根据 Java 的成员可访问性规则,您不能在不扩展类的情况下访问受保护的成员。
You can try the following.
您可以尝试以下操作。
package secondary;
import com.core.MyCollection;
public class MyCollection2 extends MyCollection{
public void test(){
intg = 1;
}
}
Instead of creating the new instance try to assign the value. It will work.
尝试分配值而不是创建新实例。它会起作用。

