如何避免代码重复初始化最终属性?

发布于 2024-08-28 00:37:45 字数 571 浏览 9 评论 0原文

public class Code{

//many properties
//...

final String NEWLINE;// ohh a final property!

void creation() //this method is for avoid repetition of code
{        

    //final initialization can't be put here =(

    Source= new StringBuffer();

   //many other commons new's ..
   //...
}

Code()
{
    NEWLINE = System.getProperty("line.separator");
    creation();
}

Code(String name, int numberr)
{
    NEWLINE = System.getProperty("line.separator");
    creation();

    name=new Someting(name);
    number = new Magic(number);
}

}

public class Code{

//many properties
//...

final String NEWLINE;// ohh a final property!

void creation() //this method is for avoid repetition of code
{        

    //final initialization can't be put here =(

    Source= new StringBuffer();

   //many other commons new's ..
   //...
}

Code()
{
    NEWLINE = System.getProperty("line.separator");
    creation();
}

Code(String name, int numberr)
{
    NEWLINE = System.getProperty("line.separator");
    creation();

    name=new Someting(name);
    number = new Magic(number);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

金橙橙 2024-09-04 00:37:45

这是您的代码,有 4 种不同的初始化最终变量的方法。

  1. 内联
  2. 匿名初始化块
  3. 在构造函数中
  4. 显式调用默认构造函数进行

初始化结果输出如下所示。

public class Code {

    // many properties
    private String name;
    private String number;
    // ...

    // 1.
    final String NEWLINE_1 = "1" + System.getProperty("line.separator");
    final String NEWLINE_2;
    final String NEWLINE_3;

    // 2.
    {
        System.out.println("initializer block invoked before Constructor");

        NEWLINE_2 = "2" + System.getProperty("line.separator");
        // final initialization CAN be put here =(

        // Source = new StringBuffer();

        // many other commons new's ..
        // ...
    }

    Code() {
        System.out.println("default constructor");
        // NEWLINE_1 = "error";     can't do this
        // NEWLINE_2 = "error";     can't do this

        // 3.
        NEWLINE_3 = "3" + System.getProperty("line.separator");
    }

    Code(String name, int number) {
        // 4.
        this();
        System.out.println("constructor(name, number)");

        name = new String("Someting(name)");
        this.number = new String("Magic(number)");
    }

    public static void main(String[] args) {
        Code code_1 = new Code();
        System.out.println(code_1.NEWLINE_1 + ":" + code_1.NEWLINE_2 + ":" + code_1.NEWLINE_3);

        Code code_2 = new Code("crowne", 2);
        System.out.println(code_2.NEWLINE_1 + ":" + code_2.NEWLINE_2 + ":" + code_2.NEWLINE_3);
    }
}

initializer block invoked before Constructor
default constructor
1
:2
:3

initializer block invoked before Constructor
default constructor
constructor(name, number)
1
:2
:3

Here is your code with 4 different ways of initializing final variables.

  1. inline
  2. anonymous initializer block
  3. initialized in the constructor
  4. calling the default constructor explicitly

The resulting output is shown below.

public class Code {

    // many properties
    private String name;
    private String number;
    // ...

    // 1.
    final String NEWLINE_1 = "1" + System.getProperty("line.separator");
    final String NEWLINE_2;
    final String NEWLINE_3;

    // 2.
    {
        System.out.println("initializer block invoked before Constructor");

        NEWLINE_2 = "2" + System.getProperty("line.separator");
        // final initialization CAN be put here =(

        // Source = new StringBuffer();

        // many other commons new's ..
        // ...
    }

    Code() {
        System.out.println("default constructor");
        // NEWLINE_1 = "error";     can't do this
        // NEWLINE_2 = "error";     can't do this

        // 3.
        NEWLINE_3 = "3" + System.getProperty("line.separator");
    }

    Code(String name, int number) {
        // 4.
        this();
        System.out.println("constructor(name, number)");

        name = new String("Someting(name)");
        this.number = new String("Magic(number)");
    }

    public static void main(String[] args) {
        Code code_1 = new Code();
        System.out.println(code_1.NEWLINE_1 + ":" + code_1.NEWLINE_2 + ":" + code_1.NEWLINE_3);

        Code code_2 = new Code("crowne", 2);
        System.out.println(code_2.NEWLINE_1 + ":" + code_2.NEWLINE_2 + ":" + code_2.NEWLINE_3);
    }
}

initializer block invoked before Constructor
default constructor
1
:2
:3

initializer block invoked before Constructor
default constructor
constructor(name, number)
1
:2
:3
若能看破又如何 2024-09-04 00:37:45

所有初始值设定项均由编译器添加到每个构造函数的开头。这包括:

  • 实例变量初始化
  • 初始化块 { .. }

所以你不必在任何地方都包含它,只需将它作为实例变量初始化:

private final String NEWLINE = System.getProperty("line.separator");

或放在初始化块中:

{
     NEWLINE = System.getProperty("line.separator");
}

当然,在这个准确的示例是,您应该将该字段设为静态

All initializers are added by the compiler to the beginning of each constructor. This includes:

  • instance variable initialization
  • initialization blocks { .. }

So you don't have to include this everywhere just place it either as an instance-variable initialization:

private final String NEWLINE = System.getProperty("line.separator");

or in initialization block:

{
     NEWLINE = System.getProperty("line.separator");
}

Of course, in this precise example, you should make the field static.

弃爱 2024-09-04 00:37:45

只需执行:

final String NEWLINE = System.getProperty("line.separator");

请参阅:JLS 8.3.2。字段初始化

另请参阅:JLS 12.5 创建新类实例 表示执行顺序。

Just do:

final String NEWLINE = System.getProperty("line.separator");

See: JLS 8.3.2. Initialization of Fields.

See also: JLS 12.5 Creation of New Class Instances for execution order.

蓦然回首 2024-09-04 00:37:45

如果每次都以相同的方式初始化它们,则可以将代码放在构造函数之外。 Java 允许您执行

final String NEWLINE = System.getProperty("line.separator");

以下操作: 您还可以让除无参构造函数之外的所有构造函数调用无参构造函数。例如:

Code(String name, int number)
{
    this();

    name=new Someting(name);
    number = new Magic(number);
}

If they are being initialized the same way every time, you can put the code outside of the constructors. Java allows you to do:

final String NEWLINE = System.getProperty("line.separator");

You can also have all constructors other than the no-argument one call the no-argument constructor. For example:

Code(String name, int number)
{
    this();

    name=new Someting(name);
    number = new Magic(number);
}
愁以何悠 2024-09-04 00:37:45

另外,如果初始化很复杂并且您必须在构造期间执行此操作,请提供一个返回结果的静态方法,如下所示:

Code()
{
    NEWLINE = newLineValue();
    creation();
}

Code(String name, int number)
{
    NEWLINE = newLineValue();
    creation();

    name = new Something(name);
    number = new Magic(number);
}

private static String newLineValue()
{
    return System.getProperty("line.separator");
}

在这种情况下,newLineValue() 很简单,因此我不会使用它在这里,但如果这实际上有大量的工作,那么它可能会有用。您还可以从构造函数传入参数。

One more, if the initialization is complex and you must do it during construction, provide a static method the returns the result, as in:

Code()
{
    NEWLINE = newLineValue();
    creation();
}

Code(String name, int number)
{
    NEWLINE = newLineValue();
    creation();

    name = new Something(name);
    number = new Magic(number);
}

private static String newLineValue()
{
    return System.getProperty("line.separator");
}

In this case, newLineValue() is trivial so I wouldn't use it here, but if this actually had a significant amount of work then it could be useful. You can also pass in parameters from the constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文