Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

고마구의 개발 블로그

240620 9주차 목요일 - JAVA 19 본문

KDT풀스택과정 공부

240620 9주차 목요일 - JAVA 19

고마구 2024. 6. 20. 10:41

public class PlayGame {

private Pet character=null;

System.out.println("1.피카츄 2.고양이");

String ch=sc.nextLine();

if(ch.equals("1")) {

character=new Pikachu();

}else if(ch.equals("2")) {

character=new Cat();

}

불러와서 이런식으로 사용됨

character.levelUp();

character.printInfo();

 

==================================

ArrayList<Pet> pArrs2=new ArrayList<Pet>();

pArrs2.add(new Pet());

pArrs2.add(new Cat());

pArrs2.add(new Dog());

//pArrs2는 pet,cat,dog재정의를 통해서 객체가 다르면 다른 메소드를 실행할 수 있게 해준다.

for(int i=0;i<pArrs2.size();i++) {

pArrs2.get(i).eat();

}