public class Onigiri {
static int count = 0;
static void printCount(){
System.out.println(count);
}
// フィールド/メンバ=データの詳細
String gu ; /* おにぎりの具名 */
boolean withNori; /* 海苔がついているか? true / false */
int shape ; /* 0:丸, 1:俵型, 3:三角形 */
//コンストラクタ=データ製造機
public Onigiri() {
gu = "無し(塩むすび)";
withNori = false;
shape = 3;
count++;
}
public Onigiri(String _gu) {
this();
gu = _gu;
}
public Onigiri(String gu, int shape) {
this(gu);
this.shape = shape;
}
public Onigiri(String gu, boolean wnori) {
this(gu);
withNori = wnori;
}
public Onigiri(String gu, boolean wnori, int shape) {
this(gu, wnori);
this.shape = shape;
}
//メソッドの役割:(1) データの修正・更新をする (2) データに関する情報を出力 (3) データに関する処理
public void setWithNori(boolean wnori) {
withNori = wnori;
}
public String toString() {
String str = "具は "+gu+" で、";
switch(shape){
case 0:
str = str+"丸型で、";
break;
case 1:
str = str+"俵型で、";
break;
case 3:
str = str+"三角形で、";
break;
}
str = str + (withNori ? "海苔付き" : "海苔なし");
return str;
}
public static void main(String[] args) {
new Onigiri();
//基本のおにぎりをつくる
//コンストラクタを呼び出し、インスタンスを生成
Onigiri siomusubi = new Onigiri();
System.out.println("siomusubi = " + siomusubi.toString());
Onigiri ume = new Onigiri("梅", true);
System.out.println("ume = " + ume.toString());
Onigiri marusake = new Onigiri("鮭", 0);
System.out.println("marusake = " + marusake.toString());
Onigiri mentai = new Onigiri("明太子", true, 1);
System.out.println("mentai = " + mentai.toString());
Onigiri.printCount();
}
}