用eclipse定义一个z4_1Person 类的子类Student,并创建子类对象,然后显示其信息
时间: 2024-05-15 14:18:26 浏览: 106
好的,您可以参考下面的代码实现:
```java
// 定义 Person 类
class z4_1Person {
String name;
int age;
public z4_1Person(String name, int age) {
this.name = name;
this.age = age;
}
public void display() {
System.out.println("Name: " + name + " , Age: " + age);
}
}
// 定义 Student 类,继承自 Person 类
class Student extends z4_1Person {
int grade;
public Student(String name, int age, int grade) {
super(name, age);
this.grade = grade;
}
public void display() {
super.display();
System.out.println("Grade: " + grade);
}
}
// 创建 Student 子类对象并显示其信息
public class Main {
public static void main(String[] args) {
Student student = new Student("Tom", 18, 90);
student.display();
}
}
```
输出结果为:
```
Name: Tom , Age: 18
Grade: 90
```
阅读全文