gpt4 book ai didi

java - 多级继承默认构造函数

转载 作者:行者123 更新时间:2023-12-02 06:41:06 28 4
gpt4 key购买 nike

父类是...

public class UMember {
private String first;
private String last;
private String street;
private String city;
private String state;
private String zipcode;

public UMember() {
this("na", "na", "na", "na", "na", "na");
}

public UMember(String first, String last, String street,
String city, String state, String zipcode){
set(first, last, street, city, state, zipcode);
}

private void set(String first, String last, String street,
String city, String state, String zipcode){
this.first = first;
this.last = last;
this.street = street;
this.city = city;
this.state = state;
this.zipcode = zipcode;
}

public void setUMember(String first, String last, String street,
String city, String state, String zipcode) {
set(first, last, street, city, state, zipcode);
}

UMember 的父类是...

public class Employee extends UMember {
private int eid;
private String doh;
private String dept;
private double salary;

public Employee() {
this(0, "na", "na", 0.0, "na", "na", "na", "na", "na", "na");
}

public Employee(int eid, String doh, String dept, double salary) {
this(eid, doh, dept, salary, "na", "na", "na", "na", "na", "na");
}



public Employee(int eid, String doh, String dept, double salary,
String first, String last, String street, String city,
String state, String zipcode) {
super(first, last, street, city, state, zipcode);
set(eid, doh, dept, salary);
}

private void set(int eid, String doh, String dept, double salary) {
this.setEid(eid);
this.setDoh(doh);
this.setDept(dept);
this.setSalary(salary);
}

private void setEmployee(int eid, String doh, String dept, double salary,
String first, String last, String street, String city,
String state, String zipcode) {
setUMember(first, last, street, city, state, zipcode);
set(eid, doh, dept, salary);
}

Employee的子类是...

public class Faculty extends Employee{
private String rank;
private int annualTeachingLoad;

public Faculty() {
this("na", 0, 0, "na", "na", 0.0,
"na", "na", "na", "na", "na", "na");
}

public Faculty(String rank, int annualTeachingLoad) {
this(rank, annualTeachingLoad, 0, "na", "na", 0.0,
"na", "na", "na", "na", "na", "na");
}

Faculty 子类中的上述默认构造函数都不正确。我不明白为什么。我首先使用“Faculty”的默认值,然后是“Employee”,最后是“UMmember”。任何关于为什么这两段代码都不正确的帮助将不胜感激。

最佳答案

您正在使用 this 而不是 super 调用构造函数。使用 this 调用构造函数只能调用在同一个类中声明的构造函数,而 super 用于调用父类(super class)中的构造函数。

关于java - 多级继承默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19129261/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com