访问java类成员的级别

时间:2022-09-02 09:31:32

I realise that this is a very basic question, but it is one which has always bothered me. As I understand things, if you declare a field private in Java then it is not visible outside of that class. If it is protected then it is available to inherited classes and anything in the same package (correct me if either of those definitions is incorrect).

我意识到这是一个非常基本的问题,但它总是困扰着我。据我所知,如果你在Java中声明一个私有字段,那么它在该类之外是不可见的。如果它受到保护,则它可用于继承的类和同一个包中的任何内容(如果这些定义中的任何一个不正确,请更正我)。

Does this mean it is not possible to declare a field that is accessible to only inherited classes and not other non-inherited classes in the same package?

这是否意味着不可能声明只有继承类可访问的字段而不能声明同一个包中的其他非继承类?

I appreciate that there are ways around this, but are there instances when you would want to have this sort of behaviour?

我很欣赏有这种方法可以解决这个问题,但是有没有想要这种行为的情况呢?

Obviously the above question applies to methods as well as fields.

显然,上述问题适用于方法和领域。

Many thanks.

3 个解决方案

#1


See: http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
Package > Subclasses, you can never have a field only visible by subclasses but not by classes from the same package.

请参阅:http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html Package> Subclasses,您永远不能只有子类可见的字段,而不能来自同一个包的类。

#2


Basically:

  • private: Accessible only by the class.
  • 私人:只能由班级访问。

  • public: Accessible by any class.
  • 公共:任何类别都可以访问。

  • protected: Accessible by the class, all inherited classes and the classes of the current package (edited).
  • protected:可由类访问,所有继承的类和当前包的类(已编辑)。

  • no scope defined: Accessible by all classes of the current package.
  • 没有定义范围:当前包的所有类都可以访问。

more information here.

更多信息在这里。

#3


Yes, Java's protected access is a little bit odd in that way. I can't immediately see why it's desirable at all. Personally it doesn't bother me for fields as I don't like non-private fields anyway (other than constants) but the same is true for other members.

是的,Java的受保护访问在这方面有点奇怪。我不能立刻明白为什么它是可取的。就个人而言,我并不打扰我的字段,因为我不喜欢非私有字段(除了常量),但其他成员也是如此。

.NET doesn't have the concept of package/namespace access visibility at all, but it has an alternative which is assembly (think "jar file" - not exactly the same, but close). Frankly I'd like to have namespace and deployment-unit visibility options, but it seems I'm doomed to disappointment...

.NET根本没有包/命名空间访问可见性的概念,但它有一个替代方法就是汇编(想想“jar文件” - 不完全相同,但是关闭)。坦率地说,我想拥有命名空间和部署单元可视性选项,但似乎我注定要失望......

#1


See: http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
Package > Subclasses, you can never have a field only visible by subclasses but not by classes from the same package.

请参阅:http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html Package> Subclasses,您永远不能只有子类可见的字段,而不能来自同一个包的类。

#2


Basically:

  • private: Accessible only by the class.
  • 私人:只能由班级访问。

  • public: Accessible by any class.
  • 公共:任何类别都可以访问。

  • protected: Accessible by the class, all inherited classes and the classes of the current package (edited).
  • protected:可由类访问,所有继承的类和当前包的类(已编辑)。

  • no scope defined: Accessible by all classes of the current package.
  • 没有定义范围:当前包的所有类都可以访问。

more information here.

更多信息在这里。

#3


Yes, Java's protected access is a little bit odd in that way. I can't immediately see why it's desirable at all. Personally it doesn't bother me for fields as I don't like non-private fields anyway (other than constants) but the same is true for other members.

是的,Java的受保护访问在这方面有点奇怪。我不能立刻明白为什么它是可取的。就个人而言,我并不打扰我的字段,因为我不喜欢非私有字段(除了常量),但其他成员也是如此。

.NET doesn't have the concept of package/namespace access visibility at all, but it has an alternative which is assembly (think "jar file" - not exactly the same, but close). Frankly I'd like to have namespace and deployment-unit visibility options, but it seems I'm doomed to disappointment...

.NET根本没有包/命名空间访问可见性的概念,但它有一个替代方法就是汇编(想想“jar文件” - 不完全相同,但是关闭)。坦率地说,我想拥有命名空间和部署单元可视性选项,但似乎我注定要失望......