0
0

More than 1 year has passed since last update.

thisとthis()

Posted at
  • 生成者やメソッドの媒介変数とオブジェクト変数の名前が同じときに使用
  • 同じクラス内の他の生成者を呼び出すときに使用

this

メンバー変数に値を入れてあげたいときに使用
メンバー変数:メソッドや生成者ではなく、classにある変数

public class student(){
   int age;        // メンバー変数
   int score;
}

変数名が同じときは近い変数を指す

public student(int age, int score){
   age = age;
   score =score;
}

媒介変数のage、scoreを指すためエラー発生

public student(int age, int score){
   this.age = age;
   this.score =score;
}

thisをつけるとメンバー変数を指す


this()

生成子、メソッドを指す

public student(int age, int score){
   age = age;
   score =score;
}

public student(){
   this(10, 90);
}

同じクラス内にthis()は他の生成子を呼び出す

0
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0