class Point {
private int x,y;
public Point(int x,int y) {this.x = x; this.y = y;}
public int getX() {return x;}
public int getY() {return y;}
protected void move(int x,int y) {this.x= x; this.y = y;}
}
public class ColorPoint extends Point {
private String color;
public ColorPoint(int x, int y,String color) {
super(x, y);
this.color = color;
}
public ColorPoint() {
super(0,0);
color = "BLACK";
}
void setXY(int x, int y) {
move(x,y);
}
void setColor(String color) {
this.color = color;
}
public String toString() {
return color+"색의 "+"("+getX()+","+getY()+")";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ColorPoint zeroPoint = new ColorPoint();
System.out.println(zeroPoint.toString()+"입니다.");
ColorPoint cp = new ColorPoint(5,5,"YELLOW");
cp.setXY(10,20);
cp.setColor("RED");
String str = cp.toString();
System.out.println(str+"입니다.");
}
}
결과
BLACK색의 (0,0)입니다.
RED색의 (10,20)입니다.
'명품JAVA프로그래밍 > 5장 상속' 카테고리의 다른 글
[명품JAVA프로그래밍] 5장 실습문제 7번 (0) | 2022.01.12 |
---|---|
[명품JAVA프로그래밍] 5장 실습문제 6번 (0) | 2022.01.12 |
[명품JAVA프로그래밍] 5장 실습문제 5번 (0) | 2022.01.12 |
[명품JAVA프로그래밍] 5장 실습문제 4번 (0) | 2022.01.12 |
[명품JAVA프로그래밍] 5장 실습문제 3번 (0) | 2022.01.12 |