编写一个课程类,包含3个成员变量:课程编号,课程名和学分数;一个方法:printCourceInfo,用于输出课程的相关信息,并编写课程类的测试程序;1个构造方法:用于初始化课程编号,课程名和学分。
时间: 2023-05-12 14:04:20 浏览: 96
好的,以下是课程类的代码:
```python
class Course:
def __init__(self, course_id, course_name, credit):
self.course_id = course_id
self.course_name = course_name
self.credit = credit
def printCourseInfo(self):
print("课程编号:", self.course_id)
print("课程名:", self.course_name)
print("学分数:", self.credit)
```
以下是测试程序:
```python
course1 = Course("001", "计算机科学导论", 3)
course2 = Course("002", "数据结构与算法", 4)
course1.printCourseInfo()
course2.printCourseInfo()
```
输出结果:
```
课程编号: 001
课程名: 计算机科学导论
学分数: 3
课程编号: 002
课程名: 数据结构与算法
学分数: 4
```
希望这个回答能够帮到你!
相关问题
编写一个课程类,包含3个成员变量:课程编号,课程名和学分数;一个方法:printCourceInfo,用于输出课程
类的信息。以下是示例代码:
class Course:
def __init__(self, course_id, course_name, credit):
self.course_id = course_id
self.course_name = course_name
self.credit = credit
def printCourseInfo(self):
print("课程编号:", self.course_id)
print("课程名:", self.course_name)
print("学分数:", self.credit)
# 示例用法
course = Course("001", "计算机科学导论", 3)
course.printCourseInfo() # 输出:课程编号:001 课程名:计算机科学导论 学分数:3
编写一个课程类,包含3个成员变量:课程编号(cNumber)、课程名(cName)和学分数(cUnit);1个方法:printCourceInfo,用于输出课程的相关信息,并编写课程类的测试程序;1个构造方法:用于初始化课程编号、课程名和学分
数。以下是课程类的代码:
```python
class Course:
def __init__(self, cNumber, cName, cUnit):
self.cNumber = cNumber
self.cName = cName
self.cUnit = cUnit
def printCourseInfo(self):
print("课程编号:", self.cNumber)
print("课程名:", self.cName)
print("学分数:", self.cUnit)
# 测试程序
course = Course("001", "计算机科学", 3)
course.printCourseInfo()
```
输出结果为:
```
课程编号: 001
课程名: 计算机科学
学分数: 3
```
希望这个答案能够帮到您!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.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)