나중에 다른 동물로 변경할 수 있습니다. 예를 들어
)
Animal pet = new Dog();
// you do some thing else .. assume your pet is currently dog and your apartment can collect this detail as an animal..
// now your dog ran away :D and you have cat as your pet
// all you have do is just change the variable to point to cat instance
pet = new Cat(); //ofcourse Cat should inherit Animal
그래서 자사의 유연성
이제 가정 pets
예)
public Pass issuePass(Animal pet)
{
return new Pass(pet.gettype(),pet.getowner(),pet.getname());
}
//you dont need to change this method.
//otherwise you need to create new methods unneccesarily
public Pass issuePass(Dog pet)
{
return new Pass(pet.gettype(),pet.getowner(),pet.getname());
}
public Pass issuePass(Cat pet)
{
return new Pass(pet.gettype(),pet.getowner(),pet.getname());
}
'Max'는'Animal' 인스턴스가 예상되는 곳이면 어디서든 사용할 수 있습니다. –