■フィールドとは?
フィールド・・・クラス内の変数
フィールドの定義
class X {
int a = 5 ; ←フィールドの定義
:
}
■フィールドの参照と代入
・同じオブジェクト内のフィールドを代入・参照・・・フィールド名をそのまま記述
|
代入
|
a = 9 ;
|
|
参照
|
b = a + 2 ;
|
a →フィールド名
a →フィールド名
・異なるオブジェクト内のフィールドを代入・参照・・・「.」ピリオドを記述
|
代入
|
x.a = 5 ;
|
|
参照
|
b = x.a + 2 ;
|
x.a →オブジェクト名.フィールド名
x.a →オブジェクト名.フィールド名
☆サンプル☆
class Wdata {
int month;
int day;
String sky; ←フィールドの定義
}
class Weather {
public static void main(String[] args) {
Wdata today = new Wdata () ;
today.month = 10;
today.day = 5;
today.sky = "はれ";
System.out.println(today.month + "月" + today.day + "日" + today.sky);
}
}