java 在构造函数结束后调用方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27453105/
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-11-02 11:49:49  来源:igfitidea点击:

Call a method after the constructor has ended

java

提问by mikelplhts

I need to call a method after the constructor has ended and I have no idea how to do. I have this class:

我需要在构造函数结束后调用一个方法,但我不知道该怎么做。我有这门课:

Class A {
    public A() {
        //...
    }

    public void init() {
        //Call after the constructor
    }
}

回答by aioobe

You either have to do this on the client side, as so:

您要么必须在客户端执行此操作,如下所示:

A a = new A();
a.init();

or you would have to do it in the end of the constructor:

或者您必须在构造函数的末尾执行此操作:

class A {
    public A() {
        // ...
        init();
    }

    public final void init() {
        // ...
    }
}

The second way is not recommended however, unless you make the method private or final.

但是,不建议使用第二种方式,除非您将方法设为私有或最终。



Another alternative may be to use a factory method:

另一种选择可能是使用工厂方法:

class A {
    private A() {  // private to make sure one has to go through factory method
        // ...
    }
    public final void init() {
        // ...
    }
    public static A create() {
        A a = new A();
        a.init();
        return a;
    }
}


Related questions:

相关问题:

回答by David Conrad

You will need a static factory method to construct the object, call the init method, and finally return the object:

您将需要一个静态工厂方法来构造对象,调用 init 方法,最后返回对象:

class A {
    private A() {
        //...
    }

    private void init() {
        //Call after the constructor
    }

    public static A create() {
        A a = new A();
        a.init();
        return a;
    }
}

Notice I have made the constructor and the init()method private, so that they can only be accessed by the factory method. Client code would make objects by calling A.create()instead of calling the constructor.

请注意,我已将构造函数和init()方法设为私有,以便它们只能由工厂方法访问。客户端代码将通过调用A.create()而不是调用构造函数来创建对象。

回答by UniversE

I pick up some ideas and provide an abstractable solution:

我收集了一些想法并提供了一个抽象的解决方案:

class A {
    protected A() {
        // ...
    }
    protected void init() {
        // ...
    }
    public static <T extends A> T create(Class<T> type) {
        try {
            T obj = type.newInstance();
            obj.init();
            return obj;
        } catch (ReflectiveOperationException ex) {
            System.err.println("No default constructor available.");
            assert false;
            return null;
        }
    }
}

回答by acsadam0404

If you want to call method BEFORE constructor you can use initializer block. https://www.geeksforgeeks.org/g-fact-26-the-initializer-block-in-java/

如果你想在构造函数之前调用方法,你可以使用初始化块。https://www.geeksforgeeks.org/g-fact-26-the-initializer-block-in-java/

class A {
    { 
        init() 
    }

    public A() {
        //todo
    }

    private final void init() {
       //todo
    }
}

回答by AngularLover

What did you so far? Are you looking something like this?

到目前为止你做了什么?你看起来像这样吗?

  Class A {
        public A() {
            //...
        }

        public void init() {
            //Call after the constructor
        }
    }

     public static void main(String[] args)
    {
    A a = new A();

    a.init();
}

回答by Nico

Why not this :

为什么不是这个:

Class A {
    public A() {
        //... Do you thing
        this.init();
    }

    public void init() {
        //Call after the constructor
    }
}

回答by Dima Maligin

How bout this:

这个怎么样:

Class A {
    public A() {
        // to do
        init();
    }

    public void init() {
        //Call after the constructor
    }
}