class Person{ public String name; public String getName(){ return name; } } class Employee extends Person{ public int employeeNumber; public int getEmployeeNumber(){ return employeeNumber; } } class Manager extends Employee{ public String responsibilities; public String getResponsibilities(){ return responsibilities; } } public class Exam { public static void main(String args[]){ Employee Li = new Employee(); Li.name="Li Ming"; Li.employeeNumber=123456; System.out.println(Li.getName()); System.out.println(Li.getEmployeeNumber()); Manager he = new Manager(); he.name="He Xia"; he.employeeNumber=543469; he.responsibilities="Internet project"; System.out.println(he.getName()); System.out.println(he.getResponsibilities()); } }
时间: 2023-06-07 07:09:39 浏览: 77
你的问题是关于Java类继承的。代码定义了一个Person类,一个Employee类,一个Manager类。Employee和Manager都是Person的子类,Manager是Employee的子类。代码在main方法中创建了Employee和Manager对象,然后打印它们的姓名和员工编号或职责。
相关问题
设计一个抽象类名字为 Person,它的子类为Student和 Employee;person类有name以及toString方法;student有 status属性(freshman, sophomore, junior, or senior),定义status 为常量;Employee类 有office, salary;分别定义各个类的有参构造方法,Person(String name)、Student(String name,int status)、Employee(String name,String office,double salary),注意需要在子类构造方法第一句调用父类有参构造方法,为子类也实现toString方法。编写测试类定义一个数组存放每个非抽象类对象(Student和 Employee),然后利用多态调用toString方法打印各个对象的所有属性信息。(为了减少书写量,只需要为name属性完成get和set方法,其他属性不用写)
以下是根据您的要求设计的抽象类和子类的代码:
```java
// Person抽象类
public abstract class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract String toString();
}
// Student子类
public class Student extends Person {
private static final int FRESHMAN = 1;
private static final int SOPHOMORE = 2;
private static final int JUNIOR = 3;
private static final int SENIOR = 4;
private int status;
public Student(String name, int status) {
super(name);
this.status = status;
}
public String getStatus() {
String statusStr;
switch (status) {
case FRESHMAN:
statusStr = "Freshman";
break;
case SOPHOMORE:
statusStr = "Sophomore";
break;
case JUNIOR:
statusStr = "Junior";
break;
case SENIOR:
statusStr = "Senior";
break;
default:
statusStr = "Unknown";
break;
}
return statusStr;
}
@Override
public String toString() {
return "Name: " + getName() + ", Status: " + getStatus();
}
}
// Employee子类
public class Employee extends Person {
private String office;
private double salary;
public Employee(String name, String office, double salary) {
super(name);
this.office = office;
this.salary = salary;
}
public String getOffice() {
return office;
}
public double getSalary() {
return salary;
}
@Override
public String toString() {
return "Name: " + getName() + ", Office: " + getOffice() + ", Salary: " + getSalary();
}
}
// 测试类
public class Test {
public static void main(String[] args) {
Person[] people = new Person[4];
people[0] = new Student("Alice", 3);
people[1] = new Student("Bob", 2);
people[2] = new Employee("Charlie", "HR", 50000);
people[3] = new Employee("Dave", "Finance", 60000);
for (Person person : people) {
System.out.println(person.toString());
}
}
}
```
这段代码定义了一个抽象类`Person`,它有一个`name`属性和一个`toString`方法。`Student`和`Employee`都是`Person`的子类,分别有额外的属性和构造方法,并且都实现了`toString`方法。在测试类中,使用多态性将`Student`和`Employee`对象存储在`Person`类型的数组中,并通过调用`toString`方法打印出各个对象的属性信息。
定义一个Person类和它的子类Employee。Person类有姓名、年龄和电话号码等私有成员变量,为私有成员变量分别定义set和get方法;定义一个方法showMessage()输出Person类的成员变量。Employee类有部门、工资等私有成员变量,定义一个方法showMessage()输出Employee的基本信息(含姓名、年龄和电话号码)。Person类和Employee类均定义带参和不带参的构造方法实现对其成员变量的初始化。定义一个public类,在其main()方法中分别基于父类和子类创建两个对象,通过构造方法分别为父类和子类的成员变量赋值,然后分别调用父类和子类的showMessage()方法输出其基本信息。使用java编写
在 Java 中,可以使用如下代码定义 Person 类和 Employee 类:
```
public class Person {
private String name;
private int age;
private String phone;
public Person() {
// 默认构造方法
}
public Person(String name, int age, String phone) {
this.name = name;
this.age = age;
this.phone = phone;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;
}
public void showMessage() {
System.out.println("姓名: " + name + ", 年龄: " + age + ", 电话: " + phone);
}
}
public class Employee extends Person {
private String department;
private double salary;
public Employee() {
// 默认构造方法
}
public Employee(String name, int age, String phone, String department, double salary) {
super(name, age, phone);
this.department = department;
this.salary = salary;
}
public void setDepartment(String department) {
this.department = department;
}
public String getDepartment() {
return department;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getSalary() {
return salary;
}
public void showMessage() {
super.showMessage();
System.out.println("部门: " + department + ", 工资: " + salary);
}
}
```
然后,可以在另一个 public 类中的 main 方法中创建 Person 类和 Employee 类的对象,并调用 showMessage 方法输出其基本信息:
```
public class Test {
public static void main(String[] args) {
Person p = new Person("张三", 25, "123456");
p.showMessage();
Employee e = new Employee("李四", 30, "654321", "市场部", 8000);
e.showMessage();
}
}
```
输出结果如下:
```
姓名: 张三, 年龄: 25, 电话: 123456
阅读全文