java编程实现类的继承。编写父类People,子类Employee继承自人类。人类具有姓名,性别,年龄等性质,还具有吃和说的行为。雇员类继承父类,还拥有工号性质和工作行为。构造人类和学生类的对象,调用吃、说、工作的方法输出有关信息。
时间: 2023-06-03 20:01:40 浏览: 230
论JAVA继承机制中父类与子类的关系
3星 · 编辑精心推荐
好的,我了解了。以下是Java代码实现:
```java
class People {
private String name;
private String gender;
private int age;
public People(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public void eat() {
System.out.println(name + " is eating.");
}
public void say() {
System.out.println(name + " is speaking.");
}
}
class Employee extends People {
private String employeeID;
public Employee(String name, String gender, int age, String employeeID) {
super(name, gender, age);
this.employeeID = employeeID;
}
public void work() {
System.out.println(getName() + " is working with ID: " + employeeID);
}
}
public class Main {
public static void main(String[] args) {
People person1 = new People("Tom", "Male", 30);
Employee employee1 = new Employee("Jerry", "Male", 25, "201801");
person1.eat();
person1.say();
employee1.eat();
employee1.say();
employee1.work();
}
}
```
输出结果为:
```
Tom is eating.
Tom is speaking.
Jerry is eating.
Jerry is speaking.
Jerry is working with ID: 201801
```
以上是代码实现,你可以试着运行一下看看效果。
阅读全文