编写一个Course类作为父类,属性score,qiandaocishu
时间: 2023-11-27 18:49:43 浏览: 90
以下是一个简单的Course类,具有score和qiandaocishu属性:
```python
class Course:
def __init__(self, score, qiandaocishu):
self.score = score
self.qiandaocishu = qiandaocishu
```
在这个类中,__init__方法用于初始化score和qiandaocishu属性。可以使用以下方式创建Course对象:
```python
course = Course(90, 10)
```
这将创建一个新的Course对象,其中score属性为90,qiandaocishu属性为10。你可以通过以下方式访问这些属性:
```python
print(course.score) # 输出90
print(course.qiandaocishu) # 输出10
```
注意,这个类还没有任何方法,但是你可以根据自己的需要添加更多的方法。
相关问题
1、编写一个Course类作为父类,属性score,qiandaocishu 2、编写一个Kotlin类作为Course的子类,增加课程名称属性 3、在Kotlin类中写一个方法printqiandaocishu,使用for in循环输出1..qiandaocishu 4、创建一个Kotlin文件和main方法,创建Kotlin类对象,并调用printqiandaocishu方法
1. Course类的实现:
```kotlin
open class Course(var score: Int, var qiandaocishu: Int)
```
2. Kotlin类作为Course的子类的实现:
```kotlin
class KotlinCourse(score: Int, qiandaocishu: Int, var courseName: String) : Course(score, qiandaocishu)
```
3. 在Kotlin类中写一个方法printqiandaocishu,使用for in循环输出1..qiandaocishu的实现:
```kotlin
fun printqiandaocishu() {
for (i in 1..qiandaocishu) {
println("第 $i 次签到")
}
}
```
4. 创建一个Kotlin文件和main方法,创建Kotlin类对象,并调用printqiandaocishu方法的实现:
```kotlin
fun main() {
val kotlinCourse = KotlinCourse(90, 10, "Kotlin课程")
kotlinCourse.printqiandaocishu()
}
```
完整代码如下:
```kotlin
open class Course(var score: Int, var qiandaocishu: Int)
class KotlinCourse(score: Int, qiandaocishu: Int, var courseName: String) : Course(score, qiandaocishu) {
fun printqiandaocishu() {
for (i in 1..qiandaocishu) {
println("第 $i 次签到")
}
}
}
fun main() {
val kotlinCourse = KotlinCourse(90, 10, "Kotlin课程")
kotlinCourse.printqiandaocishu()
}
```
python设计一个Course类
好的,以下是一个简单的Python Course类的设计:
```python
class Course:
def __init__(self, course_name, instructor, duration):
self.course_name = course_name
self.instructor = instructor
self.duration = duration
def get_course_name(self):
return self.course_name
def get_instructor(self):
return self.instructor
def get_duration(self):
return self.duration
def set_course_name(self, new_course_name):
self.course_name = new_course_name
def set_instructor(self, new_instructor):
self.instructor = new_instructor
def set_duration(self, new_duration):
self.duration = new_duration
```
这个类有三个属性:课程名称,讲师和时长。它还有6个方法:获取课程名称,获取讲师,获取时长,设置课程名称,设置讲师和设置时长。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)