abstract class Bird {
private int x, y, z;
void fly(int x, int y, int z) {
printLocationBefore();
System.out.println("이동합니당.");
this.x = x;
this.y = y;
if (flyable(z)) {
this.z = z;
} else {
System.out.println("그높이 안됨");
}
printLocation();
}
abstract boolean flyable(int z);
public void printLocation() {
System.out.println("현위치 : (" + x + "," + y + "," + z + ")");
System.out.println("======이동완료========");
}
public void printLocationBefore() {
System.out.println("이전위치 : (" + x + "," + y + "," + z + ")");
}
}
class Pigeon extends Bird {
@Override
boolean flyable(int z) {
return z < 10000;
}
}
class Peacock extends Bird {
@Override
boolean flyable(int z) {
return false;
}
}
public class Main {
public static void main(String[] args) {
Bird pigeon = new Pigeon();
Bird peacock = new Peacock();
System.out.println("비둘기");
pigeon.fly(4,5,2);
System.out.println("공작새");
peacock.fly(2,5,2);
System.out.println("비둘기");
pigeon.fly(1, 5, 12000);
}
}
비둘기
이전위치 : (0,0,0) 이동합니당.
현위치 : (4,5,2)
======이동완료========
공작새
이전위치 : (0,0,0)
이동합니당.
그높이 안됨
현위치 : (2,5,0)
======이동완료========
비둘기
이전위치 : (4,5,2)
이동합니당.
그높이 안됨
현위치 : (1,5,2)
======이동완료========
interface Flyable {
void fly(int x, int y, int z);
}
class Pigeon implements Flyable {
private int x, y, z;
@Override
public void fly(int x, int y, int z) {
printLocationBefore();
System.out.println("날아오릅니다.");
this.x = x;
this.y = y;
this.z = z;
printLocation();
}
public void printLocationBefore() {
System.out.println("이전위치 (" + x + "," + y + "," + z + ")");
}
public void printLocation() {
System.out.println("현위치 (" + x + "," + y + "," + z + ")");
}
}
public class Main {
public static void main(String[] args) {
Flyable pigeon = new Pigeon();
pigeon.fly(1, 2, 4);
}
}