编写Java程序,抽象类Person含有私有属性name和id,含有有参构造方法,为name和id赋值,含有void show(), 可输出成员属性值,“我是xx,id为”,含String getName(),返回name值 含抽象方法 String run(String name) Person的子类Student含私有属性school和department,含有参构造方法,为name,id,school,department赋值, 重写父类中的run(),返回“某学院某同学在某学校夜跑” 类Test定义含有main()方法的测试类,在main()中创造对象,调用构造方法完成成员赋值,并通过相应成员调用,完成输出
时间: 2024-02-27 21:54:09 浏览: 179
好的,以下是Java程序的实现:
Person抽象类:
```java
public abstract class Person {
private String name;
private String id;
public Person(String name, String id) {
this.name = name;
this.id = id;
}
public void show() {
System.out.println("我是" + name + ", id为" + id);
}
public String getName() {
return name;
}
public abstract String run(String name);
}
```
Student子类:
```java
public class Student extends Person {
private String school;
private String department;
public Student(String name, String id, String school, String department) {
super(name, id);
this.school = school;
this.department = department;
}
@Override
public String run(String name) {
return "某学院" + name + "在" + school + "夜跑";
}
}
```
Test测试类:
```java
public class Test {
public static void main(String[] args) {
Person person = new Student("小明", "20210001", "某大学", "计算机系");
person.show();
System.out.println(person.run(person.getName()));
}
}
```
输出结果:
```
我是小明, id为20210001
某学院小明在某大学夜跑
```
希望我的回答能够帮助到你。
阅读全文