Rectangle:
class Rectangle{
int width;
int height;
void setSize(int w, int h){
width = w;
height = h;
}
int getArea(){
return width * height;
}
}
Rectangle r = new Rectangle();
Rectangle r = new Rectangle();
r.width = 123;
r.height = 45;
System.out.println("幅は" + r.width);
System.out.println("高さは" + r.height);
Rectangle r = new Rectangle();
r.setSize(123, 45);
class Rectangle{
int width;
int height;
Rectangle(int w, int h){
setSize(w, h);
}
void setSize(int w, int h){
width = w;
height = h;
}
getArea(){
return width * height;
}
}
コンストラクタがある場合:
Rectangle r = new Rectangle(123, 45);
Rectangle.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | class Rectangle{
int width;
int height;
Rectangle(){
setSize(10, 20);
}
Rectangle(int w, int h){
setSize(w, h);
}
void setSize(int w, int h){
width = w;
height = h;
}
int getArea(){
return width * height;
}
public static void main(String[] args){
Rectangle r1 = new Rectangle();
System.out.println("r1.width = " + r1.width);
System.out.println("r1.height = " + r1.height);
System.out.println("r1.getArea() = " + r1.getArea());
Rectangle r2 = new Rectangle(123, 45);
System.out.println("r2.width = " + r2.width);
System.out.println("r2.height = " + r2.height);
System.out.println("r2.getArea() = " + r2.getArea());
}
}
|
Rectangle.java の実行結果は:
[wtopia koji]$ java Rectangle
r1.width = 10
r1.height = 20
r1.getArea() = 200
r2.width = 123
r2.height = 45
r2.getArea() = 5535
1. クラスフィールド
2. クラス変数
3. スタティックフィールド
Rectangle2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | class Rectangle2{
static int counter = 0;
int width;
int height;
int number;
Rectangle2(){
setSize(10, 20);
number = counter;
counter++;
}
Rectangle2(int w, int h){
setSize(w, h);
number = counter;
counter++;
}
void setSize(int w, int h){
width = w;
height = h;
}
int getArea(){
return width * height;
}
public String toString(){
return "幅 = " + width + " 高さ= " + height + "だよ";
}
public static void main(String[] args){
Rectangle2 r1 = new Rectangle2();
System.out.println("r1.width = " + r1.width);
System.out.println("r1.height = " + r1.height);
System.out.println("r1.getArea() = " + r1.getArea());
System.out.println("r1.number = " + r1.number);
System.out.println("r1.counter = " + r1.counter);
Rectangle2 r2 = new Rectangle2(123, 45);
System.out.println("r2.width = " + r2.width);
System.out.println("r2.height = " + r2.height);
System.out.println("r2.getArea() = " + r2.getArea());
System.out.println("r2.number = " + r2.number);
System.out.println("r2.counter = " + r2.counter);
System.out.println(r1);
System.out.println(r2);
Rectangle2 r3 = new Rectangle2();
System.out.println(r3.toString());
}
}
|
Rectangle2.java の実行結果は:
[wtopia koji]$ java Rectangle2
r1.width = 10
r1.height = 20
r1.getArea() = 200
r1.number = 0
r1.counter = 1
r2.width = 123
r2.height = 45
r2.getArea() = 5535
r2.number = 1
r2.counter = 2
幅 = 10 高さ= 20だよ
幅 = 123 高さ= 45だよ
幅 = 10 高さ= 20だよ
そのクラスのインスタンスの標準的な文字列表現を定める:
class Rectangle{
...
public String toString(){
return "[ width = " + width + ", height = " + height + "]";
}
}
以下のように記述する:
Rectangle r = new Rectangle(123, 45);
System.out.println(r);
以下のように表示される:
widht = 123, height = 45
r.toString() メソッドを呼び出すと, 戻り値の文字列をそのインスタンスの文字列表現として扱う.
1. final
2. abstract
3. static
4.synchronized
5. native