본문 바로가기

학습공간/JAVA, 객체지향프로그래밍

[4주차] 상속

반응형

객체지향프로그래밍 5장 상속

실습문제 1번 프린터 출력 시 용지 감소

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
public class Inkget_Printer extends Printer{        // Inkget_Printer.java
    private int ink;
    
    public void print()
    {
        paper--;
        ink--;
        paper_check();
    }
    public Inkget_Printer(){
        ink = 100;
        model_name = "Samsung printer";
        manufacture = "epson";
        interface_type = "USB";
        number_of_print = 1;
        
    }
    public void paper_check(){
         System.out.println("InkgetPrint의 남은 용지의 수는 "+paper+"장 입니다.");
    }
    public void display()
    {
        System.out.println("모델명 : "+model_name);
        System.out.println("제조사 : "+manufacture);
        System.out.println("인터페이스 종류 : "+interface_type);
        System.out.println("인쇄매수 : "+number_of_print);
        System.out.println("잉크잔량 : "+ink);
        System.out.println();
    }
}
 
public class Laser_Printer extends Printer{        // Laser_Printer.java
    private int toner;
    public void print()
    {
        paper--;
        toner--;
        paper_check();
    }
    public Laser_Printer(){
        toner = 100;
        model_name = "hp printer";
        manufacture = "MicroSoft";
        interface_type = "병렬 인터페이스";
        number_of_print = 1;
    }
    public void paper_check(){
         System.out.println("LaserPrint의 남은 용지의 수는 "+paper+"장 입니다.");
    }
    public void display()
    {
        System.out.println("모델명 : "+model_name);
        System.out.println("제조사 : "+manufacture);
        System.out.println("인터페이스 종류 : "+interface_type);
        System.out.println("인쇄매수 : "+number_of_print);
        System.out.println("토너잔량 : "+toner);
        System.out.println();
    }
}
 
public class Printer {                    // Printer.java
    protected String model_name;
    protected String manufacture;
    protected String interface_type;
    protected int number_of_print = 1;
    protected int paper;
    public void print(){
        paper--;
        paper_check();
    }
    public Printer(){
        paper = 100;
    }
    public void paper_check(){
         System.out.println("Print Test : 남은 용지의 수는 "+paper+"장 입니다.");
    }
    public static void main(String[] args) {
        Printer p = new Printer();
 
        p.print();    // Print Test
        System.out.println();
 
        
        Printer l = new Laser_Printer();        // 업캐스팅
 
        l.print();        // 오버라이딩
 
        Laser_Printer L;
        L = (Laser_Printer)l;            // 다운캐스팅
 
        l.print();        // 오버라이딩
        
        L.display();
                // l.로는 접근 못하는 서브클래스의 display()메소드를 L.로 접근    
        
        Inkget_Printer I = new Inkget_Printer();
        I.display();
        I.print();
        I.print();
        I.print();
        I.display();
    }
}
 
cs

실습문제 3번 스택

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public interface Stack {                    // Stack.java
    public int length();
    public Object pop();
    public boolean push(Object ob);
}
 
public class StringStack implements Stack{        // StringStack.java
    protected String [] stack = new String [10];
    private int top = -1;
    
    @Override
    public int length() {
        System.out.print("현재 스택의 길이 : ");
        return top+1;
    }
 
    @Override
    public Object pop() {
        if(top == -1){
            System.out.print("스택이 비었습니다 ---->");
            return null;
        }
        else{
            System.out.print("꺼낸 데이터 값 : ");
            return this.stack[top--];
        }
    }
 
    @Override
    public boolean push(Object ob) {
        if(top == 9){
            System.out.println("데이터 포화 상태");
            return false;
        }
        else{
            this.stack[++top] = ob.toString();
            return true;    
        }
    }
}
public class StackTest {                // StackTest.java
    public static void main(String[] args) {
 
        StringStack s1 = new StringStack();
        s1.push("abc");
        s1.push("def");
        
        System.out.println(s1.pop());        // 꺼냄
        System.out.println(s1.length());        // 현재길이
        System.out.println(s1.pop());
        System.out.println(s1.length());
        System.out.println(s1.pop());    // empty
        System.out.println(s1.length());    //
        
        s1.push("A");
        s1.push("B");
        s1.push("C");
        s1.push("D");
        s1.push("E");
        s1.push("F");
        s1.push("G");
        s1.push("H");
        s1.push("I");
        s1.push("J");
        s1.push("K");    // full
        s1.push("L");    // full
        
        System.out.println(s1.pop());        // 꺼냄
        System.out.println(s1.length());        // 현재길이
        System.out.println(s1.pop());        // 꺼냄
        System.out.println(s1.length());        // 현재길이
    }
}
 
cs

실습문제 5번 좌표 클래스를 상속받고 컬러 속성을 추가

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
abstract class MyPoint {                // MyPoint.java
    int x;
    int y;
    public MyPoint(int x, int y){
        this.x = x; this.y = y;
    }
    protected abstract void move(int x, int y);
    protected abstract void reverse();
    protected void show(){
        System.out.println(x+","+y);
    }
 
}
 
public class MyColorPoint extends MyPoint{        // MyColorPoint.java
    private String color;
    public MyColorPoint(int x, int y, String color){
        super(x,y);
        this.x = x;    this.y = y;
        this.color = color;
    }
    protected void move(int x, int y){
        this.x = x;    this.y = y;
    }
    protected void reverse(){
        int tmp;    
        tmp = x;
        this.x = y;    
        this.y = tmp;
    }
    protected void show(){
        System.out.println(x+","+y+","+color);
    }
    public static void main(String[] args) {
        MyPoint p = new MyColorPoint(2,3,"blue");    // 업캐스팅
        p.move(34);
        p.reverse();
        p.show();
    }
}
 
cs

반응형