java 包私有类中的公共方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5260467/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:16:51  来源:igfitidea点击:

public methods in package-private classes

javamethodspackagevisibilitypublic-method

提问by fredoverflow

Does it make a difference to mark methods as publicin package-private classes?

将方法标记为public包私有类是否有区别?

class SomePackagePrivateClass
{
    void foo();          // package private method

    public void bar();   // public method
}

Is there any practical difference in visibility between fooand barhere?

foobar这里的可见性有什么实际区别吗?

采纳答案by Péter T?r?k

If the class is not going to be extended by another, more visible subclass*, the only difference is clarity of intent. Declaring all methods package private makes it more difficult for future readers to determine which of the methods are meant to be called by other classes in the same package.

如果该类不会被另一个更明显的子类* 扩展,唯一的区别是意图的清晰度。将所有方法声明为包私有会使未来的读者更难确定哪些方法将被同一包中的其他类调用。

*which would not make much sense as a design solution to me, but technically is possible nevertheless.

*这对我来说作为设计解决方案没有多大意义,但从技术上讲仍然是可能的。

回答by Thomas

Example using inheritance:

使用继承的示例:

A.java

一个.java

package pkg1

class A {
  void foo();
  public void bar() {};
}

B.java

B.java

package pkg1

public class B extends A{

}

C.java

C.java

package pkg2

public class C {
  public void doSomething() {
   B b = new B();
   b.bar(); //ok
   b.foo(); //won't work, since foo() is not visible outside of package 'pkg1'

   A a = new A(); //won't work since A is not visible outside of package 'pkg1'
   a.bar(); //won't work, since a cannot be created
  }
}

回答by J?rn Horstmann

Another case where the method has to bepublic is when you are creating a package private implementation of some public class or interface. Since you are not allowed to reduce the visibility of overridden methods, these have to be public.

方法必须是公共的另一种情况是当您创建某个公共类或接口的包私有实现时。由于不允许您降低被覆盖方法的可见性,因此这些方法必须是公开的。

回答by midnite

Well... i had this doubt also (that's why i searched for this thread). This might be a good question.

嗯...我也有这个疑问(这就是我搜索这个线程的原因)。这可能是个好问题。

But ...

但 ...

After a second thought, things are really simpler than we thought.

仔细想想,事情真的比我们想象的要简单。

A package-private method, is a package-private method.

包私有方法,是包私有方法。

Seems nonsense? But ...

好像是废话?但 ...

A package-private method, even if its class is inherited, it is stilla package-private method.

一个包私有方法,即使它的类被继承了,它仍然是一个包私有方法。

Does it make more sense now? For a more verbose explanation:

现在更有意义了吗?更详细的解释:

A package-private method, even if its class is inherited by a more visible subclass, it is stilla package-private method.

If the subclass is of the same package, those package-private methods are also inherited, but they are still package-private.

If the subclass is of the different package (of here, we need the parent class to be public, with some package-private methods), those package-private methods are notinherited (because they are not visible at all).

A point to note, which may be the cause of this doubt, that a package-private method in a public class is not visibleoutside its package also.

一个包私有方法,即使它的类被一个更可见的子类继承,它仍然是一个包私有方法。

如果子类属于同一个包,那些包私有的方法也会被继承,但它们仍然是包私有的

如果子类属于不同的包(在这里,我们需要父类是公共的,有一些包私有的方法),那些包私有的方法不会被继承(因为它们根本不可见)。

需要注意的一点,这可能是造成这种怀疑的原因,公共类中的包私有方法在其包外也不可见



The above explains about the package-private method. And the case of the public-method is just the same.

以上解释了 package-private 方法。和 public-method 的情况是一样的。

When a package-private class is inherited by a pubic subclass (of the same package, this is a must), its public methods are inherited as public methods, and thus they become public methods in a pubic class.

当一个包私有类被一个公共子类(同一个包的,这是必须的)继承时,它的公共方法被继承为公共方法,从而成为公共类中的公共方法。

In the OP example, as foo()and bar()are both in a package-private class, their visibilities are limited to package-private at this moment, until further codes are added (e.g. inheritance).

在 OP 示例中,asfoo()bar()都在包私有类中,此时它们的可见性仅限于包私有,直到添加更多代码(例如继承)。

Hope this is clear (and simple) enough.

希望这足够清楚(和简单)。

P.S. the Java access control table

PS Java访问控制表

回答by blue_note

Yes. publicmethods in a package-private class can be useful (although they are rare). Illustrative example from the java library (and taken from the classic book "effective java"):

是的。public包私有类中的方法可能很有用(尽管它们很少见)。来自 java 库的说明性示例(取自经典书籍“有效的 java”):

Have you ever seen the java.util.JumboEnumSet, java.util.RegularEnumSet?? Probably not, because they are private, and are not mentioned in the documentation.

你见过java.util.JumboEnumSet, java.util.RegularEnumSet吗??可能不是,因为它们是私有的,文档中没有提到。

Have you ever used them?? Probably yes. They are returned by the static method of java.util.EnumSet.noneOf(...). Although you can't construct them yourself, you can use their public methods when they are returned from this method.

你用过吗??大概是。它们由 的静态方法返回java.util.EnumSet.noneOf(...)。虽然你不能自己构造它们,但是当它们从这个方法返回时,你可以使用它们的公共方法。

The good thing about that is that you don't know which one you are using, or even that they exist. Java decides which one is more efficient depending on the arguments, and might use a different on in a newer version. You only access their functionality by referring to them through their common interface (which is a good practice anyway!!)

这样做的好处是你不知道你在使用哪一个,甚至不知道它们存在。Java 根据参数决定哪个更有效,并且可能在较新的版本中使用不同的 on。您只能通过它们的通用接口引用它们来访问它们的功能(无论如何这是一个很好的做法!!)

回答by Tom Hawtin - tackline

It makes very little difference, unless the class is extended by a public (or protected nested) class.

除非该类由公共(或受保护的嵌套)类扩展,否则差别很小。

As a matter of consistency, I'd go for public. It should be rare for methods to be anything other than publicor private.

为了一致性,我会选择public. 方法不是public或 的情况应该很少见private

There are a number of examples of public methods in the package private java.lang.AbstractStringBuilderclass. For instance length()is public in AbstractStringBuilderand is exposed in the public class StringBuilder(overridden in StringBufferto add synchronisation).

包私有java.lang.AbstractStringBuilder类中有许多公共方法的例子。例如length()在公共AbstractStringBuilder和公共类暴露StringBuilder(在重写StringBuffer到附加同步)。

回答by Deepak

foohas default package visibilitywhere as barhas publicvisibility

foodefault package visibility地方为bar具有public可视性