设计一个课程类,该类有编号,名称,任课教师,上课地点4个属性,其中上课地点为私有属性,还包含构造函数和展示课程信息的方法,创建对象显示课程信息python
时间: 2024-05-06 21:15:00 浏览: 160
以下是一个可能的实现,其中上课地点为私有属性,使用了Python的property装饰器来实现访问和修改私有属性的方法。
```python
class Course:
def __init__(self, course_id, name, teacher, location):
self.course_id = course_id
self.name = name
self.teacher = teacher
self._location = location
@property
def location(self):
return self._location
@location.setter
def location(self, new_location):
self._location = new_location
def show_info(self):
print("Course ID:", self.course_id)
print("Name:", self.name)
print("Teacher:", self.teacher)
print("Location:", self.location)
# 创建课程对象并显示信息
course1 = Course(1, "Math", "John Doe", "Room A101")
course1.show_info()
# 修改地点
course1.location = "Room B202"
course1.show_info()
```
输出结果:
```
Course ID: 1
Name: Math
Teacher: John Doe
Location: Room A101
Course ID: 1
Name: Math
Teacher: John Doe
Location: Room B202
```
阅读全文