코딩/java

상자 정보 (상속)

런던전통손만두 2019. 2. 10. 21:07
반응형

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 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 class Practice_62 {

 

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의 정보입니다.");

System.out.println("가로: " + box1.getX() + ", 세로: " + box1.getY() + ", 높이: " + box1.getZ());

System.out.println("재질: " + box1.getAttribute());

System.out.println("부피: " + (box1.getX() * box1.getY() * box1.getZ()));

System.out.println("무게: " + 1.1 * (box1.getX() * box1.getY() * box1.getZ()) + "\n");

 

 

System.out.println("box1의 정보입니다.");

System.out.println("가로: " + box2.getX() + ", 세로: " + box2.getY() + ", 높이: " + box2.getZ());

System.out.println("재질: " + box2.getAttribute());

System.out.println("부피: " + (box2.getX() * box2.getY() * box2.getZ()));

System.out.println("무게: " + 1.1 * (box2.getX() * box2.getY() * box2.getZ()));

 

}

 

}

 

 

결과:

 

반응형