package kr.or.kihd.inheritance2;
//조상 부모 클래스
public class Shape {
String color ="black";
public void draw() {
System.out.println("draw()");
}
}
package kr.or.kihd.inheritance2;
//독립된 클래스
public class Point {
int x;
int y;
//기본 생성자
public Point() {
this(0,0);
}
//매개변수가 있는 생성자
public Point(int x, int y) {
System.out.println("Point 클래스의 매개변수가 있는 생성자 호출");
this.x = x;
this.y = y;
}
}
package kr.or.kihd.inheritance2;
public class Circle extends Shape{
Point center; //원점
int radius; //반지름
public Circle() {
this(new Point(0,0),100);
}
public Circle(Point center, int radius) {
System.out.println("Circle클래스의 멤버변수가 있는 생성자 호출");
this.center = center;
this.radius = radius;
}
@Override
public void draw() {
System.out.println("색깔 : " +this.color);
System.out.println("원점 : (x : " + this.center.x +
", y : " + this.center.y+
", 반지름 : " + this.radius + ")");
}
}
package kr.or.kihd.inheritance2;
public class ShapeTest {
public static void main(String[] args) {
Circle circle = new Circle();
circle.draw();
System.out.println();
Circle circle2 = new Circle(new Point(150, 150), 500);
circle2.draw();
System.out.println();
Triangle triangle = new Triangle();
triangle.draw();
System.out.println();
Point[] point = new Point[] {new Point(10,10),
new Point(10,10),
new Point(10,10),};
Triangle triangle2 = new Triangle(point);
triangle2.draw();
}
}
package kr.or.kihd.inheritance2;
public class Triangle extends Shape{
Point[] point;
public Triangle() {
this(new Point(0,0),new Point(50,50),new Point(100,100));
}
public Triangle(Point point1, Point point2 , Point point3) {
System.out.println("Triangle의 매개변수 3개 호출됨");
this.point = new Point[] {point1, point2, point3};
}
public Triangle(Point[] point) {
System.out.println("Triangle의 매개변수 point 호출됨");
this.point = point;
}
@Override
public void draw() {
System.out.println("[point1] : " + this.point[0].x +
", "+ this.point[0].y);
System.out.println("[point2] : " + this.point[1].x +
", "+ this.point[1].y);
System.out.println("[point3] : " + this.point[2].x +
", "+ this.point[2].y);
}
}