객체지향 프로그래밍을 통해 아래와 같은 출력결과를 갖는 원과 삼각형 좌표를 찍는 예제를 작성
출력결과로부터 색깔, 포인트, 삼각형, 원의 클래스를 도출
class Color
{
String color = "black";
void printColor()
{
System.out.println("색깔은" + color + "입니다");
}
}
>> 인스턴스변수 color 한 개와 printColor()메소드 한 개를 갖는 Color 클래스 생성
class Point
{
int x, y;
Point()
{
this(0,0);
}
Point(int x, int y)
{
this.x = x;
this.y = y;
}
String getPoint()
{
return "(" + x + ", " + y + ")";
}
}
>> 인스턴스변수 x, y와 getPoint() 메소드를 갖는 Point 클래스 생성
>> 생성자를 지정하여 매개변수가 없을 때는 x와 y를 0으로 초기화
>> 생성자를 지정하여 매개변수 x, y가 주어질 때는 이 값을 인스턴스변수 x, y에 대입
class Circle extends Color
{
Point center;
int r;
Circle()
{
this(new Point(), 100);
}
Circle(Point center, int r)
{
this.center = center;
this.r = r;
}
void printCircle()
{
System.out.println("Circle의 center = " + center.getPoint() + ", r = " + r + ", color = " + color);
}
}
>> Point객체 타입 인스턴스변수 center, int타입 인스턴스변수 r, printCircle() 메소드를 갖는 Circle 클래스 생성
>> Circle 클래스는 Color의 인스턴스 변수를 사용하기 위해 Color 클래스 상속
>> center와 r을 매개변수로 받는 생성자를 지정하여, 받은 값을 인스턴스변수에 대입
class Triangle extends Color
{
Point[] p;
Triangle(Point[] p)
{
this.p = p;
}
void printTriangle()
{
System.out.println("Triangle의 point = " + p[0].getPoint() +", "+ p[1].getPoint() +", "+p[2].getPoint() + ", color = " + color);
}
}
>> Point[]객체 배열 타입 인스턴스변수 p와 printTriangle() 메소드를 갖는 Triangle 클래스 생성
>> Triangle 클래스는 Color의 인스턴스 변수를 사용하기 위해 Color 클래스 상속
>> 생성자를 통해, 매개변수로 받은 Point[]객체 배열 시작주소값을 인스턴스변수 p에 대입
public static void main(String[] args)
{
Point[] p = {new Point(50, 100), new Point(100, 150), new Point(150, 200)};
Triangle t = new Triangle(p);
t.printTriangle();
Circle c = new Circle(new Point(10, 20), 50);
c.printCircle();
}
>> Triangle 객체 생성에 앞서, 생성자에서 Point[] 객체 배열 타입을 매개변수를 요구하기 때문에
Point[] 객체 배열 p 생성
>> p값을 인수로 넘겨 Triangle 인스턴스 t 생성
>> Point객체와 r 값을 인수로 넘겨 Circle 인스턴스 c 생성
>> 각 인스턴스가 갖는 메소드 printTriangle()과 prinCircle() 실행
전체코드
package sample;
public class Sample
{
public static void main(String[] args)
{
Point[] p = {new Point(50, 100), new Point(100, 150), new Point(150, 200)};
Triangle t = new Triangle(p);
t.printTriangle();
Circle c = new Circle(new Point(10, 20), 50);
c.printCircle();
}
}
class Color
{
String color = "black";
void printColor()
{
System.out.println("색깔은" + color + "입니다");
}
}
class Point
{
int x, y;
Point()
{
this(0,0);
}
Point(int x, int y)
{
this.x = x;
this.y = y;
}
String getPoint()
{
return "(" + x + ", " + y + ")";
}
}
class Circle extends Color
{
Point center;
int r;
Circle()
{
this(new Point(), 100);
}
Circle(Point center, int r)
{
this.center = center;
this.r = r;
}
void printCircle()
{
System.out.println("Circle의 center = " + center.getPoint() + ", r = " + r + ", color = " + color);
}
}
class Triangle extends Color
{
Point[] p;
Triangle(Point[] p)
{
this.p = p;
}
void printTriangle()
{
System.out.println("Triangle의 point = " + p[0].getPoint() +", "+ p[1].getPoint() +", "+p[2].getPoint() + ", color = " + color);
}
}
'자바 JAVA' 카테고리의 다른 글
생성자 매개변수 다 넘겼는데 객체 만들 때 오류? super() 예약어! (0) | 2021.06.09 |
---|---|
오버라이딩 overriding 기초 예제 (0) | 2021.06.08 |
자바 공부 잊지말아야 할 내용 정리 1편 (0) | 2021.06.06 |
2차원 배열을 이용한 예제 풀이 - 자바 기초 (0) | 2021.05.26 |
간단한 번호추첨 프로그램 작성 예제 - 자바 기초 (0) | 2021.05.24 |