5

クラスには 2 つのコンストラクターがあり、1 つはFileオブジェクトを受け取り、もう 1 つはオブジェクトを受け取り、キーワードStringを使用したいと考えています。this実装のある関数はFileas パラメータを持つもので、Stringwill callを持つものthisです。今、コンストラクターで例外をチェックしたいのですが、Stringエラーが発生しましたthis。これは最初の行である必要があります。エラーを確認してから呼び出すにはどうすればよいですかthis

これが私のコードです:

public Test (String filename) {
    if (filename == null)
        throw new NullPointerException("The String you entered is null.");
    if (filename.isEmpty())
        throw new IllegalArgumentException("The String you entered is empty.");

    this(new File(filename)); // error
}

public Test (File f) {
    /* implementation here */
}

これは正確なエラーです:Constructor call must be the first statement in a constructor

4

3 に答える 3