package chapter20230823.test03;
public class Pet {
private String name;
private String masterName;
public Pet(String name, String masterName) {
this.name = name;
this.masterName = masterName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMasterName() {
return masterName;
}
public void introduce() {
System.out.println(" 내 이름은 " + name + " 입니다.");
System.out.println(" 주인님은 " + masterName + " 입니다.");
}
}
package chapter20230823.test03;
public class RobotPet extends Pet{
public RobotPet(String name, String masterName) {
super(name, masterName);
}
@Override
public void introduce() {
System.out.println(" 저는 로봇입니다. 이름은 " + getName() + " 입니다.");
System.out.println(" 주인님은 " + getMasterName() + " 입니다.");
}
public void work(int sw) {
switch (sw) {
case 0:
System.out.println("청소를 합니다.");
break;
case 1:
System.out.println("빨래를 합니다.");
break;
case 2:
System.out.println("밥을 짓습니다.");
break;
}
}
}
package chapter20230823.test03;
public class test01 {
public static void main(String[] args) {
Pet kurt = new Pet("kurt", "아이");
kurt.introduce();
System.out.println();
RobotPet r2d2 = new RobotPet("R2D2", "루크");
r2d2.introduce();
r2d2.work(0);
System.out.println();
Pet toy = new RobotPet("toy", "아이2");
toy.introduce();
System.out.println();
Pet pet = r2d2;
pet.introduce();
System.out.println();
r2d2.setName("아이로봇");
r2d2.introduce();
pet.introduce();
}
}
package chapter20230823.test03;
public class test02 {
static void intro(Pet pet) {
pet.introduce();
};
public static void main(String[] args) {
Pet[] pets = {
new Pet("Kurt" , "아이"),
new RobotPet("R2D2", "루크"),
new Pet("마이클", "영진"),
};
for(Pet pet : pets) {
intro(pet);
System.out.println();
}
}
}