1.定义PrimaryStudent,从Student继承,并新增一个grade字段:
时间: 2024-06-01 10:11:25 浏览: 11
class PrimaryStudent(Student):
def __init__(self, name, age, gender, grade):
super().__init__(name, age, gender)
self.grade = grade
相关问题
用Java定义一个Student:Student继承Person类:私有属性:年级grade,学校school。方法:grade和school两个属性的get和set方法。
好的,以下是用Java定义一个Student的代码:
```
public class Student extends Person {
private String grade;
private String school;
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
```
其中,Person类是Student类的父类,具体实现可以根据需求自行编写。
定义一个student类,用来输入并显示学生信息
好的,您可以参考以下代码实现:
```python
class Student:
def __init__(self, name, age, gender, grade):
self.name = name
self.age = age
self.gender = gender
self.grade = grade
def display_info(self):
print(f"姓名:{self.name},年龄:{self.age},性别:{self.gender},年级:{self.grade}")
```
您可以按照如下方式来使用该类:
```python
# 创建一个学生对象
s = Student("Tom", 18, "男", "高三")
# 显示学生信息
s.display_info()
```
输出结果将会是:
```
姓名:Tom,年龄:18,性别:男,年级:高三
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)