简体   繁体   English

继承和静态工厂方法

[英]Inheritance and static factory methods

I have programmed a java.util.logging.Formatter (named OneLineFormatter) for my console outputs. 我已经为控制台输出编程了一个java.util.logging.Formatter(名为OneLineFormatter)。 It has two static factory methods, both call the private constructor. 它有两个静态工厂方法,都调用私有构造函数。

Now I wanted to program a second one for debugging purposes (named DebugFormatter), which only overrides the method formatRecord in OneLineFormatter so the traces are printed too instead of just the localized message and the class. 现在,我想编写第二个用于调试目的的程序(名为DebugFormatter),它仅覆盖OneLineFormatter中的formatRecord方法,因此也可以打印跟踪,而不仅是本地化的消息和类。

Eclipse warned me that the super constructor OneLineFormatter() is undefined and I have to invoke another constructor. Eclipse警告我,超级构造函数OneLineFormatter()未定义,因此我必须调用另一个构造函数。 I googled the problem and found this: Java error: Implicit super constructor is undefined for default constructor on StackOverflow. 我用问题搜索了一下,发现了这一点: Java错误:对于 StackOverflow上的默认构造函数未定义隐式超级构造函数 But I do not want to create a public constructor because that would be against the factory principle. 但是我不想创建一个公共构造函数,因为这将违反工厂原则。 The factory methods and the constructor can be the same (the DebugFormatter factory methods should create a new DebugFormatter instead of a OneLineFormatter though). 工厂方法和构造函数可以相同(尽管DebugFormatter工厂方法应创建一个新的DebugFormatter而不是OneLineFormatter)。

If you need some more info just ask. 如果您需要更多信息,请询问。 Thanks for your help in advance! 谢谢您的帮助!

The code: 编码:

public class OneLineFormatter extends Formatter {
    public static Formatter withPackageFromRoot(String rootName) {
        return new OneLineFormatter(rootName);
    }

    public static Formatter withClassOutputOnly() {
        return new OneLineFormatter("");
    }

    private String rootName;

    private OneLineFormatter(String rootName) {
        this.rootName = rootName;
    }

    @Override
    public String format(LogRecord record){<code>}

    private String formatRecord(LogRecord record{<code that I want to override>}
}

And the second class: 第二类:

public class DebugFormatter extends OneLineFormatter {
    public static Formatter withClassOutputOnly() {
        return new DebugFormatter("");
    }

    public static Formatter withPackageFromRoot(String rootName) {
        return new DebugFormatter(rootName);
    }

    private DebugFormatter(String rootName) {<same as OneLineFormatter(String)>}

    @Override
    private String formatRecord(LogRecord record) {<code>} 

}

EDIT 1: added code EDIT 2: corrected code 编辑1:添加代码编辑2:更正的代码

You could just make the constructor for OneLineFormatter package-private or protected . 你可以只作出构造OneLineFormatter package-privateprotected This way you could reduce the access to the constructor to a point that fits your needs 这样,您可以减少对构造函数的访问,使其符合您的需求

OneLineFormatter(String rootName) {
    this.rootName = rootName;
}
// OR 
protected OneLineFormatter(String rootName) {
    this.rootName = rootName;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM