前言
今天学习类的相关知识
一、创建和使用类
1. 类的名称首字母要大写,实例的名称首字母要小写。
2. 方法__init__()是一种特殊的方法,每当根据Dog类创建新的实例时,都会自动运行该方法。而其他的方法需要在下面主程序中专门写一条才能运行,如my_dog.sit(),不写不运行。
3. self. 变量:该类型构成的变量可供类中所有的方法使用。
示例:
class Dog:
def __init__(self, name, age): #init两侧各有两个下划线,不是两侧各一个!!!谨记
self.name = name
self.age = age
def sit(self):
print (f'{self.name} is sitting!')
def roll_over(self):
print (f'{self. name} is rolling over')
my_dog = Dog('white', 6)
print (f'my dog name is {my_dog.name}')
print (f'my dog age is {my_dog.age}')
my_dog.sit()
my_dog.roll_over()
your_dog = Dog('Lucy', 8)
print(f'your dog name is {your_dog.name}')
print (f'your dog age is {your_dog.age}')
your_dog.sit()
your_dog.roll_over()
一定要记得,init两个各有两个下划线,不是一个!!!
结果:
my dog name is white
my dog age is 6
white is sitting!
white is rolling over
your dog name is Lucy
your dog age is 8
Lucy is sitting!
Lucy is rolling over
注意:
1. 访问属性的格式:实例(my_dog). 与该实例相关的参数(name, age)
2. 调用方法格式:即调用类中def定义的函数。实例(my_dog). 要调用的方法()
3. 可以有多个实例(my_dog, your_dog)
练习:9-1 9-2
代码:
class Restaurant:
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print (f"the restaurant's name is {self.restaurant_name}")
print (f"the cuisine's type is {self.cuisine_type}")
def open_restaurant(self):
print (f"{self.restaurant_name} is open")
restaurant = Restaurant('Zeyu Mu', 'Chinese food')
print (f"name is {restaurant.restaurant_name}")
print (f"type is {restaurant.cuisine_type}")
restaurant.describe_restaurant()
restaurant.open_restaurant()
结果:
name is Zeyu Mu
type is Chinese food
the restaurant's name is Zeyu Mu
the cuisine's type is Chinese food
Zeyu Mu is open
二、修改属性
1. 可以认为在__init__()部分下的内容是对形参的初始化,用到的属性不一定要显示在括号中,当不显示在括号中时,需要对其进行赋值。
2. 修改属性的方法有两种:
(1)直接修改属性的值:
示例:对汽车行驶里程进行修改
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 = f'{self.make} {self.year} {self.model}'
print (long_name)
return long_name.title()
def read_odometer(self):
print (f'The car has {self.odometer_reading} miles on it')
my_car = Car('audi', 'a4', 2022)
my_car.get_descriptive_name()
my_car.odometer_reading = 23 #对属性进行修改
my_car.read_odometer()
结果“
audi 2022 a4
The car has 23 miles on it
注意:要先对属性进行赋值,然后执行其对应的方式,才可得到想要的结果。不能只赋值,不执行。
(2)通过方法修改属性的值
示例:对汽车行驶里程进行修改,且不允许数值变小
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 = f'{self.make} {self.year} {self.model}'
print (long_name)
return long_name.title()
def updata_odometer(self, updata): #对里程数进行赋值,且不允许数值变小
if updata >= self.odometer_reading:
self.odometer_reading = updata #该语句是关键,可以不做条件判断
else:
print ('error!')
def read_odometer(self):
print (f'The car has {self.odometer_reading} miles on it')
my_car = Car('audi', 'a4', 2022)
my_car.get_descriptive_name()
my_car.updata_odometer(23) #对属性进行修改
my_car.read_odometer()
结果
audi 2022 a4
The car has 23 miles on it
练习: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 (f'\nname is {self.restaurant_name}')
print (f'\ntype is {self.cuisine_type}')
def open_restaurant(self):
print (f'\n{self.restaurant_name} is open')
def set_number_served(self, number):
self.number_served = number
def read_number_served(self):
print (f'number is {self.number_served}')
def increment_number_served(self,add):
self.number_served += add
restaurant = Restaurant('Zeyu Mu', 'Chinese food')
restaurant.open_restaurant()
restaurant.describe_restaurant()
restaurant.set_number_served(100)
restaurant.read_number_served()
num = restaurant.increment_number_served(100)
restaurant.read_number_served()
结果:
Zeyu Mu is open
name is Zeyu Mu
type is Chinese food
number is 100
number is 200
三、继承
1. 编写类时,并非要从空白开始。一个类继承另一个类时,将制动获得另一个类的所有属性和方法。原有的类称为父类,新类称为子类。
2. 定义子类时,必须在圆括号内指定父类的名称。
示例:在上文汽车的类下发展电动汽车类
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def get_descriptive_name(self):
long_name = f'{self.make} {self.year} {self.model}'
print (long_name)
return long_name.title()
class ElectricCar(Car):
def __init__(self, make, model, year): #非常关键的一步
super().__init__(make, model, year) #非常关键的一步
self.battery_size = 75
def describe_battery(self):
print(f'this car has a {self.battery_size} kwh battery')
my_car = Car('audi', 'a4', 2022)
my_car.get_descriptive_name()
my_tesla = ElectricCar(('alice'), 'a8', 2022)
my_tesla.describe_battery()
结果:
audi 2022 a4
this car has a 75 kwh battery
注意:
(1) 方法__init__()接收创建Car实例所需的信息
(2) super()使我们能调用父类的方法。该行代码使其能调用Car类的方法__init__(),让ElectricCar实例包含这个方法中定义的所有属性。
3. 将实例用作属性
该方法能将大类拆分成多个协同工作的小类
示例:
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def get_descriptive_name(self):
long_name = f'{self.make} {self.year} {self.model}'
print (long_name)
return long_name.title()
class Battery:
def __init__(self, battery_size = 75):
self.battery_size = battery_size
def describe_battery(self):
print(f'this car has a {self.battery_size} kwh battery')
def get_range(self):
if self.battery_size == 75:
range = 260
elif self.battery_size == 100:
range = 315
print (f'This car can go about {range} miles')
class ElectricCar(Car):
def __init__(self, make, model, year):
super().__init__(make, model, year)
self.battery = Battery() #非常关键的一步
my_tesla = ElectricCar('alice', 'a8', 2022)
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
结果:
this car has a 75 kwh battery
This car can go about 260 miles
总结
本次学习没有遇到问题,比较顺利!继续努力呀!