問題一覧に戻る
中級オブジェクト指向
問題49: thisキーワード

'this'キーワードは現在のオブジェクトインスタンスを参照します。同じ名前のインスタンス変数とパラメータを区別するために使用されます。

public class Rectangle {
private double width;
private double height;

// thisで区別
public Rectangle(double width, double height) {
.width = width;
.height = height;
}

public double getArea() {
return width * height;
}

public static void main(String[] args) {
Rectangle rect = new Rectangle(5.0, 3.0);
System.out.println("Area: " + rect.getArea());
}
}