this キーワードの使用
this キーワードには、2 つの使用方法があります。
this をドット表記で括弧をつけずに使用し、表示されるクラスの現在のインスタンスを表すことができます。this キーワードのこの形式は、インスタンス変数とメソッドへのアクセスに使用します。次に例を示します。
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class myTestThis {
18
19string s;
20 {
21 this.s = 'TestString';
22 }
23}上記の例では、クラス myTestThis はインスタンス変数 s を宣言します。初期化コードで this キーワードを使用して変数に値を設定します。
また、コンストラクタチェーニングの実行で this キーワードを使用することもできます。コンストラクタチェーニングとは、1 つのコンストラクタから別のコンストラクタをコールすることです。この形式では、this キーワードを括弧と共に使用します。次に例を示します。
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class testThis {
18
19// First constructor for the class. It requires a string parameter.
20 public testThis(string s2) {
21 }
22
23// Second constructor for the class. It does not require a parameter.
24// This constructor calls the first constructor using the this keyword.
25 public testThis() {
26 this('None');
27 }
28}コンストラクタでのコンストラクタチェーニングの実行で this キーワードを使用する場合、コンストラクタの 1 つ目のステートメントに記述する必要があります。