Salesforce Apex 引数ありのコンストラクタを使用するサンプル
書式
public class クラス名 {
public コンストラクタ名(引数){
処理コード}
}
引数を取るコンストラクタを作成し、引数を取らないコンストラクタも引き続き使用する場合は、引数を取らない独自のコンストラクタをコード内で作成する必要があります。
いったんクラスのコンストラクタを作成すると、デフォルトの引数を取らない公開コンストラクタにアクセスすることはできません。
使用例
public class SampleObject {
private static final Integer DEFAULT_SIZE = 20;
Integer size;
//引数なしのコンストラクタ
public SampleObject() {
this(DEFAULT_SIZE); // Using this(...) calls the one argument constructor
}
//引数ありのコンストラクタ
public SampleObject(Integer ObjectSize) {
size = ObjectSize;
}
}
public class SampleObject {
private static final Integer DEFAULT_SIZE = 20;
Integer size;
//引数なしのコンストラクタ
public SampleObject() {
this(DEFAULT_SIZE); // Using this(...) calls the one argument constructor
}
//引数ありのコンストラクタ
public SampleObject(Integer ObjectSize) {
size = ObjectSize;
}
}
public class SampleObject { private static final Integer DEFAULT_SIZE = 20; Integer size; //引数なしのコンストラクタ public SampleObject() { this(DEFAULT_SIZE); // Using this(...) calls the one argument constructor } //引数ありのコンストラクタ public SampleObject(Integer ObjectSize) { size = ObjectSize; } }
この型の新しいオブジェクトは、次のコードを使用してインスタンス化できます。
SampleObject myObject1 = new SampleObject(22);
SampleObject myObject2 = new SampleObject();
SampleObject myObject1 = new SampleObject(22);
SampleObject myObject2 = new SampleObject();
SampleObject myObject1 = new SampleObject(22); SampleObject myObject2 = new SampleObject();