问题描述
我在超类中有一个重载方法的基本继承情况。
I have a basic inheritance situation with an overloaded method in the super class.
public class Person {
private String name;
private int dob;
private String gender;
public Person(String theName, int birth, String sex){
name = theName;
dob = birth;
gender = sex;
}
public void work(){
getWorkDetail(this);
}
public void getWorkDetail(Employee e){
System.out.println("This person is an Employee");
}
public void getWorkDetail(Person p){
System.out.println("This person is not an Employee");
}
}
以下员工
class扩展上面的 Person
类:
The following Employee
class extends the Person
class above:
public class Employee extends Person {
String department;
double salary;
public Employee(String theName, int birth, String sex){
super(theName, birth, sex);
department = "Not assigned";
salary = 30000;
}
}
主要方法只创建员工
对象(包括静态和动态类型)并在其上调用 .work()
:
The main method simply creates an Employee
object (both static and dynamic type) and calls .work()
on it:
public static void main(String[] args){
Employee e1 = new Employee("Manager1", 1976, "Female");
e1.work();
}
最终打印
通过这个我认为,因为对象 e1
的静态和动态类型都是 Employee
它会在Person中调用重载方法,该方法将 Employee
作为参数。由于我明显错误,我打开了一个调试器,假设 getWorkDetail(this)
中的 Person 引用了this c $ c> class必须变形为超类。然而,这不是我发现的。
Looking through this I had thought that since both the static and dynamic type of the object e1
is Employee
it would call the overloaded method in Person that takes an Employee
as a parameter. Since I am clearly wrong about this I opened a debugger assuming the reference to "this" at the line getWorkDetail(this)
in the Person
class must have morphed to it's super class. However this is not what I found.
显然此时此刻在代码中,
是一个 Employee
对象,但它仍然选择执行重载方法 getWorkDetail(Person p)
。任何人都可以解释这种行为吗?
Clearly at this point in the code this
is an Employee
object, however it still chose to execute the overloaded method getWorkDetail(Person p)
. Can anyone explain this behavior?
推荐答案
与方法覆盖不同,方法重载是基于静态类型链接的。在这种情况下, getWorkDetail(this)
in Person
只知道 Person
type。
Unlike method overrides, method overloads are linked based on the static type. And in this case, getWorkDetail(this)
in Person
only knows about the Person
type.
方法重载不是为了提供动态运行时行为而设计的。
Method overloading is not designed to provide dynamic runtime behavior.
要采取动态绑定的优点,您可能需要重新设计代码以覆盖方法,而不是:
To take advantage of dynamic binding, you may need to redesign your code to override the methods, instead:
public static void main(String[] args) throws IOException {
new Employee("Manager1", 1976, "Female").getWorkDetail();
new Person("Manager1", 1976, "Female").getWorkDetail();
}
并根据实现类修改行为。当然,只要你需要重写重载方法,你也可以重载方法。如果需要的话。
And modify behavior based on implementing classes. Of course, you can overload methods, as long as you take care of overriding the overloaded methods too, if required.
class Person {
private String name;
private int dob;
private String gender;
public Person(String theName, int birth, String sex) {
name = theName;
dob = birth;
gender = sex;
}
public void getWorkDetail() {
System.out.println("This person is not an Employee");
}
}
class Employee extends Person {
String department;
double salary;
public Employee(String theName, int birth, String sex) {
super(theName, birth, sex);
department = "Not assigned";
salary = 30000;
}
public void getWorkDetail() {
System.out.println("This person is an Employee");
}
}
这篇关于在Java中调用哪个重载方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!