如果我有一个SubOfParent类,它是Parent的子类,有两种方法:
public static void doStuff(Parent in) {} public static void doStuff(SubOfPArent in) {}
当我传递一个SubOfParent类型的对象时,为什么第一个doStuff被调用?
感谢您对此的任何见解!
解决方法
Parent p = new SubOfParent(); SubOfParent s = new SubOfParent(); doStuff(p); //calls doStuff(Parent in) doStuff(s); //calls doStuff(SubOfParent in) //If you're working with a parent but need to call the subclass,you need to upcast it. dostuff(p as SubOfParent);