256

使用したいClass.newInstance()のですが、インスタンス化しようとしているクラスに nullary コンストラクターがありません。したがって、コンストラクター引数を渡すことができる必要があります。これを行う方法はありますか?

4

8 に答える 8

223
MyClass.class.getDeclaredConstructor(String.class).newInstance("HERESMYARG");

また

obj.getClass().getDeclaredConstructor(String.class).newInstance("HERESMYARG");
于 2008-10-24T17:52:26.553 に答える
91
myObject.getClass().getDeclaredConstructors(types list).newInstance(args list);

編集:コメントによると、一部のユーザーにとっては、クラス名とメソッド名を指すだけでは十分ではないようです。詳細については、コンストラクターの取得呼び出しに関するドキュメントを参照してください。

于 2008-10-24T17:51:53.933 に答える
83

次のコンストラクタがあると仮定します

class MyClass {
    public MyClass(Long l, String s, int i) {

    }
}

次のように、このコンストラクターを使用するつもりであることを示す必要があります。

Class classToLoad = MyClass.class;

Class[] cArg = new Class[3]; //Our constructor has 3 arguments
cArg[0] = Long.class; //First argument is of *object* type Long
cArg[1] = String.class; //Second argument is of *object* type String
cArg[2] = int.class; //Third argument is of *primitive* type int

Long l = new Long(88);
String s = "text";
int i = 5;

classToLoad.getDeclaredConstructor(cArg).newInstance(l, s, i);
于 2014-06-08T19:15:07.283 に答える
20

使用しないでくださいClass.newInstance()。このスレッドを参照してください:なぜ Class.newInstance() は悪なのですか?

他の答えが言うように、Constructor.newInstance()代わりに使用してください。

于 2008-10-24T20:43:51.713 に答える
9

getConstructor(...)で他のコンストラクターを取得できます。

于 2008-10-24T17:51:03.857 に答える
1

getDeclaredConstructorクラスのメソッドを使用できます。クラスの配列が必要です。テスト済みの実際の例を次に示します。

public static JFrame createJFrame(Class c, String name, Component parentComponent)
{
    try
    {
        JFrame frame = (JFrame)c.getDeclaredConstructor(new Class[] {String.class}).newInstance("name");
        if (parentComponent != null)
        {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        else
        {
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }
        frame.setLocationRelativeTo(parentComponent);
        frame.pack();
        frame.setVisible(true);
    }
    catch (InstantiationException instantiationException)
    {
        ExceptionHandler.handleException(instantiationException, parentComponent, Language.messages.get(Language.InstantiationExceptionKey), c.getName());
    }
    catch(NoSuchMethodException noSuchMethodException)
    {
        //ExceptionHandler.handleException(noSuchMethodException, parentComponent, Language.NoSuchMethodExceptionKey, "NamedConstructor");
        ExceptionHandler.handleException(noSuchMethodException, parentComponent, Language.messages.get(Language.NoSuchMethodExceptionKey), "(Constructor or a JFrame method)");
    }
    catch (IllegalAccessException illegalAccessException)
    {
        ExceptionHandler.handleException(illegalAccessException, parentComponent, Language.messages.get(Language.IllegalAccessExceptionKey));
    }
    catch (InvocationTargetException invocationTargetException)
    {
        ExceptionHandler.handleException(invocationTargetException, parentComponent, Language.messages.get(Language.InvocationTargetExceptionKey));
    }
    finally
    {
        return null;
    }
}
于 2013-07-02T14:56:30.040 に答える