Skip to content

Commit

Permalink
[Week3][Chap11] 다형성 활용2 (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip committed Feb 13, 2024
1 parent 9f0abf0 commit 26b88e8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 임준형/java-basic/src/main/java/week3/poly/ex2/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package week3.poly.ex2;

public class Animal {
public void sound() {
System.out.println("동물 울음 소리");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package week3.poly.ex2;

import java.util.ArrayList;
import java.util.List;

public class AnimalSoundMain1 {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
Caw caw = new Caw();

soundAnimal(dog);
soundAnimal(cat);
soundAnimal(caw);

// 혼자해본 연습
/*
Animal dog1 = new Dog();
Animal cat1 = new Cat();
Animal caw1 = new Caw();
List<Animal> animals = new ArrayList<>();
animals.add(dog1);
animals.add(cat1);
animals.add(caw1);
animals.forEach(animal -> soundAnimal(animal));
*/
}

private static void soundAnimal(Animal animal) {
System.out.println("동물 소리 테스트 시작");
animal.sound();
System.out.println("동물 소리 테스트 종료");
}
}
9 changes: 9 additions & 0 deletions 임준형/java-basic/src/main/java/week3/poly/ex2/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package week3.poly.ex2;

public class Cat extends Animal {

@Override
public void sound() {
System.out.println("야옹");
}
}
8 changes: 8 additions & 0 deletions 임준형/java-basic/src/main/java/week3/poly/ex2/Caw.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package week3.poly.ex2;

public class Caw extends Animal{
@Override
public void sound() {
System.out.println("음메");
}
}
8 changes: 8 additions & 0 deletions 임준형/java-basic/src/main/java/week3/poly/ex2/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package week3.poly.ex2;

public class Dog extends Animal{
@Override
public void sound() {
System.out.println("멍멍");
}
}

0 comments on commit 26b88e8

Please sign in to comment.