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 PositivePoint extends Point {
public PositivePoint() {
// TODO Auto-generated constructor stub
super(0,0);
}
public PositivePoint(int x, int y) {
super(x,y);
if(getX()>=0&&getY()>=0) {
super.move(x, y);
}
else {
super.move(0, 0);
}
}
public void move(int x,int y) {
if(x>=0&&y>=0) {
super.move(x,y);
}
}
public String toString() {
return "("+getX()+","+getY()+")의 점";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
PositivePoint p = new PositivePoint();
p.move(10, 10);
System.out.println(p.toString()+"입니다.");
p.move(-5, -5); //객체 p는 음수 공간으로 이동하지 않음
System.out.println(p.toString()+"입니다.");
PositivePoint p2 = new PositivePoint(-10,-10);
System.out.println(p2.toString()+"입니다.");
}
}
결과
(10,10)의 점입니다.
(10,10)의 점입니다.
(0,0)의 점입니다.
'명품JAVA프로그래밍 > 5장 상속' 카테고리의 다른 글
[명품JAVA프로그래밍] 5장 실습문제 10번 (0) | 2022.01.13 |
---|---|
[명품JAVA프로그래밍] 5장 실습문제 9번 (0) | 2022.01.13 |
[명품JAVA프로그래밍] 5장 실습문제 7번 (0) | 2022.01.12 |
[명품JAVA프로그래밍] 5장 실습문제 6번 (0) | 2022.01.12 |
[명품JAVA프로그래밍] 5장 실습문제 6번 (0) | 2022.01.12 |