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

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

extends of the class with private constructor

javaconstructor

提问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, publicor default) super constructor to be able to extend test.

没有出路。你必须创建一个可用的(protectedpublic或默认)的超级构造函数能够延长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 classwith only privateconstructors, you can also change the classto finalbecause it can't be extended at all.

当您有一个class只有private构造函数时,您也可以更改classtofinal因为它根本无法扩展。



Another solution would be having a method in testwhich create instances of testand delegate every method call from Oneto a testinstance. This way you don't have to extend test.

另一种解决方案是使用一种方法,在test该方法中创建实例test并将每个方法调用委托Onetest实例。这样您就不必扩展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 testnon-privateor move Oneinto test.

使testnon-的构造函数private或移动Onetest.

BTW, your sample code contains a few issues:

顺便说一句,您的示例代码包含一些问题:

  • classes should be named title case (Testinstead of test)
  • I'd suggest to make the One's constructor privateunless 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() 来获取扩展类的实例。