「Java」クラスのthisを利用するサンプル
this
クラスの現在のインスタンスを指します。
Javaコード
package com.arkgame.study;
public class ThisDemo {
// クラスのメンバー
String cft = "parameter_class";
public static void main(String[] args) {
ThisDemo s = new ThisDemo();
s.runFunc();
}
void runFunc() {
// ローカル変数
String cft = "parameter_local";
System.out.println("実行結果:");
// ローカル変数を指す
System.out.println("値1:" + cft);
// メンバー出力
System.out.println("値2:" + this.cft);
}
}
package com.arkgame.study;
public class ThisDemo {
// クラスのメンバー
String cft = "parameter_class";
public static void main(String[] args) {
ThisDemo s = new ThisDemo();
s.runFunc();
}
void runFunc() {
// ローカル変数
String cft = "parameter_local";
System.out.println("実行結果:");
// ローカル変数を指す
System.out.println("値1:" + cft);
// メンバー出力
System.out.println("値2:" + this.cft);
}
}
package com.arkgame.study; public class ThisDemo { // クラスのメンバー String cft = "parameter_class"; public static void main(String[] args) { ThisDemo s = new ThisDemo(); s.runFunc(); } void runFunc() { // ローカル変数 String cft = "parameter_local"; System.out.println("実行結果:"); // ローカル変数を指す System.out.println("値1:" + cft); // メンバー出力 System.out.println("値2:" + this.cft); } }
実行結果:
値1:parameter_local
値2:parameter_class