않을 것으로 예상됩니다 :혼합 유형 java 다형성. 출력은 다음 코드에서
class Payment { }
class CashPayment extends Payment{ }
class Store{
public void acceptPayment (Payment p)
{System.out.println ("Store::Payment");}
}
class FastFoodStore extends Store {
public void acceptPayment (CashPayment c)
{System.out.println ("FastFoodStore::CashPayment");}
public void acceptPayment (Payment p)
{System.out.println ("FastFoodStore::Payment");}
}
//
public class Example {
public static void main(String [] args){
Store store1 = new Store();
FastFoodStore sandwitchPlace = new FastFoodStore();
Store store2 = new FastFoodStore();
Payment p1 = new Payment();
CashPayment cp = new CashPayment();
Payment p2 = new CashPayment();
store1.acceptPayment (p1);
store1.acceptPayment (cp);
store1.acceptPayment (p2);
sandwitchPlace.acceptPayment (p1);
sandwitchPlace.acceptPayment (cp);
sandwitchPlace.acceptPayment (p2);
store2.acceptPayment (p1);
store2.acceptPayment (cp);
store2.acceptPayment (p2);
}
}
store2.acceptPayment (cp);
이 FastFoodStore :: CashPayment을 :: 지불 FastFoodStore를 표시하지만 이유 내가 정말 이해가 안입니까? store2는 기본적으로 런타임시 FastFoodStore의 메소드를 호출하고 CashPayment 유형 매개 변수를 전달합니다. 이 경우 FastFoodStore :: CashPayment가 표시됩니다. 누군가 도와 드릴 수 있습니까?
public void acceptPayment (CashPayment c) Store 클래스에 다른 메소드를 추가하면 store2.acceptPayment (cp); FastFoodStore :: CashPayment가 될 것인가? – user1865027
oh yea .... 그냥 일식에 그것을 테스트했습니다. 나는 마침내 지금 이해한다. 당신은 최고입니다! 정말 고맙습니다 – user1865027