「Java」クラスのthisを利用するサンプル

this
クラスの現在のインスタンスを指します。

Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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

Java

Posted by arkgame