首先,我虽然java的多态函数是由它的参数实例类型映射的.
请有人帮忙解释为什么我的函数没有调用myFunction(EmployeeImpl emp)签名实例EmployeeImpl.
public class MainApp {
public static void main(String[] args){
Employee emp = new EmployeeImpl();
emp.callMyFunction();
}
}
abstract class Employee{
public void callMyFunction(){
//here is huge amount of code, which all child class has the same
//excepted this line is called to a different function by it instant types.
EmployeeFacade.myFunction(this);
}
}
class EmployeeImpl extends Employee{
}
class EmployeeFacade{
public static void myFunction(Employee emp){
//same data to database
System.out.println("Employee: "+ emp.getClass().getName());
}
public static void myFunction(EmployeeImpl emp){
//same data to database
System.out.println("EmployeeImpl: "+ emp.getClass().getName());
}
}
Run Code Online (Sandbox Code Playgroud)
结果:员工:EmployeeImpl
编辑:这只是一个示例应用程序,具有与我的现实应用程序相同的结构,它有20多个包含相同函数的子类callMyFunction,这个函数有超过20行代码.因此,对于我来说,override使用所有子类的相同代码进行此函数是非常困难的.无论如何,如果我需要改变未来的功能会怎样?我会用相同的代码更改所有20个功能吗?
有没有比这更容易的?
| 归档时间: |
|
| 查看次数: |
504 次 |
| 最近记录: |