티스토리 뷰

반응형

⊙ 문제

 

다음 표와 실행 결과를 참고해 자식 클래스인 Circle과 ColoredCircle을 작성하시오. 그리고 Circle과 ColoredCircle 객체의 show() 메서드를 호출하는 테스트 프로그램도 작성하시오.

 

클래스

Circle

ColoredCircle

필드

int radius

String color

메서드

void show()

void  show()

생성자

Circle(int radius)

ColoredCircle(int radius, String color)

 

 


 

⊙ 문제 접근 과정

 

클래스를 만들고 자식 클래스를 설정해주자

 

 


 

⊙ 문제 풀이

 

class Circle {
    int radius;
    Circle(int radius) {
        this.radius = radius;
    }
    void show() {
        System.out.println("반지름이 " + radius + "인 원이다.");
    }
}
class ColoredCircle extends Circle {
    String color;

    ColoredCircle(int radius, String color) {
        super(radius);
        this.color=color;
    }
    void show() {
        System.out.println("반지름이 " + radius + "인 " + color + " 원이다.");
    }
}

public class Main {
    public static void main(String[] args) {
        Circle c1 = new Circle(5);
        ColoredCircle c2 = new ColoredCircle(10,"빨간색");

        c1.show();
        c2.show();
    }
}

 

 


⊙ 결과

 

 


⊙ 마무리

 

NONE

 

좋아요 로그인하지 않아도 누를 수 있습니다!

 

728x90
반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함