この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

拡張クラスの例

クラスの拡張の例を次に示します。Apex クラスのすべての機能を示します。この例で使用されるキーワードや概念は、この章内で詳細に説明します。

1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// Top-level (outer) class must be public or global (usually public unless they contain
18// a Web Service, then they must be global)
19public class OuterClass {
20
21  // Static final variable (constant) – outer class level only
22  private static final Integer MY_INT;
23
24  // Non-final static variable - use this to communicate state across triggers
25  // within a single request)
26  public static String sharedState;
27  
28  // Static method - outer class level only
29  public static Integer getInt() { return MY_INT; }
30
31  // Static initialization (can be included where the variable is defined)
32  static {
33    MY_INT = 2; 
34  }
35
36  // Member variable for outer class
37  private final String m;
38
39  // Instance initialization block - can be done where the variable is declared, 
40  // or in a constructor
41  {
42    m = 'a';  
43  }
44  
45  // Because no constructor is explicitly defined in this outer class, an implicit, 
46  // no-argument, public constructor exists
47
48  // Inner interface
49  public virtual interface MyInterface { 
50
51    // No access modifier is necessary for interface methods - these are always 
52    // public or global depending on the interface visibility
53    void myMethod(); 
54  }
55
56  // Interface extension
57  interface MySecondInterface extends MyInterface { 
58    Integer method2(Integer i); 
59  }
60
61  // Inner class - because it is virtual it can be extended.
62  // This class implements an interface that, in turn, extends another interface.
63  // Consequently the class must implement all methods.
64  public virtual class InnerClass implements MySecondInterface {
65
66    // Inner member variables
67    private final String s;
68    private final String s2;
69
70    // Inner instance initialization block (this code could be located above)
71    {
72       this.s = 'x';
73    }
74
75    // Inline initialization (happens after the block above executes)
76    private final Integer i = s.length();
77 
78    // Explicit no argument constructor
79    InnerClass() {
80       // This invokes another constructor that is defined later
81       this('none');
82    }
83
84    // Constructor that assigns a final variable value
85    public InnerClass(String s2) { 
86      this.s2 = s2; 
87    }
88
89    // Instance method that implements a method from MyInterface.
90    // Because it is declared virtual it can be overridden by a subclass.
91    public virtual void myMethod() { /* does nothing */ }
92
93    // Implementation of the second interface method above.
94    // This method references member variables (with and without the "this" prefix)
95    public Integer method2(Integer i) { return this.i + s.length(); }
96  }
97
98  // Abstract class (that subclasses the class above). No constructor is needed since
99  // parent class has a no-argument constructor
100  public abstract class AbstractChildClass extends InnerClass {
101
102    // Override the parent class method with this signature.
103    // Must use the override keyword
104    public override void myMethod() { /* do something else */ }
105
106    // Same name as parent class method, but different signature.
107    // This is a different method (displaying polymorphism) so it does not need
108    // to use the override keyword
109    protected void method2() {}
110
111    // Abstract method - subclasses of this class must implement this method
112    abstract Integer abstractMethod();
113  }
114
115  // Complete the abstract class by implementing its abstract method
116  public class ConcreteChildClass extends AbstractChildClass {
117    // Here we expand the visibility of the parent method - note that visibility 
118    // cannot be restricted by a sub-class
119    public override Integer abstractMethod() { return 5; }
120  }
121
122  // A second sub-class of the original InnerClass
123  public class AnotherChildClass extends InnerClass {
124    AnotherChildClass(String s) {
125      // Explicitly invoke a different super constructor than one with no arguments
126      super(s);
127    }
128  }
129  
130  // Exception inner class
131  public virtual class MyException extends Exception {
132    // Exception class member variable
133    public Double d;
134
135    // Exception class constructor    
136    MyException(Double d) {
137      this.d = d;
138    }
139    
140    // Exception class method, marked as protected
141    protected void doIt() {}
142  }
143  
144  // Exception classes can be abstract and implement interfaces
145  public abstract class MySecondException extends Exception implements MyInterface {
146  }
147}
148
149
このコード例では次を示しています。
  • 最上位クラスの定義 (外部クラスとも呼ぶ)
  • 最上位クラスの静的変数および静的メソッド、および静的初期化コードブロック
  • 最上位クラスのメンバー変数とメソッド
  • ユーザ定義のコンストラクタが存在しないクラス。暗黙的で、引数をとらないコンストラクタを含む。
  • 最上位クラスのインターフェース定義
  • 別のインターフェースを拡張するインターフェース
  • 最上位クラス内の内部クラス定義 (1 つ下のレベル)
  • メソッド署名の公開バージョンを実装することでインターフェース (つまり、関連付けられているサブインターフェース) を実装するクラス
  • 内部クラスコンストラクタの定義と呼び出し
  • 内部クラスのメンバー変数と、this キーワード (引数なし) を使用したその変数の参照
  • 別のコンストラクタの呼び出しに this キーワード (引数なし) を使用する内部クラスコンストラクタ
  • コンストラクタ外 (変数が定義されている箇所と、中括弧 ({}) で囲まれた匿名のブロックの両方) の初期化コード。これらのコードは、Java と同様にファイルに記述されている順序どおりにすべてのコンストラクションと共に実行されます。
  • クラスの拡張と抽象クラス
  • 基本のクラスメソッドを上書きするメソッド (virtual として宣言する必要がある)
  • サブクラスメソッドを上書きするメソッドの override キーワード
  • 抽象メソッドと具体的なサブクラスによる実装
  • protected アクセス修飾子
  • ファーストクラスオブジェクトとしての例外とそのメンバー、メソッド、コンストラクタ

この例では、上記のクラスを他の Apex コードからコールする方法を示します。

1// Construct an instance of an inner concrete class, with a user-defined constructor
2OuterClass.InnerClass ic = new OuterClass.InnerClass('x');
3
4// Call user-defined methods in the class
5System.assertEquals(2, ic.method2(1));
6
7// Define a variable with an interface data type, and assign it a value that is of 
8// a type that implements that interface
9OuterClass.MyInterface mi = ic;
10
11// Use instanceof and casting as usual
12OuterClass.InnerClass ic2 = mi instanceof OuterClass.InnerClass ? 
13                            (OuterClass.InnerClass)mi : null;
14System.assert(ic2 != null);
15
16// Construct the outer type
17OuterClass o = new OuterClass();
18System.assertEquals(2, OuterClass.getInt());
19
20// Construct instances of abstract class children
21System.assertEquals(5, new OuterClass.ConcreteChildClass().abstractMethod());
22
23// Illegal - cannot construct an abstract class
24// new OuterClass.AbstractChildClass();
25
26// Illegal – cannot access a static method through an instance
27// o.getInt();
28
29// Illegal - cannot call protected method externally
30// new OuterClass.ConcreteChildClass().method2();
このコード例では次を示しています。
  • 外部クラスの作成
  • 内部クラスの作成と内部インターフェース型の宣言
  • インターフェース型として宣言された変数を、インターフェースを実装するクラスのインスタンスに割り当て可能
  • そのインターフェースを実装するクラス型にインターフェース変数をキャスト (instanceof 演算子を使用した検証後)