在以下代码中:
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::Payment 但不显示 FastFoodStore::CashPayment?store2 基本上会在运行时调用 FastFoodStore 中的方法并传递一个 CashPayment 类型的参数。在这种情况下,将显示 FastFoodStore::CashPayment。有人可以帮忙吗?