编程题, 用java编写一个名为Person的类和它的子类Employee 具体要求如下: (1)Person类中的属性有:姓名name ( String类型) , 地址address ( String类型) , 电话号码telephone ( String类型) ; (2)Employee类继承Person类,并新建属性:办公室office ( String类型) ,工资wage ( double类型) (3)每个类中分别定义两个构造方法。 (4)在子类中定义主方法,创建子类对象并打印类的部分属性。
时间: 2024-03-15 14:42:08 浏览: 80
以下是Java代码实现:
```java
public class Person {
private String name;
private String address;
private String telephone;
public Person(String name, String address, String telephone) {
this.name = name;
this.address = address;
this.telephone = telephone;
}
public Person() {
this("", "", "");
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getTelephone() {
return telephone;
}
}
public class Employee extends Person {
private String office;
private double wage;
public Employee(String name, String address, String telephone, String office, double wage) {
super(name, address, telephone);
this.office = office;
this.wage = wage;
}
public Employee() {
this("", "", "", "", 0.0);
}
public static void main(String[] args) {
Employee employee = new Employee("张三", "北京市海淀区", "010-12345678", "A101", 10000.0);
System.out.println("姓名:" + employee.getName());
System.out.println("地址:" + employee.getAddress());
System.out.println("电话:" + employee.getTelephone());
System.out.println("办公室:" + employee.office);
System.out.println("工资:" + employee.wage);
}
}
```
运行结果:
```
姓名:张三
地址:北京市海淀区
电话:010-12345678
办公室:A101
工资:10000.0
```
阅读全文