编写代码,定义一个Person类,该类包含实例属性name(姓名),gender(性别)和age(年龄),还包含实例方法showlnfo(),用于输出3个实例属性;以Person类为基础 定义一个Student类,新增3个实例属性,即sid(学号)、chn(语文)和math(数学), 并对实例方法showlnfo()进行重写,用于输出6个实例属性。
时间: 2024-02-06 12:12:37 浏览: 62
好的,以下是代码实现:
```java
class Person {
String name;
String gender;
int age;
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public void showInfo() {
System.out.println("姓名:" + name + ",性别:" + gender + ",年龄:" + age);
}
}
class Student extends Person {
String sid;
int chn;
int math;
public Student(String name, String gender, int age, String sid, int chn, int math) {
super(name, gender, age);
this.sid = sid;
this.chn = chn;
this.math = math;
}
@Override
public void showInfo() {
System.out.println("姓名:" + name + ",性别:" + gender + ",年龄:" + age +
",学号:" + sid + ",语文成绩:" + chn + ",数学成绩:" + math);
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person("张三", "男", 20);
p.showInfo();
Student s = new Student("李四", "女", 18, "001", 90, 95);
s.showInfo();
}
}
```
运行结果:
```
姓名:张三,性别:男,年龄:20
姓名:李四,性别:女,年龄:18,学号:001,语文成绩:90,数学成绩:95
```
阅读全文