ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 상자 정보 (상속 super)
    코딩/java 2019. 3. 4. 23:08
    반응형

    public class Box {

    private int x;

    private int y;

    private int z;

    private boolean empty;

     

    public String toString() {

    String rslt = "가로: " + x + "cm 세로: " + y + "cm 높이: " + z + "cm";

     

    /*if (empty)

    return rslt + "지금 박스는 비어있습니다.\n";

    else

    return rslt + "지금 박스에는 물건이 들어있습니다.\n";*/

     

    return rslt;

      }

     

    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(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) {

    super(x, y, z);

    this.attribute = attribute;

     }

     

    public void printInfo() {

    System.out.println(toString());

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

    System.out.println("부피: " + getVolume());

    System.out.println("무게: " + getWeight() + "\n"); 

     }

    }

     

     

    public class Practice_70 {

     

    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.08
    학생 정보 출력 (super)  (0) 2019.03.08
    사각형, 도형 정보 출력  (0) 2019.03.03
    음식 정보 출력(package)  (0) 2019.03.03
    음식 정보 출력  (0) 2019.03.03
Designed by Tistory.