I think in this case there is no need to declare a public constructor since the class is not accessible outside the package anyway. But is there some hidden impact when the class has only package private constructor?
我认为在这种情况下,不需要声明公共构造函数,因为无论如何都不能在包外部访问类。但是当类只有包私有构造函数时,是否存在一些隐藏的影响?
4 个解决方案
#1
23
No, you don't have to declare the public constructor; package private constructors will be just as usable. Classes outside the package wouldn't be able to use the constructor anyway, since they can't see the class.
不,您不必声明公共构造函数;包私有构造函数将同样可用。包之外的类无论如何都无法使用构造函数,因为它们无法看到类。
#2
4
If your class is package private then the access levels indicated by the modifier keyword public
together with the default package private access level of the constructor are equivalent.
如果您的类是包私有,则修饰符关键字public指示的访问级别与构造函数的默认包私有访问级别是等效的。
You can however indicate the behavior you intent the method to have in case the class visibility is changed during development. This may happen when you open some APIs which were previously internal. In that case it looks more conservative to declare the constructor as package private since you do not open all doors at the same time.
但是,如果在开发期间更改了类可见性,则可以指示您希望方法具有的行为。当您打开以前内部的一些API时,可能会发生这种情况。在这种情况下,将构造函数声明为包私有看起来更保守,因为您不能同时打开所有门。
#3
-3
we cant made a constructor public in a private class because private class cant be accessed outside and none of its methods can be accessed and as constructors are special members functions so it can also not be accessed from outside so it cant be declared public because it ultimately violates the private definition of class
我们不能在私有类中创建一个构造函数public,因为私有类不能在外部访问,并且它的方法都不能被访问,并且构造函数是特殊的成员函数,因此它也不能从外部访问,所以它不能被公开,因为它最终违反了阶级的私人定义
#4
-3
Constructors cannot be defined in the private part of the class. The compiler will not allow you to do so since members defined as private are not accessible by the main program. Whenever we call it is only accessible by the class in which we are defining it or friend label. The compiler will give the constructor name and it is not accessible.
无法在类的私有部分中定义构造函数。编译器不允许您这样做,因为主程序无法访问定义为私有的成员。每当我们调用它时,只能由我们定义它的类或朋友标签访问它。编译器将提供构造函数名称,并且无法访问它。
#1
23
No, you don't have to declare the public constructor; package private constructors will be just as usable. Classes outside the package wouldn't be able to use the constructor anyway, since they can't see the class.
不,您不必声明公共构造函数;包私有构造函数将同样可用。包之外的类无论如何都无法使用构造函数,因为它们无法看到类。
#2
4
If your class is package private then the access levels indicated by the modifier keyword public
together with the default package private access level of the constructor are equivalent.
如果您的类是包私有,则修饰符关键字public指示的访问级别与构造函数的默认包私有访问级别是等效的。
You can however indicate the behavior you intent the method to have in case the class visibility is changed during development. This may happen when you open some APIs which were previously internal. In that case it looks more conservative to declare the constructor as package private since you do not open all doors at the same time.
但是,如果在开发期间更改了类可见性,则可以指示您希望方法具有的行为。当您打开以前内部的一些API时,可能会发生这种情况。在这种情况下,将构造函数声明为包私有看起来更保守,因为您不能同时打开所有门。
#3
-3
we cant made a constructor public in a private class because private class cant be accessed outside and none of its methods can be accessed and as constructors are special members functions so it can also not be accessed from outside so it cant be declared public because it ultimately violates the private definition of class
我们不能在私有类中创建一个构造函数public,因为私有类不能在外部访问,并且它的方法都不能被访问,并且构造函数是特殊的成员函数,因此它也不能从外部访问,所以它不能被公开,因为它最终违反了阶级的私人定义
#4
-3
Constructors cannot be defined in the private part of the class. The compiler will not allow you to do so since members defined as private are not accessible by the main program. Whenever we call it is only accessible by the class in which we are defining it or friend label. The compiler will give the constructor name and it is not accessible.
无法在类的私有部分中定义构造函数。编译器不允许您这样做,因为主程序无法访问定义为私有的成员。每当我们调用它时,只能由我们定义它的类或朋友标签访问它。编译器将提供构造函数名称,并且无法访问它。