1. class Pen
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 | package pen; class Pen { private int amount; // 잔여량 private int width; // 펜의 굵기 public Pen() {} public Pen(int amount, int width) { this.amount = amount; this.width = width; } public String getInfo() { return "잔여량: " + amount + ", 펜의 굵기: " + width; } public void refill(int amount) { this.amount += amount; } // get, set method public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } } public class PenMain { public static void main(String[] args) { SharpPencil s = new SharpPencil(10, 1); System.out.println("SharpPencil: " + s.getInfo()); BallPen b = new BallPen(15, 2, "black"); b.refill(10); System.out.println("BallPen: " + b.getInfo()); FountainPen f = new FountainPen(15, 2, "blue"); System.out.println("FountainPen: " + f.getInfo()); } } | cs |
2. class SharpPencil extends Pen
1 2 3 4 5 6 7 8 9 | package pen; public class SharpPencil extends Pen { public SharpPencil() {} public SharpPencil(int amount, int width) { super(amount, width); } } | cs |
3. class BallPen extends Pen
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package pen; public class BallPen extends Pen { private String color; public BallPen() {} public BallPen(int amount, int width, String color) { super(amount, width); this.color = color; } @Override public String getInfo() { return super.getInfo() + ", 색상: " + color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } } | cs |
4. class FountainPen extends BallPen
1 2 3 4 5 6 7 8 9 | package pen; public class FountainPen extends BallPen { public FountainPen() {} public FountainPen(int amount, int width, String color) { super(amount, width, color); } } | cs |
'JAVA > JAVA2' 카테고리의 다른 글
JAVA2_day04 | 추가공부 (예외 처리, Throwable 클래스, 스택) (0) | 2018.01.16 |
---|---|
JAVA2_day04 | 예외처리 (Exception Handling) (0) | 2018.01.16 |
JAVA2_day03 | 인터페이스 (Interface) (0) | 2018.01.12 |
JAVA2_day03 | 추상화 (Abstract) (0) | 2018.01.12 |
JAVA2_day03 | final 제어자 (0) | 2018.01.12 |