-
상자 정보 (상속2)코딩/java 2019. 3. 3. 00:02반응형
public class Box {
private int x;
private int y;
private int z;
private boolean empty;
public String toString() {
String rslt = "가로: " + x + "cm\n세로: " + y + "cm\n높이: " + z + "cm\n";
if (empty)
return rslt + "지금 박스는 비어있습니다.\n";
else
return rslt + "지금 박스에는 물건이 들어있습니다.\n";
}
public void setX(int x) {this.x = x;}
public void setY(int y) {this.y = y;}
public void setZ(int z) {this.z = z;}
public int getX() {return x;}
public int getY() {return y;}
public int getZ() {return z;}
public void fillBox() {
this.empty = false;
}
public void emptyBox() {
this.empty = true;
}
public int getVolume() {
return x * y * z;
}
public double getWeight() {
return 1.1 * getVolume();
}
public Box() {
this(0, 0, 0);
}
public Box(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
emptyBox();
}
}
public class MaterialBox extends Box{
private String attribute;
public void setAttribute(String attribute) {this.attribute = attribute;};
public String getAttribute() {return attribute;};
public MaterialBox(int x, int y, int z, String attribute) {
setX(x);
setY(y);
setZ(z);
this.attribute = attribute;
}
public void printInfo() {
System.out.println("가로: " + getX() + ", 세로: " + getY() + ", 높이: " + getZ());
System.out.println("재질: " + getAttribute());
System.out.println("부피: " + getVolume());
System.out.println("무게: " + getWeight() + "\n");
}
}
public class Practice_63 {
public static void main(String[] args) {
MaterialBox box1 = new MaterialBox(2, 3, 4, "wood");
MaterialBox box2 = new MaterialBox(10, 5, 5, "paper");
System.out.println("box1의 정보입니다.");
box1.printInfo();
System.out.println("box2의 정보입니다.");
box2.printInfo();
}
}
결과:
반응형'코딩 > java' 카테고리의 다른 글
랜덤 주사위 (같은 수가 나올 때까지) (0) 2019.03.03 package 연습 (0) 2019.03.03 상자 정보 (상속) (0) 2019.02.10 학생 정보(상속) (0) 2019.02.10 동물 (상속 연습) (0) 2019.02.10