类是对现实生活中一类具有共同特征的事物的抽象,根据类来创建对象被称为实例化,创建类时,类名首字母要大写完整的类如下例所示
#class声明类,后跟类名,类名首字母需要大写 class Dog(): """一次模拟小狗的简单测试""" #def定义__init__()方法 #其中self为类种方法自带形参,指向实例 def __init__(self, name, age): """初始化属性name和age""" #self前缀的形参名用于指向实例带入的实参,意为获取存储在形参name中的值 #这种可以通过实例访问的变量成为属性 self.name = name self.age = age #定义在类中的函数,也称为方法,方法中的形参self代指实例的参数 #如果方法中也有不包含在__init__()方法定义的形参,也可以继续跟在self之后 def sit(self): """模拟小狗被命令时蹲下""" print(self.name.title() + " is now sitting.") def roll_over(self): """模拟小狗被命令时打滚""" print(self.name.title() + " rolled over!") #实例名 = 类名(),该过程被称为实例化 #实例化后,实例中的参数将会传入类中等待执行 my_dog = Dog('willie',6) your_dog = Dog('lucy',3) print("My dog's name is " + my_dog.name.title() + ".") print("My dog is " + str(my_dog.age) + " years old.") #实例化后,调用类中的方法,只需要实例调用类中的方法名即可 my_dog.sit() print(" You dog's name is " + my_dog.name.title() + ".") print("You dog is " + str(my_dog.age) + " years old.") your_dog.sit() #输出结果如下所示: My dog's name is Willie. My dog is 6 years old. Willie is now sitting. You dog's name is Willie. You dog is 6 years old. Lucy is now sitting
课后习题答案如下:
#9-1 #创建类Restaurant class Restaurant(): #定义方法__init__包含三个形参self、restaurant_name和cuisine_type,其中self是每个与类有关的方法自带的形参,也表示实例本身,而其他两个形参则用来传递实参中的数据 def __init__(self,restaurant_name,cuisine_type): #以self为前缀的变量可供类中的所有方法使用,也可以通过类的任何实力来访问这些变量 #本例中实例one_restaurant将实参中的值传入方法__init__中的形参中,形参再将值保存在被self修饰的两个变量中供给下面代码使用 self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type #方法describe_restaurant没有新增能其他信息,因此只需要self一个参数,方法open_restaurant同样 def describe_restaurant(self): print(self.restaurant_name + "是一家中国饭馆.") def open_restaurant(self): print("正在营业!") one_restaurant = Restaurant('菊下楼','四川料理') print("这家饭馆叫做" + one_restaurant.restaurant_name + ".") print(one_restaurant.restaurant_name + "主营" + one_restaurant.cuisine_type + ".") one_restaurant.describe_restaurant() one_restaurant.open_restaurant() #输出结果如下所示: 这家饭馆叫做菊下楼. 菊下楼主营四川料理. 菊下楼是一家中国饭馆. 正在营业! #9-3 class User(): def __init__(self,first_name,last_name,wugong): self.first_name = first_name self.last_name = last_name self.wugong = wugong def describe_use(self): print(self.first_name + self.last_name + "是一名精通" + self.wugong +"的少林高手!") def greet_user(self): print("少林功夫好耶,真厉害!") user_name = User('周','星星','大力金刚腿') user_name.describe_use() user_name.greet_user() #输出结果如下所示: 周星星是一名精通大力金刚腿的少林高手! 少林功夫好耶,真厉害!
修改类中属性值的方式大概有两种,直接修改、通过方法修改属性的值修改,如下所示:
class Car(): """一次模拟汽车的简单尝试""" def __init__(self,make,model,year): """初始化描述汽车的属性""" self.make = make self.model = model self.year = year #新增属性odometer_reading,赋默认值0 #如果形参有值,可以将其放在属性设置时写出,也可以在设置形参时直接写上默认值 #形参需要通过实例化带入实参的情况下,需要在__init__()中声明 self.odometer_reading = 0 def get_descriptive_name(self): """返回整洁的描述信息""" long_name = str(self.year) + ' ' + self.make + ' ' + self.model return long_name.title() def read_odometer(self): """打印一条指出汽车里程的信息""" print("This car has " + str(self.odometer_reading) + " miles on it.") #除了直接设置,也可以通过增加方法来修改属性值 def update_odometer(self,mileage): """ 将里程表读书设置为指定的值 禁止将里程表读数往回调 """ if mileage > self.odometer_reading: self.odometer_reading = mileage else: print("You can't roll back an odometer!") def increment_odometer(self,miles): """将里程表的读数增加""" self.odometer_reading += miles my_new_car = Car('audi','a4',2016) print(my_new_car.get_descriptive_name()) #直接修改属性值,类似关键字实参的设置 print("---------------直接修改属性值---------------") my_new_car.odometer_reading = 23 my_new_car.read_odometer() #通过新增方法修改属性值,保留了默认值,在不引用新增方法的情况下,输出无变化 #用实例引用属性,并赋值 print("---------------新增方法修改属性值---------------") my_new_car.update_odometer(18) my_new_car.read_odometer() #通过新增方法实现属性值的增加 print("---------------新增方法实现属性值的增加---------------") my_new_car.increment_odometer(100) my_new_car.read_odometer()
课后习题答案如下所示:
#9-4 class Restaurant(): def __init__(self,restaurant_name,cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type self.number_served = 0 def describe_restaurant(self): print("这家餐厅叫做" + self.restaurant_name + ",是一家" + self.cuisine_type +"餐厅!") def open_restaurant(self): print("正在营业!") def number(self): print("曾有" + str(self.number_served) + "人在这里就餐") def set_number_served(self,numbers): self.number_served = numbers def increment_number_served(self,new_number): self.number_served += new_number restaurant = Restaurant('菊下楼','川菜') restaurant.describe_restaurant() restaurant.open_restaurant() restaurant.number_served = 200 restaurant.number() restaurant.set_number_served(300) restaurant.number() restaurant.increment_number_served(500) restaurant.number() #输出结果如下所示: 这家餐厅叫做菊下楼,是一家川菜餐厅! 正在营业! 曾有200人在这里就餐 曾有300人在这里就餐 曾有800人在这里就餐 #9-5 class User(): def __init__(self,first_name,last_name,login_attempts = 0): self.first_name = first_name self.last_name = last_name self.login_attempts = login_attempts def describe_user(self): print(self.first_name.title() + ' ' + self.last_name.title()) def greet_user(self): print("Hello,my firend!") def increment_login_attempts(self): self.login_attempts += 1 print(self.login_attempts) def reset_login_attempts(self): self.login_attempts = 0 print(self.login_attempts) my_firend = User('jimi','lee') my_firend.describe_user() my_firend.greet_user() my_firend.increment_login_attempts() my_firend.reset_login_attempts() #输出结果如下所示: Jimi Lee Hello,my firend! 1 0
继承是面向对象编程的概念之一,通过类的调用,让其中一个类获得另一个类的部分或全部特征的一种方式。继承可以使得子类别具有父类别的各种属性和方法,如下例所示:
#给子类定义属性和方法 class Car(): """一次模拟汽车的简单尝试""" def __init__(self,make,model,year): self.make = make self.model = model self.year = year self.odmeter_reading = 0 def get_descriptive_name(self): long_name = str(self.year) + ' ' + self.make + ' ' + self.model return long_name.title() def read_odometer(self): print("This car has " + str(self.odmeter_reading) + " miles on it.") def update_odometer(self,mileage): if mileage >= self.odmeter_reading: self.odmeter_reading = mileage else: print("You can't roll back an odometer!") def increment_odometer(self,miles): self.odmeter_reading += miles #定义类ElectricCar,括号中是继承的父类Car class ElectricCar(Car): """Represent aspects of a car,specific toelectric vehicles.""" def __init__(self,make,model,year): """ 电动汽车的独特之处 初始化父类的属性,在初始化为电动汽车特有的属性 """ #函数super()用于调整父类和子类的继承问题,尤其是多类继承的时候,super()可以声明调用类的名字,单类中可以不用声明 #此处通过调用父类中的__init__()方法,让子类ElectricCar中的实例包含父类实例的所有属性 super().__init__(make,model,year) 添加了子类的新属性self.battery_size = 70,只存在于子类ElectricCar中,父类没有该属性 self.battery_size = 70 def describe_battery(self): """打印一条描述电瓶容量的消息""" print("This car has a " + str(self.battery_size) + "-kWh battery.") my_tesla = ElectricCar('tesla','model s',2016) print(my_tesla.get_descriptive_name()) my_tesla.describe_battery() #输出结果如下: 2016 Tesla Model S This car has a 70-kWh battery.
子类继承父类以后,也可以单独给子类定义属性和方法,此时该属性或者方法独属于子类,与父类无关,如下所示:
#给子类定义属性和方法 class Car(): """一次模拟汽车的简单尝试""" def __init__(self,make,model,year): self.make = make self.model = model self.year = year self.odmeter_reading = 0 def get_descriptive_name(self): long_name = str(self.year) + ' ' + self.make + ' ' + self.model return long_name.title() def read_odometer(self): print("This car has " + str(self.odmeter_reading) + " miles on it.") def update_odometer(self,mileage): if mileage >= self.odmeter_reading: self.odmeter_reading = mileage else: print("You can't roll back an odometer!") def increment_odometer(self,miles): self.odmeter_reading += miles #定义类ElectricCar,括号中是继承的父类Car class ElectricCar(Car): """Represent aspects of a car,specific toelectric vehicles.""" def __init__(self,make,model,year): """ 电动汽车的独特之处 初始化父类的属性,在初始化为电动汽车特有的属性 """ #函数super()用于调整父类和子类的继承问题,尤其是多类继承的时候,super()可以声明调用类的名字,单类中可以不用声明 #此处通过调用父类中的__init__()方法,让子类ElectricCar中的实例包含父类实例的所有属性 super().__init__(make,model,year) #添加了子类的新属性self.battery_size = 70,只存在于子类ElectricCar中,父类没有该属性 self.battery_size = 70 def describe_battery(self): """打印一条描述电瓶容量的消息""" print("This car has a " + str(self.battery_size) + "-kWh battery.") #重写父类的方法,可在子类中定义一个与父类中需要重写的方法同名的方法即可 def get_descriptive_name(self): long_name = str(self.year) + ' ' + self.model + ' ' + self.make + " (原结果为:2016 Tesla Model S)" return long_name.title() my_tesla = ElectricCar('tesla','model s',2016)
#此处实例调用了get_descriptive_name()方法,因此直接输出了方法的返回值 print(my_tesla.get_descriptive_name()) my_tesla.describe_battery() #输出结果如下: 2016 Model S Tesla (原结果为:2016 Tesla Model S) This car has a 70-kWh battery.
此外,除了在子类对继承的父类进行操作,还可以将实例用作属性,如下所示:
#将实例用做属性 class Car(): """一次模拟汽车的简单尝试""" def __init__(self,make,model,year): self.make = make self.model = model self.year = year self.odmeter_reading = 0 def get_descriptive_name(self): long_name = str(self.year) + ' ' + self.make + ' ' + self.model return long_name.title() def read_odometer(self): print("This car has " + str(self.odmeter_reading) + " miles on it.") def update_odometer(self,mileage): if mileage >= self.odmeter_reading: self.odmeter_reading = mileage else: print("You can't roll back an odometer!") def increment_odometer(self,miles): self.odmeter_reading += miles #定义一个没有继承的新类,包含形参battery_size,并赋默认值70 class Battery(): """一次模拟电动汽车电瓶的简单尝试""" def __init__(self,battery_size = 70): """初始化电瓶的属性""" self.battery_size = battery_size def describe_battery(self): """打印一条描述电瓶容量的消息""" print("This car has a " + str(self.battery_size) + "-kWh battery.") def get_range(self): """打印一条消息,指出电瓶的续航里程""" if self.battery_size == 70: range = 240 elif self.battery_size == 85: range = 270 message = "This car can go approximately " + str(range) message += " miles on a full charge." print(message) class ElectricCar(Car): """电动汽车的独特之处""" def __init__(self,make,model,year): """ 初始化父类的属性,再初始化为电动汽车特有的属性 """ super().__init__(make,model,year) #添加新的属性self.battery,将类Battery实例化为Battery()(由于没有规定参数,因此为空,Battery类中有默认值70) self.battery = Battery() my_tesla = ElectricCar('tesla','model s',2016) print(my_tesla.get_descriptive_name()) #访问作为属性的实例,实例.属性.方法 my_tesla.battery.describe_battery() my_tesla.battery.get_range() #输出结果如下所示: 2016 Tesla Model S This car has a 70-kWh battery. This car can go approximately 240 miles on a full charge.
课后习题答案如下所示:
#9-6 class Restaurant(): def __init__(self,restaurant_name,cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type self.number_served = 0 def describe_restaurant(self): print("这家餐厅叫做" + self.restaurant_name + ",是一家" + self.cuisine_type +"餐厅!") def open_restaurant(self): print("正在营业!") def number(self): print("曾有" + str(self.number_served) + "人在这里就餐") def set_number_served(self,numbers): self.number_served = numbers def increment_number_served(self,new_number): self.number_served += new_number #定义子类IceCreanStand继承父类Restaurant class IceCreanStand(Restaurant): #定义方法__init__,同时包含父类形参restaurant_name,cuisine_type def __init__(self,restaurant_name,cuisine_type): #使用super()函数,将子类部分形参与父类形参关联,此时子类被关联形参可以使用父类方法 super().__init__(restaurant_name,cuisine_type) #定义子类专属形参flavors,赋值为空列表,该形参不可使用使用 self.flavors = [] #定义方法flavors_type,该方法单独实现子类需求 def flavors_type(self): while True: ice_cream = input("输入冰淇淋的口味: ") if ice_cream == 'quit': break else: self.flavors.append(ice_cream) print("这里的冰淇淋口味有:") for ice in self.flavors: print(" -" + ice) restaurant = IceCreanStand('菊下楼','川菜') restaurant.describe_restaurant() restaurant.open_restaurant() restaurant.number() restaurant.flavors_type() #输出结果如下所示: 这家餐厅叫做菊下楼,是一家川菜餐厅! 正在营业! 曾有0人在这里就餐 输入冰淇淋的口味: 草莓 输入冰淇淋的口味: 香蕉 输入冰淇淋的口味: 苹果 输入冰淇淋的口味: 荔枝 输入冰淇淋的口味: quit 这里的冰淇淋口味有: -草莓 -香蕉 -苹果 -荔枝 #9-7 #创建父类User class User(): #创建__init__()方法初始化属性 def __init__(self, first_name, last_name, login_attempts=0): self.first_name = first_name self.last_name = last_name self.login_attempts = login_attempts def describe_user(self): print(self.first_name.title() + ' ' + self.last_name.title()) def greet_user(self): print("Hello,my firend!") def increment_login_attempts(self): self.login_attempts += 1 print(self.login_attempts) def reset_login_attempts(self): self.login_attempts = 0 print(self.login_attempts) #创建子类Admin继承父类User class Admin(User):irst_name, last_name, login_attempts #定义方法__init__,包含父类变量 def __init__(self, first_name, last_name, login_attempts): #使用super()方法,让子类变量跟父类变量关联,使子类实例能够包含父类的属性 super().__init__(first_name, last_name, login_attempts) self.privileges = ['可以删除', '可以新增', '可以修改'] #创建方法show_privileges,使用父类属性进行判断,并更具判断结果,输出类属性的值 def show_privileges(self): if self.login_attempts == 0: print("没有特权!") elif self.login_attempts == 1: print(self.first_name.title() + ' ' + self.last_name.title() + ",你可以试试" + self.privileges[0] + "操作!") elif self.login_attempts == 2: print(self.first_name.title() + ' ' + self.last_name.title() + ",你可以试试" + self.privileges[1] + "操作!") elif self.login_attempts == 3: print(self.first_name.title() + ' ' + self.last_name.title() + ",你可以试试" + self.privileges[2] + "操作!") #实例包含父类实参,还包含子类实参 my_firend = Admin('jimi', 'lee', 1) my_firend.describe_user() my_firend.greet_user() my_firend.show_privileges() #输出结果如下: Jimi Lee Hello,my firend! Jimi Lee,你可以试试可以删除操作! #输出结果如下所示: Jimi Lee Hello,my firend! 你没有特权! #9-8 class User(): def __init__(self, first_name, last_name, login_attempts=0): self.first_name = first_name self.last_name = last_name self.login_attempts = login_attempts def describe_user(self): print(self.first_name.title() + ' ' + self.last_name.title()) def greet_user(self): print("Hello,my firend!") #定义新的类Privileges,只有属性self.privileges,赋值列表 class Privileges(): def __init__(self,privileges = ['可以删除', '可以新增', '可以修改']): self.privileges = privileges def show_privileges(self): print("你没有特权!") class Admin(User): def __init__(self, first_name, last_name, login_attempts): super().__init__(first_name, last_name, login_attempts) #将类Privileges实例化后用作属性 self.privilege = Privileges() my_firend = Admin('jimi', 'lee',1) my_firend.describe_user() my_firend.greet_user() #引用实例属性化的方法时,需注意格式为实例.属性.方法 my_firend.privilege.show_privileges() #输出结果如下所示: Jimi Lee Hello,my firend! 你没有特权! #9-9 class Car(): """一次模拟汽车的简单尝试""" def __init__(self,make,model,year): self.make = make self.model = model self.year = year self.odmeter_reading = 0 def get_descriptive_name(self): long_name = str(self.year) + ' ' + self.make + ' ' + self.model return long_name.title() def read_odometer(self): print("This car has " + str(self.odmeter_reading) + " miles on it.") def update_odometer(self,mileage): if mileage >= self.odmeter_reading: self.odmeter_reading = mileage else: print("You can't roll back an odometer!") def increment_odometer(self,miles): self.odmeter_reading += miles class Battery(): """一次模拟电动汽车电瓶的简单尝试""" def __init__(self,battery_size = 70): """初始化电瓶的属性""" self.battery_size = battery_size def describe_battery(self): """打印一条描述电瓶容量的消息""" print("This car has a " + str(self.battery_size) + "-kWh battery.") def get_range(self): """打印一条消息,指出电瓶的续航里程""" if self.battery_size == 70: range = 240 elif self.battery_size == 85: range = 270 message = "This car can go approximately " + str(range) message += " miles on a full charge." print(message) def upgrade_battery(self): #此处判断,出现!=后,下文需用=,用==会报错,不知道为什么,先记下来! if self.battery_size != 85: self.battery_size = 85 class ElectricCar(Car): """电动汽车的独特之处""" def __init__(self,make,model,year): """ 初始化父类的属性,再初始化为电动汽车特有的属性 """ super().__init__(make,model,year) self.battery = Battery() my_tesla = ElectricCar('tesla','model s',2016) print(my_tesla.get_descriptive_name()) my_tesla.battery.get_range() my_tesla.battery.upgrade_battery() my_tesla.battery.get_range() #输出结果如下所示: 2016 Tesla Model S This car can go approximately 240 miles on a full charge. This car can go approximately 270 miles on a full charge.
类的导入分为导入单个类,在模块中存储多个类和导入整个模块,具体实例如下所示:
#导入单个类 --------------------------------------------------------------------------------- test1.py --------------------------------------------------------------------------------- """一个可用于表示汽车的类""" class Car(): """一次模拟汽车的简单尝试""" def __init__(self,make,model,year): """初始化描述汽车的属性""" self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_descriptive_name(self): """返回整洁的描述性名称""" long_name = str(self.year) + ' ' + self.make + ' '+ self.model return long_name def read_odometer(self): """打印一条信息,指出汽车的里程""" print("This car has " + str(self.odometer_reading) + " miles on it.") def update_odometer(selfself,mileage): """ 将里程表读数设置为指定值 拒绝里程表往回拨 """ if mileage >= self.odometer_reading: self.odometer_reading = mileage else: print("You can't roll back an odometer!") def increment_odometer(self,miles): """将里程表读数增加指定的量""" self.odometer_reading += miles --------------------------------------------------------------------------------- test2.py --------------------------------------------------------------------------------- #在其他程序中调用模块中的类,需要声明,格式如下 #引用模块语句,打开文件test1,调用类Car from test1 import Car my_new_car = Car('audi','a4',2016) print(my_new_car.get_descriptive_name()) my_new_car.odometer_reading = 23 my_new_car.read_odometer() my_new_car = Car('audi','a4',2016) print(my_new_car.get_descriptive_name()) my_new_car.odometer_reading = 23 my_new_car.read_odometer() #输出结果如下所示: 2016 audi a4 This car has 23 miles on it. #在一个模块中存储多个类 --------------------------------------------------------------------------------- test1.py --------------------------------------------------------------------------------- """一个可用于表示汽车的类""" class Car(): """一次模拟汽车的简单尝试""" def __init__(self,make,model,year): """初始化描述汽车的属性""" self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_descriptive_name(self): """返回整洁的描述性名称""" long_name = str(self.year) + ' ' + self.make + ' '+ self.model return long_name def read_odometer(self): """打印一条信息,指出汽车的里程""" print("This car has " + str(self.odometer_reading) + " miles on it.") def update_odometer(selfself,mileage): """ 将里程表读数设置为指定值 拒绝里程表往回拨 """ if mileage >= self.odometer_reading: self.odometer_reading = mileage else: print("You can't roll back an odometer!") def increment_odometer(self,miles): """将里程表读数增加指定的量""" self.odometer_reading += miles class Battery(): """一次模拟电动汽车电瓶的简单尝试""" def __init__(self,battery_size = 70): """初始化电瓶的属性""" self.battery_size = battery_size def describe_battery(self): """打印一条描述电瓶容量的消息""" print("This car has a " + str(self.battery_size) + "-kWh battery.") def get_range(self): """打印一条描述电瓶续航里程的消息""" if self.battery_size == 70: range = 240 elif self.battery_size == 85: range = 270 message = "This car can go approximately " + str(range) message += " miles on a full charge." print(message) class ElectricCar(Car): """模拟电动汽车的独特之处""" def __init__(self,make,model,year): """ 初始化父类的属性,在初始化电动汽车特有的属性 """ super().__init__(make,model,year) self.battery = Battery() --------------------------------------------------------------------------------- test3.py --------------------------------------------------------------------------------- #一个模块多个类的调用,只需注明调用的类名即可 #引用模块语句,打开文件test1,调用类ElectricCar from test1 import ElectricCar my_tesla = ElectricCar('tesla','model s',2016) print(my_tesla.get_descriptive_name()) my_tesla.battery.describe_battery() my_tesla.battery.get_range() --------------------------------------------------------------------------------- test4.py --------------------------------------------------------------------------------- #导入整个模块 #先导入模块 import test1 #导入模块后,调用类只需要实例化的时候,用句点的方法引用,通过模块来调用类 my_beetly = test1.Car('volkswagen','beetle',2016) #调用类以后,在调用类中的方法 print(my_beetly.get_descriptive_name()) my_tesla = test1.ElectricCar('tesla','roadster',2016) print(my_tesla.get_descriptive_name()) #输出结果如下所示: #test3 2016 tesla model s This car has a 70-kWh battery. This car can go approximately 240 miles on a full charge. #test4 2016 volkswagen beetle 2016 tesla roadster