根据汽车的特征和行为定义一个表示汽车的类,示例代码如下:
class Car:
wheels = 4 #定义属性,表示汽车的车轮数量
def drive(self): #定义方法,用于实现汽车驾驶的行为
print('行驶')
定义属性
class Car:
wheels = 4 #定义属性,表示汽车的车轮数量
def drive(self): #定义方法,用于实现汽车驾驶的行为
print('行驶')
class Student:
age = 18
height = 1.70
weight = 56
grade = 80
def get_age(self):
return
def get_height(self):
return
def get_weight(self):
return
def get_grade(self):
return
class 书:
名称 = "Python程序设计"
出版社 = "人民邮电出版社"
编著 = "黑马程序员"
# 方法
def open(self):
print("打开")
class Movie:
name = "Zootopia"
time = 1.40
#++++++++++++++ 8.2.2 对象的创建与使用 +++++++++++++++++
car = Car() #创建
print(car.wheels) #访问属性
car.drive() #调用方法
book = 书()
movie = Movie()
print(movie.name)
movie.see()