java 为什么不能从构造函数是私有的类继承?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16661595/
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
Why can you not inherit from a class whose constructor is private?
提问by CodeBlue
Why does Java disallow inheritance from a class whose constructor is private?
为什么 Java 不允许从构造函数是私有的类继承?
回答by Peter Lawrey
Java doesn't prevent sub-classing of class with private constructors.
Java 不会阻止使用私有构造函数对类进行子类化。
public class Main {
static class A {
private A() {
System.out.println("Subclassed A in "+getClass().getName());
}
}
static class B extends A {
public B() {
}
}
public static void main(String... ignored) {
new B();
}
}
prints
印刷
Subclassed A in Main$B
What it prevents is sub-classes which cannot access any constructors of its super class. This means a private constructor cannot be used in another class file, and a package local constructor cannot be used in another package.
它阻止的是无法访问其超类的任何构造函数的子类。这意味着私有构造函数不能在另一个类文件中使用,包本地构造函数不能在另一个包中使用。
In this situation, the only option you have is delegation. You need to call a factory method to create an instance of the "super" class and wrap it.
在这种情况下,您唯一的选择就是委派。您需要调用工厂方法来创建“超级”类的实例并对其进行包装。
回答by Luiggi Mendoza
Because a class mustcall its super class constructor always. If the super class constructor can't be accessed, then the sub class can't be initialized.
因为一个类必须始终调用它的超类构造函数。如果无法访问超类构造函数,则无法初始化子类。
More info: JLS 8.8.10. Preventing Instantiation of a Class
更多信息:JLS 8.8.10。防止类的实例化
Regarding Brian Roach's comments:
关于 Brian Roach 的评论:
The call [to the parent class constructor] is only implicit if you don't do it explicitly and the parent has a public or protected no-arg constructor (or hasn't defined any in which case there's a default no-arg). It's required because ... that's how the language works. Children [classes] must call [their] parent's constructor.
[对父类构造函数的调用] 仅在您不显式执行并且父类具有公共或受保护的无参数构造函数(或未定义任何在这种情况下存在默认无参数的情况下)时才是隐式的。它是必需的,因为......这就是语言的工作方式。孩子 [classes] 必须调用 [他们] 父母的构造函数。
Note that when you instantiate anyclass in Java, there's always a implicit call to Object
constructor since it is the super class of all classes. It will execute its default constructor:
请注意,当您在 Java 中实例化任何类时,总会隐式调用Object
构造函数,因为它是所有类的超类。它将执行其默认构造函数:
public Object() {
}
Note from the JLS link:
来自 JLS 链接的注意事项:
It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor(§6.6) that takes no arguments and has no throws clause.
如果隐式声明了默认构造函数,但超类没有可访问的构造函数(第 6.6 节),该构造函数不接受任何参数且没有 throws 子句,则会出现编译时错误。
回答by Lokesh
If constructor of a class is private then child class cannot make call to super constructor. Hence inheritance would fail.
如果类的构造函数是私有的,则子类不能调用超级构造函数。因此继承将失败。
回答by Hari Hara Kumar
If you have a subclass, you have 2 possiblities for child class(subclass) constructors : 1. Default Constructor(No argument constructor) : In this case default constructor will automatically try to call the parent class constructor : this will fail since parent class constructor is private. 2. Parameterized Constructor : When you try to create an object for a child class which has parameterized constructor, you need to mandatorily call parent class constructor from child class constructor by either passing parameters or not passing parameters : this will also fail since parent constructor is private.
如果您有子类,则子类(子类)构造函数有 2 种可能性: 1. 默认构造函数(无参数构造函数):在这种情况下,默认构造函数将自动尝试调用父类构造函数:由于父类构造函数,这将失败是私人的。2. 参数化构造函数:当您尝试为具有参数化构造函数的子类创建对象时,您需要通过传递参数或不传递参数来强制从子类构造函数调用父类构造函数:这也会失败,因为父构造函数是私人的。
Since child class will have either default constructor or parameterized constructor and its not possible to have either of them, you cannot have a subclass for a parent class with private constructor.
由于子类将具有默认构造函数或参数化构造函数,并且不可能拥有它们中的任何一个,因此您不能为具有私有构造函数的父类创建子类。
回答by Sanjaya Liyanage
Yes adding something to Luiggi's answer this feature of java is used when creating the Singleton classes which allows only one instance of that class to be created.
是的,在 Luiggi 的回答中添加了一些东西,在创建单例类时使用了 Java 的这个特性,它只允许创建该类的一个实例。