受保护的修饰符是什么意思? [英] What does the protected modifier mean?

查看:356
本文介绍了受保护的修饰符是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Java编程语言第3版这本书。

在第3.5章中,它说明了 protected 修饰符,其中包含以下单词:

In chapter 3.5 , it illustrates the protected modifier with the following words:


更确切地说,除了可以在类本身内访问以及在
相同包中编码之外,还可以通过对象引用
从类访问受保护的成员,这至少是与类相同的类型,类类型的引用或其子类型的

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。受保护的成员可以通过同一个包中的代码访问吗?以前我所知道的是受保护的成员只能被子类访问...

1. protected member can be accessed by code within the same package ? What I knew before is protected member can only be accessed by the subclass...

2。我不明白也可以从...访问受保护的成员意思是,任何人都可以向我解释一下吗?

2. I don't understand what does a protected member can also be accessed from ... mean, anyone can explain to me please?

推荐答案


  1. 是, protected 可以从类本身,类的子类以及所有类中访问成员类的相同包(如果它们是子类,则无关紧要)。如果您以前不知道最后一部分,那么您刚刚学到了新的东西。

  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). If you didn't know that last part before, then you've just learned something new.

这只是意味着您可以使用这些成员;如果某个成员不可访问,则意味着当您尝试使用它时会遇到编译器错误。

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.

这篇关于受保护的修饰符是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆