代码之家  ›  专栏  ›  技术社区  ›  Ricky Clarkson

如何使构造函数的参数可从实例初始化器获得?

  •  0
  • Ricky Clarkson  · 技术社区  · 17 年前
    class Foo {
        final String s;
    
        final int x = s.length();
    
        Foo(String s) {
            this.s = s;
        }
    }
    

    上面的代码不会编译,因为对x的赋值发生在对s的赋值之前。我可以将对x的赋值放在构造函数中,但是对于100个字段的真实情况下的可读性,我宁愿不编译。

    我想出了一个我不喜欢的变通办法。你能找个更好的吗?

    abstract class Hack {
        final String s;
    
        Hack(String s) {
            this.s = s;
        }
    }
    
    class Foo extends Hack {
        final int x = s.length();
    
        Foo(String s) {
            super(s);
        }
    }
    
    8 回复  |  直到 17 年前
        1
  •  0
  •   wierob    17 年前

    自从 x 总是依赖 s 你有3个选择。

    1)初始化 X 是初始化的副作用 S

    class Foo {
        final String s;
        final int x;
    
        private void setS(String s) {
            this.s = s;
            this.x = s.length();
        }
    
        Foo(String s) {
            this.setS(s);
        }
    }
    

    2)计算 X 从 S

    class Foo {
        final String s;
    
        private int getX() {
            return s.length();
        }
    
        Foo(String s) {
            this.setS(s);
        }
    }
    

    2a)计算 X 从 S 但是缓存 X

    class Foo {
        final String s;
        private int cached_x = null;
    
        int getX() {
            if (x == null) {
                cached_x = s.lenth();
            return cached_x;
        }
    
        Foo(String s) {
            this.s = s;
        }
    }
    

    如果这些选项很复杂,则可以在构造函数中初始化变量。这就是它的目的:

    class Foo {
        final String s;
        final int x;
    
        Foo(String s) {
            this.s = s;
            this.x = this.s.length();
        }
    }
    
        2
  •  4
  •   NomeN    17 年前

    就可读性而言:

    class Foo {    
      final String s;    
      final int sizeOfS;
      Foo(String s) {        
        this.s = s;    
        sizeOfS = s.length();
      }
    }
    

    可读性首先来自适当的变量命名!

        3
  •  1
  •   PeterMmm    17 年前

    我想你不能。AS Java basic tutorial 声明,如果对于初始化,您有一些比常量更复杂的逻辑,那么您必须将代码写入构造函数(或静态初始化块)。

        4
  •  1
  •   Yishai    17 年前

    考虑构建器模式:

     public interface Foo {}
    
     public class FooBuilder  {
    
           private final String s;
    
           public FooBuilder(String s) {
               this.s = s;
           }
    
           public Foo build() {
               return new FooImpl();
           }
    
           private class FooImpl implements Foo {
               final int x = FooBuilder.this.s.length();
           }
      }
    

    这里可能有一些不太好的扭曲,因为实现必须转移到一个内部类。尽管接口可以是一个抽象类,但实现了相同的目的。

        5
  •  0
  •   Tom Hawtin - tackline    17 年前

    除注释外:

    interface Foo {
    }
    
    public static Foo newFoo(final String _s) {
        return new Foo() {
            private final String s = _s;
            private final int x = _s.length();
       };
    }
    
        6
  •  0
  •   Jorn    17 年前

    请注意,您的示例将始终失败:它试图在初始化之前获取s的长度,从而导致 NullPointerException .

    这么做有什么问题?

    final String s;
    final int x;
    
    Foo(String s) {
      this.s = s;
      this.x = s.length();
    }
    
        7
  •  0
  •   Miserable Variable    17 年前

    因为在你原来的例子中 x 在功能上依赖于 s 做一个单独的类来聚合这两个类有意义吗?那么,父级只有一个分配。您仍然需要分配给s并计算 <init> 但在没有100个字段的情况下,这似乎更具可读性。

        8
  •  -2
  •   KitsuneYMG    17 年前

    我会有一个集合(字符串)方法来正确设置x int。请使用它作为构造函数。如果不希望S或X可以公开设置,请将sets方法设为private/protected/default。

    class Foo {
        int x;
        String s;
        private setS(String s) { this.s=s;this.x=s.length();}
    
        Foo(String s) {
                setS(s);
        }
    }
    

    另一个解决方案(似乎更接近于您要做的)是消除x int。提供:

    int getX() { return s.length(); }
    

    而不是X成员。

    如果x确实是更复杂对象的代理,请尝试:

    class Foo {
        private Object complex;
        private String s;
        Foo(String s){ this.s = s;}
        Object getComplex() {
            if (complex == null) {
                *complex=new Complex(s);
            }
            return complex;
        }
    }
    

    *可能需要在此处添加同步或双重检查锁定,以确保线程安全。