javaeclipse中(2)Employee类中的属性有:办公室office(String类型),工资wage(double类型) ,创建showemployee方法用于显示本类中的office和wage属性值。创建其构造方法,为这二个属性进行赋值。( 但是子类的构造方法在执行时需要先执行父类的构造方法,此时该 怎么处理,由大家想办法、讨论出一个可行的方法。)
时间: 2024-03-02 19:51:32 浏览: 22
好的,我来为您演示一下:
```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类中的属性信息。
希望这个演示能够帮助到您!
相关问题
java中实现一个名为Person的类和它的子类Employee 具体要求如下: (1)Person类中的属性有:姓名name(String类型),地址address(String类型),电话号码telphone(String类型); 其中telphone为私有属性。创建showinfo方法用于显示属性信息。 创建其构造方法,为这三个属性进行赋值。 (2)Employee类中的属性有:办公室office(String类型),工资wage(double类型) ,创建showemployee方法用于显示本类中的office和wage属性值。创建其构造方法,为这二个属性进行赋值。( 但是子类的构造方法在执行时需要先执行父类的构造方法,此时该 怎么处理,由大家想办法、讨论出一个可行的方法。) (3)编写这两个类,及相应的测试代码。 编写测试类test,创建employee的对象并进行初始化,然后调用showemployee和showinfo方法。
好的,以下是Java代码实现:
Person类:
```
public class Person {
protected String name;
protected String address;
private String telphone;
public Person(String name, String address, String telphone) {
this.name = name;
this.address = address;
this.telphone = telphone;
}
public void showInfo(){
System.out.println("姓名:" + name);
System.out.println("地址:" + address);
System.out.println("电话:" + telphone);
}
}
```
Employee类:
```
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);
System.out.println("工资:" + wage);
}
}
```
Test类:
```
public class Test {
public static void main(String[] args) {
Employee employee = new Employee("张三", "北京市", "123456789", "A001", 5000);
employee.showInfo();
employee.showEmployee();
}
}
```
运行结果:
```
姓名:张三
地址:北京市
电话:123456789
办公室:A001
工资:5000.0
```
【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();
}
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)