设计一个person类java_定义一个Person类,含姓名、性别、年龄等字段;继承Person类设计...
时间: 2024-03-22 11:42:47 浏览: 85
好的,以下是Java中定义一个Person类,包含姓名、性别、年龄等字段的代码:
```
public class Person {
private String name;
private String gender;
private int age;
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
接下来,让我们继承Person类并设计一个Student类,增加学校、班级、学号等字段:
```
public class Student extends Person {
private String school;
private String classNo;
private String studentNo;
public Student(String name, String gender, int age, String school, String classNo, String studentNo) {
super(name, gender, age);
this.school = school;
this.classNo = classNo;
this.studentNo = studentNo;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getClassNo() {
return classNo;
}
public void setClassNo(String classNo) {
this.classNo = classNo;
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
}
```
以上就是在Java中定义一个Person类,以及继承Person类设计一个Student类的代码。
阅读全文