Java 使用私有构造函数扩展类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3952815/
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
extends of the class with private constructor
提问by user471011
Suppose we have the following code:
假设我们有以下代码:
class Test {
private Test() {
System.out.println("test");
}
}
public class One extends Test {
One() {
System.out.println("One");
}
public static void main(String args[]) {
new One();
}
}
When we create an object One
, that was originally called the parent class constructor Test()
. but as Test()
was private - we get an error.
How much is a good example and a way out of this situation?
当我们创建一个对象时One
,它最初被称为父类构造函数Test()
。但作为Test()
私人 - 我们得到一个错误。一个好的例子和摆脱这种情况的方法有多少?
采纳答案by Colin Hebert
There is no way out. You have to create an available (protected
, public
or default) super constructor to be able to extend test
.
没有出路。你必须创建一个可用的(protected
,public
或默认)的超级构造函数能够延长test
。
This kind of notation is usually used in utility classes or singletons, where you don't want the user to create himself an instance of your class, either by extending it and instanciating the subclass, or by simply calling a constructor of your class.
这种表示法通常用于实用程序类或单例中,您不希望用户通过扩展和实例化子类或简单地调用类的构造函数来创建自己的类的实例。
When you have a class
with only private
constructors, you can also change the class
to final
because it can't be extended at all.
当您有一个class
只有private
构造函数时,您也可以更改class
tofinal
因为它根本无法扩展。
Another solution would be having a method in test
which create instances of test
and delegate every method call from One
to a test
instance. This way you don't have to extend test
.
另一种解决方案是使用一种方法,在test
该方法中创建实例test
并将每个方法调用委托One
给test
实例。这样您就不必扩展test
.
class Test {
private Test() {
System.out.println("test");
}
public static Test getInstance(){
return new Test();
}
public void methodA(){
//Some kind of implementation
}
}
public class One {
private final Test test;
One() {
System.out.println("One");
test = Test.getInstance();
}
public void methodA(){
test.methodA();
}
public static void main(String args[]) {
new One();
}
}
回答by Mot
Make the constructor of test
non-private
or move One
into test
.
使test
non-的构造函数private
或移动One
到test
.
BTW, your sample code contains a few issues:
顺便说一句,您的示例代码包含一些问题:
- classes should be named title case (
Test
instead oftest
) - I'd suggest to make the
One
's constructorprivate
unless it is called from a different class in the same package
- 类应该命名为标题大小写(
Test
而不是test
) - 我建议制作
One
's 的构造函数,private
除非它是从同一个包中的不同类调用的
回答by delphifirst
Actually, I found there is a way out. Like this:
其实,我发现有出路。像这样:
class Base {
private Base() {
}
public void fn() {
System.out.println("Base");
}
public static class Child extends Base {
public void fn() {
System.out.println("Child");
}
}
public static Base getChild() {
return new Child();
}
}
Now, you can use getChild() to get instance of the extended class.
现在,您可以使用 getChild() 来获取扩展类的实例。