Skip to content

Commit

Permalink
[Week3][Chap11] 클래스와 인터페이스 활용 (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
arinming committed Feb 6, 2024
1 parent 35facc8 commit c840abb
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 김아린/java-basic/src/poly/ex6/AbstractAnimal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package poly.ex6;

public abstract class AbstractAnimal {
public abstract void sound();

public void move() {
System.out.println("동물이 이동합니다.");
}
}
13 changes: 13 additions & 0 deletions 김아린/java-basic/src/poly/ex6/Bird.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package poly.ex6;

public class Bird extends AbstractAnimal implements Fly{
@Override
public void sound() {
System.out.println("짹짹");
}

@Override
public void fly() {
System.out.println("새 날기");
}
}
13 changes: 13 additions & 0 deletions 김아린/java-basic/src/poly/ex6/Chicken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package poly.ex6;

public class Chicken extends AbstractAnimal implements Fly {
@Override
public void sound() {
System.out.println("꼬끼오");
}

@Override
public void fly() {
System.out.println("닭 날기");
}
}
8 changes: 8 additions & 0 deletions 김아린/java-basic/src/poly/ex6/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package poly.ex6;

public class Dog extends AbstractAnimal {
@Override
public void sound() {
System.out.println("멍멍");
}
}
5 changes: 5 additions & 0 deletions 김아린/java-basic/src/poly/ex6/Fly.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package poly.ex6;

public interface Fly {
void fly();
}
29 changes: 29 additions & 0 deletions 김아린/java-basic/src/poly/ex6/SoundFlyMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package poly.ex6;

public class SoundFlyMain {
public static void main(String[] args) {
Dog dog = new Dog();
Bird bird = new Bird();
Chicken chicken = new Chicken();

soundAnimal(dog);
soundAnimal(bird);
soundAnimal(chicken);

flyAnimal(bird);
flyAnimal(chicken);
}

// AbstractAnimal 사용 가능
private static void soundAnimal(AbstractAnimal animal) {
System.out.println("동물 소리 테스트 시작");
animal.sound();
System.out.println("동물 소리 테스트 종료");
}

// Fly 인터페이스가 있으면 사용 가능
public static void flyAnimal(Fly fly) {
System.out.println("날기 테스트 시작");
fly.fly();
}
}

0 comments on commit c840abb

Please sign in to comment.