Java中的受保护的访问修饰符 [英] Protected access modifier in Java
问题描述
我在理解java中的受保护访问修饰符(或其背后的设计)时遇到了一些麻烦。我认为这意味着包访问和访问通过继承包含抽象成员的类的对象。
I am having a little trouble understanding the protected access modifier in java (or the design behind it). I thought it meant package access and access through objects that inherit the class containing an abstract member.
我写了以下示例代码。我看到如果取消注释,注释掉的行会产生编译错误。为什么我可以通过Second中的Second对象访问pro,但不能通过Second中的First对象访问pro?
I wrote the following sample code. I see that the commented out line produces a compilation error if uncommented. Why can I access pro through a Second object in Second but not through a First object in Second?
package first;
public class First {
protected void pro(){
System.out.println("Can see protected method");
}
}
package first;
public class InFirst {
public static void main(String[] args){
First fst = new First();
fst.pro();
}
}
package second;
import first.First;
public class Second extends First {
public static void main(String[] args){
First fst = new First();
// fst.pro();
Second sec = new Second();
sec.pro();
}
}
推荐答案
网页 @MadProgrammer链接给出了一个不错的解释:
The webpage @MadProgrammer linked gives a decent explanation:
受保护的修饰符指定只能在其自己的包中访问该成员
(与package-private一样),此外,另一个包中
a类的子类。
"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."
这意味着受保护的成员必须直接通过类来访问它在所述类的子类或子类中定义,同时也在适当的包内。它并不一定意味着您可以通过在所述类的子类中创建的所述类的实例来访问受保护的成员。重点是所涉及的包裹。
This means the protected member must be accessed directly through either the class it is defined in or a subclass of said class while also being within the appropriate package. It does not necessarily mean you can access the protected member through an instance of said class created within a subclass of said class. The focus is on the packages involved.
package first; // Current package
First fst = new First(); // from package first and does not extend anything
fst.pro();
尝试从哪个包中访问有问题的成员? 第一个
Attempting to access member in question from which package? first
(sub)类是否包含所述成员或其父类,它继承了该成员,在其中定义相同的包裹? 是,第一个
首先在包中定义
,所以 protected
会员可以从首先在
包中访问
。
Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes, First
is defined in package first
, so the protected
member is accessible from First
in package first
.
package second; // Current package
First fst = new First(); // from package first and does not extend anything
fst.pro();
尝试从哪个包中访问有问题的成员? 第二
Attempting to access member in question from which package? second
是(子)类,它包含所述成员或其父类,它继承了该成员,在其中定义相同的包裹? 否,第一个
首先在包中定义
,所以 protected
使第一
第二个包
无法访问该成员。
Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? No, First
is defined in package first
, so protected
makes the member inaccessible from First
in package second
.
package second; // Current package
Second sec = new Second(); // from package second and extends First from package first
sec.pro();
尝试从哪个包中访问有问题的成员? 第二
Attempting to access member in question from which package? second
是(子)类,它包含所述成员或其父类,它继承了该成员,在其中定义相同的包裹? 是,第二个
,在包第二个
中定义,从继承成员首先
,所以受保护的
成员可以从中的
。 第二
进行访问第二个包
Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes, Second
, which is defined in package second
, inherits the member from First
, so the protected
member is accessible from Second
in package second
.
package first; // Current package
Second sec = new Second(); // from package second and extends First from package first
sec.pro();
尝试从哪个包中访问有问题的成员? 第一个
Attempting to access member in question from which package? first
(sub)类是否包含所述成员或其父类,它继承了该成员,在其中定义相同的包裹? 是,第二个
从 First
继承成员,该成员首先在包中定义
,因此可以从包中的
。 Second
访问 protected
成员第一个
Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes, Second
inherits the member from First
, which is defined in package first
, so the protected
member is accessible from Second
in package first
.
package first; // Current package
Third third = new Third(); // from package third and extends Second from package second,
// which extends First from package first
third.pro();
尝试从哪个包中访问有问题的成员? 第一个
Attempting to access member in question from which package? first
(sub)类是否包含所述成员或其父类,它继承了该成员,在其中定义相同的包裹? 是,第三个
从第二个
继承该成员,该成员从继承
package first
),因此 protected
成员可从第三次
首先在包中
。
Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes, Third
inherits the member from Second
, which inherits it from First
where the member is defined (package first
), so the protected
member is accessible from Third
in package first
.
这篇关于Java中的受保护的访问修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!