javaperson类和student类
时间: 2023-04-19 14:03:19 浏览: 85
JavaPerson类是一个Java程序中的类,它代表一个人,包含姓名、年龄、性别等属性和方法。它可以被其他类继承或实例化使用。
Student类是JavaPerson类的一个子类,它代表一个学生,除了继承JavaPerson类的属性和方法外,还包含学号、班级、成绩等属性和方法。它可以被其他类继承或实例化使用,通常用于学生管理系统等应用中。
相关问题
java编写student和person类
Java编写Student和Person类:
Person类:
public class Person {
private String name;
private int age;
private String gender;
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
Student类:
public class Student extends Person {
private String studentId;
private String major;
public Student(String name, int age, String gender, String studentId, String major) {
super(name, age, gender);
this.studentId = studentId;
this.major = major;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
java中继承student和person
Java中可以使用关键字extends来实现类的继承。例如,可以定义一个Student类继承自Person类,代码如下:
```
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Student extends Person {
private String school;
public Student(String name, int age, String school) {
super(name, age);
this.school = school;
}
public String getSchool() {
return school;
}
}
```
在上面的例子中,Student类继承了Person类,并且添加了一个新的属性school。在Student类的构造方法中,使用super关键字调用了父类的构造方法来初始化继承自Person类的属性name和age。这样,Student类就可以使用Person类的属性和方法,同时还可以添加自己的属性和方法。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)