javaeclipse中(2)Employee类中的属性有:办公室office(String类型),工资wage(double类型) ,创建showemployee方法用于显示本类中的office和wage属性值。创建其构造方法,为这二个属性进行赋值。( 但是子类的构造方法在执行时需要先执行父类的构造方法,此时该 怎么处理,由大家想办法、讨论出一个可行的方法。)
时间: 2024-03-02 17:51:32 浏览: 91
好的,我来为您演示一下:
```java
public class Employee extends Person {
private String office;
private double wage;
public Employee(String name, String address, String telphone, String office, double wage) {
super(name, address, telphone);
this.office = office;
this.wage = wage;
}
public void showEmployee() {
System.out.println("Office: " + office);
System.out.println("Wage: " + wage);
}
}
```
在这个类中,我们新增了两个属性:办公室和工资。我们也定义了一个构造方法,用于初始化这两个属性,并调用父类的构造方法。这里的调用父类的构造方法,我们使用了super关键字来实现。
我们还新增了一个showEmployee方法,用于显示本类中的office和wage属性值。注意,这里我们没有调用父类的showInfo方法,因为这个方法只显示了Person类中的属性信息,而我们需要显示Employee类中的属性信息。
希望这个演示能够帮助到您!
相关问题
实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty和Staff。 具体要求如下: (1)Person类中的属性有:姓名name(String类型),地址address(String类型),电话号码telphone(String类型)和电子邮件地址email(String类型); (2)Employee类中的属性有:办公室office(String类型),工资wage(double类型),受雇日期hiredate(String类型); (3)Faculty类中的属性有:学位degree(String类型),级别level(String类型); (4)Staff类中的属性有:职务称号duty(String类型);Python、代码
以下是Python中实现Person、Employee、Faculty和Staff类的代码:
```python
class Person:
def __init__(self, name, address, telphone, email):
self.name = name
self.address = address
self.telphone = telphone
self.email = email
class Employee(Person):
def __init__(self, name, address, telphone, email, office, wage, hiredate):
super().__init__(name, address, telphone, email)
self.office = office
self.wage = wage
self.hiredate = hiredate
class Faculty(Employee):
def __init__(self, name, address, telphone, email, office, wage, hiredate, degree, level):
super().__init__(name, address, telphone, email, office, wage, hiredate)
self.degree = degree
self.level = level
class Staff(Employee):
def __init__(self, name, address, telphone, email, office, wage, hiredate, duty):
super().__init__(name, address, telphone, email, office, wage, hiredate)
self.duty = duty
```
使用方法:
```python
person = Person("Tom", "123 Main St.", "555-1234", "tom@email.com")
employee = Employee("John", "456 Oak Ave.", "555-5678", "john@email.com", "A101", 50000, "01/01/2022")
faculty = Faculty("Mary", "789 Pine Rd.", "555-9876", "mary@email.com", "B202", 70000, "01/01/2021", "PhD", "Associate")
staff = Staff("Bob", "321 Elm St.", "555-4321", "bob@email.com", "C303", 40000, "01/01/2020", "Manager")
```
【1】编写实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty和Staff。 具体要求如下: [1].Person类中的属性有:姓名name(String类型),地址address(String类型),电话号码telphone(String类型)和电子邮件地址email(String类型); [2].Employee类中的属性有:办公室office(String类型),工资wage(double类型),受雇日期hiredate(String类型); [3].Faculty类中的属性有:学位degree(String类型),级别level(String类型); Staff类中的属性有:职务称号duty(String类型)。
[4].Person类应该有一个无参的构造函数,一个带有全部属性的构造函数以及getters/setters方法;
[5].Employee类应该有一个无参的构造函数、一个带有全部属性的构造函数以及getters/setters方法;
[6].Faculty类应该有一个无参的构造函数、一个带有全部属性的构造函数以及getters/setters方法;
[7].Staff类应该有一个无参的构造函数、一个带有全部属性的构造函数以及getters/setters方法;
[8].Person类还应该有一个toString()方法,用于描述一个Person对象的信息;
[9].Employee类还应该有一个toString()方法,用于描述一个Employee对象的信息;
[10].Faculty类还应该有一个toString()方法,用于描述一个Faculty对象的信息;
[11].Staff类还应该有一个toString()方法,用于描述一个Staff对象的信息。
以下是代码实现:
```
public class Person {
private String name;
private String address;
private String telephone;
private String email;
public Person() {}
public Person(String name, String address, String telephone, String email) {
this.name = name;
this.address = address;
this.telephone = telephone;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString() {
return "Name: " + this.getName() + ", Address: " + this.getAddress() + ", Telephone: " +
this.getTelephone() + ", Email: " + this.getEmail();
}
}
public class Employee extends Person {
private String office;
private double wage;
private String hiredate;
public Employee() {}
public Employee(String name, String address, String telephone, String email, String office, double wage, String hiredate) {
super(name, address, telephone, email);
this.office = office;
this.wage = wage;
this.hiredate = hiredate;
}
public String getOffice() {
return office;
}
public void setOffice(String office) {
this.office = office;
}
public double getWage() {
return wage;
}
public void setWage(double wage) {
this.wage = wage;
}
public String getHiredate() {
return hiredate;
}
public void setHiredate(String hiredate) {
this.hiredate = hiredate;
}
public String toString() {
return super.toString() + ", Office: " + this.getOffice() + ", Wage: " + this.getWage() + ", Hiredate: " + this.getHiredate();
}
}
public class Faculty extends Employee {
private String degree;
private String level;
public Faculty() {}
public Faculty(String name, String address, String telephone, String email, String office, double wage, String hiredate,
String degree, String level) {
super(name, address, telephone, email, office, wage, hiredate);
this.degree = degree;
this.level = level;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String toString() {
return super.toString() + ", Degree: " + this.getDegree() + ", Level: " + this.getLevel();
}
}
public class Staff extends Employee {
private String duty;
public Staff() {}
public Staff(String name, String address, String telephone, String email, String office, double wage, String hiredate,
String duty) {
super(name, address, telephone, email, office, wage, hiredate);
this.duty = duty;
}
public String getDuty() {
return duty;
}
public void setDuty(String duty) {
this.duty = duty;
}
public String toString() {
return super.toString() + ", Duty: " + this.getDuty();
}
}
```
阅读全文