개인공부
의미파악해보자 (추상클래스 / 인터페이스)
리승우
2022. 7. 31. 20:18
interface Flyable{
void fly(int x, int y, int z);
}
class PPigeon implements Flyable {
private int x, y, z;
@Override
public void fly(int x, int y, int z) {
printLocation();
System.out.println("이동합니다");
this.x = x;
this.y = y;
this.z = z;
printLocation();
}
public void printLocation() {
System.out.println("현재위치 {" + x + "," + y + "," + z + ")");
}
}
public class jogun {
public static void main(String[] args) {
Flyable pigeon = new PPigeon();
pigeon.fly(1,2,3);
}
}
������ġ {0,0,0)
�̵��մϴ�
������ġ {1,2,3)
abstract class Bird{
private int x,y,z;
void fly(int x, int y, int z) {
printLocation();
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+")");
}
}
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(1,1,3);
System.out.println("---공작새---");
peacock.fly(1,1,3);
System.out.println("---비둘기---");
pigeon.fly(3,3,30000);
}
}
---��ѱ�---
������ġ {0,0,0)
�̵��մϴ�
������ġ {1,1,3)
---���ۻ�---
������ġ {0,0,0)
�̵��մϴ�
�� ���̷δ� �� �� �����ϴ�
������ġ {1,1,0)
---��ѱ�---
������ġ {1,1,3)
�̵��մϴ�
�� ���̷δ� �� �� �����ϴ�
������ġ {3,3,3)