私はクラスのコンストラクタを2つ持っています.1つはオブジェクトを受け取り、もう1つはthis
キーワードを使用します。実装の関数は、File
をパラメータとし、String
の関数はthis
を呼び出します。今私はString
を取るコンストラクタで例外をチェックしたいが、私はエラーを得る、そのthis
は、最初の行にする必要があります。エラーを確認するには、this
に電話してください。ここでコンストラクタが例外を処理し、このキーワードを使用しています。Java
は私のコードです:
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
どのようなエラーが表示されますか? – Thomas
投稿されたコードはあなたが説明したものとちょうど反対です! – NINCOMPOOP