简体   繁体   English

受保护的修饰符是什么意思?

[英]What does the protected modifier mean?

I am reading the book The Java Programming Language, 3rd edition .我正在阅读The Java Programming Language, 3rd edition 一书

In chapter 3.5 , it illustrates the protected modifier with the following words:在第 3.5 章中,它用以下词说明了protected修饰符:

More precisely, beyond being accessible within the class itself and to code within the same package, a protected member can also be accessed from a class through object references that are of at least the same type as the class that is, references of the class's type or one its subtypes.更准确地说,除了可以在类本身内访问以及在同一包中进行编码之外,还可以通过与类至少具有相同类型的对象引用(即类类型的引用)从类访问受保护的成员或其亚型之一。

The words makes me confused, in two aspects:这些话让我感到困惑,在两个方面:

1. protected member can be accessed by code within the same package ? 1.受保护的成员可以通过同一包内的代码访问吗? What I knew before is protected member can only be accessed by the subclass...我之前知道的是受保护的成员只能被子类访问......

2. I don't understand what does a protected member can also be accessed from ... mean, anyone can explain to me please? 2.我不明白什么a protected member can also be accessed from ...意思是,任何人都可以向我解释一下吗?

  1. Yes, protected members can be accessed from the class itself, subclasses of the class and also all classes in the same package of the class (doesn't matter if those are subclasses or not).是的,可以从类本身、类的子类以及类的同一个包中的所有类访问protected成员(这些类是否为子类无关紧要)。 If you didn't know that last part before, then you've just learned something new.如果你之前不知道最后一部分,那么你刚刚学到了一些新东西。

  2. It simply means that you can use those members;这只是意味着您可以使用这些成员; if a member is not accessible, it means you'll get a compiler error when you try to use it.如果某个成员不可访问,则意味着您在尝试使用它时会遇到编译器错误。

In Java, protected means that the member can be accessed by any class in the same package and by subclasses even if they are in another packages.在 Java 中, protected意味着该成员可以被同一包中的任何类以及子类访问,即使它们在另一个包中。

Note笔记

A protected variable is not visible outside the package

for example B extends A and A has a protected int x;例如 B 扩展 A 并且 A 有一个受保护的 int x; it can be use within the class B. But cannot be access using its instance variable它可以在类 B 中使用。但不能使用其实例变量访问

1) Yes, protected members can be accessed by classes from the same package. 1) 是的,同一个包中的类可以访问受保护的成员。 That's the way Java works.这就是 Java 的工作方式。

2) That means subclasses can access them. 2) 这意味着子类可以访问它们。

I don't understand what does a protected member can also be accessed from ... mean, anyone can explain to me please?我不明白受保护的成员还可以从什么访问……意思是,任何人都可以向我解释一下吗?

For example, you have an object A and an object B, both of the same class.例如,您有一个对象 A 和一个对象 B,它们都属于同一类。 Object A will be able to query the protected properties and methods of object B if it has a reference to it.如果对象 A 有对它的引用,则对象 A 将能够查询对象 B 的受保护属性和方法。 The protected modifier is enforced at class level, not at object level. protected 修饰符在类级别强制执行,而不是在对象级别。 This can come in handy in some situations.这在某些情况下会派上用场。

Here are the answers以下是答案

  1. Yes.是的。 Protected members (instance variables and methods) of a class can be accessed by other classes within the same package as well as by any other class that extends this class containing the member to be accessed.一个类的受保护成员(实例变量和方法)可以被同一包内的其他类访问,也可以被任何其他扩展包含要访问的成员的类的类访问。 In the same specification, they have also given the table where the access level is strictly increasing providing all the accesses allowed in the preceding level: private -> package -> protected -> public在同一个规范中,他们还给出了访问级别严格递增的表,提供了前一级别允许的所有访问:private -> package -> protected -> public

  2. As protected members (instance variables / states and methods / behaviors) of a class X are inherited and visible as part of the sub classes of X, say Y1, Y2, Y3 and may be further down to the next levels, any object references of type X or Y1, Y2, y3 can be used to access that protected member.由于类 X 的受保护成员(实例变量/状态和方法/行为)作为 X 的子类的一部分被继承和可见,比如 Y1、Y2、Y3 并且可能进一步向下到下一级,任何对象引用类型 X 或 Y1、Y2、y3 可用于访问该受保护成员。

Just think of it as between public and private.只需将其视为介于公共和私人之间。 You can access everything from public classes, and less from private classes.您可以从公共课程访问所有内容,而从私人课程中访问更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM