Python编程-第九章-类
面向对象编程
对象
面向对象编程 是最有效的软件编写方法之一。在面向对象编程中,你编写表示现实世界中的事物和情景的类,并基于这些类来创建对象编写类时,你定义一大类对象都有的通用行为。基于类创建对象 时,每个对象都自动具备这种通用行为,然后可根据需要赋予每个对象独特的个性。使用面向对象编程可模拟现实情景,其逼真程度达到了令人惊讶的地步。
实例化
创建和使用类
创建Dog 类
"""定义一个类:
类名称首字母必须大写。
"""
class Dog:
"""A simple attempt to simulate a puppy. """
def __init__(self,name,age):
"""Initialize the properties name and age. """
self.name = name
self.age = age
def sit(self):
"""Simulate the puppy to crouch when commanded. """
def roll_over(self):
"""Simulate the puppy to roll when commanded. """
print(f"{self.name} rolled over!")
方法__init__()
1处的方法__init__() 是一个特殊方法,每当你根据Dog 类创建新实例时,Python都会自动运行它。
创建实例
class Dog:
"""A simple attempt to simulate a puppy. """
def __init__(self, name, age):
"""Initialize the properties name and age. """
self.name = name
self.age = age
def sit(self):
"""Simulate the puppy to crouch when commanded. """
print(f"{self.name} is now siting")
def roll_over(self):
"""Simulate the puppy to roll when commanded. """
print(f"{self.name} rolled over!")
my_dog = Dog('While', 6)
my_dog.sit()
my_dog.roll_over()
- 创建一条名字为'Willie' 、年龄为6 的小狗。
- ython使用实参'Willie' 和6 调用Dog 类的方法__init__() 。
- 方法__init__() 创建一个表示特定小狗的实例,并使用提供的值来设置属性name 和age 。
- Python返回一个表示这条小狗的实例,而我们将这个实例赋给了变量my_dog 。
class Dog:
"""A simple attempt to simulate a puppy. """
def __init__(self, name, age):
"""Initialize the properties name and age. """
self.name = name
self.age = age
def sit(self):
"""Simulate the puppy to crouch when commanded. """
print(f"{self.name} is now siting")
def roll_over(self):
"""Simulate the puppy to roll when commanded. """
print(f"{self.name} rolled over!")
my_dog = Dog('While', 6)
your_dog = Dog('lucy', 3)
print(f"My dog's name is {my_dog.name}.")
print(f"My dog's {my_dog.age} years old.")
print(f"\nYour dog's is {your_dog.name}.")
print(f"Your dog is {your_dog.age} years old.")
练习题
class Restaurant:
def __init__(self, restaurant_name, cuisine_type):
"""Initialize the properties name and age. """
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
"""Simulate the puppy to crouch when commanded. """
print(f"{self.restaurant_name} is now siting")
def open_restaurant(self):
"""Simulate the puppy to roll when commanded. """
print(f"{self.cuisine_type} rolled over!")
from class_restaurant import Restaurant
my_restaurant = Restaurant('aming', 'meta')
print(f"My restaurant name is {my_restaurant.restaurant_name}.")
print(f"My restaurant's {my_restaurant.cuisine_type} .")
from class_restaurant import Restaurant
my_restaurant = Restaurant('aming', 'meta')
print(f"My restaurant name is {my_restaurant.restaurant_name}.")
print(f"My restaurant's {my_restaurant.cuisine_type} .")
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
练习9-3:用户 创建一个名为User 的类,其中包含属性first_name 和last_name ,以及用户简介通常会存储的其他几个属性。在类User 中定义一
class User:
def __init__(self, first_name, last_name):
"""Initialize the properties name and age. """
self.first_name = first_name
self.last_name = last_name
def describe_user(self):
"""Simulate the puppy to crouch when commanded. """
print(f"username is {self.first_name} {self.last_name}.")
def greet_user(self):
"""Simulate the puppy to roll when commanded. """
print(f"Login please {self.last_name}.")
from class_user import User
alice=User('alice','makr')
alice.greet_user()
alice.describe_user()
使用类和实例
Car 类
class Car:
"""A simple attempt to simulate a car. """
def __init__(self,make,model,year):
"""Initialize the attributes describing the car. """
self.make = make
self.model = model
self.year = year
def get_descriptive_name(self):
"""Returns neat descriptive information. """
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
from class_car import Car
my_new_car = Car('audi','a4',2019)
print(my_new_car.get_descriptive_name())
给属性指定默认值
class Car:
"""A simple attempt to simulate a car. """
def __init__(self,make,model,year):
"""Initialize the attributes describing the car. """
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""Returns neat descriptive information. """
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
"""Print the mileage information of the car. """
print(f"This car has {self.odometer_reading} miles on it")
from class_car import Car
my_new_car = Car('audi','a4',2019)
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer()
修改属性的值
通过属性修改值
from class_car import Car
my_new_car = Car('audi', 'a4', 2019)
print(my_new_car.get_descriptive_name())
my_new_car.odometer_reading = 23
my_new_car.read_odometer()
通过方法修改属性值
class Car:
"""A simple attempt to simulate a car. """
def __init__(self,make,model,year):
"""Initialize the attributes describing the car. """
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""Returns neat descriptive information. """
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
"""Print the mileage information of the car. """
print(f"This car has {self.odometer_reading} miles on it")
def update_odometer(self,mileage):
self.odometer_reading = mileage
from class_car import Car
my_new_car = Car('audi', 'a4', 2019)
print(my_new_car.get_descriptive_name())
my_new_car.update_odometer(50)
my_new_car.read_odometer()
加上逻辑判断不允许里程表往回调
class Car:
"""A simple attempt to simulate a car. """
def __init__(self,make,model,year):
"""Initialize the attributes describing the car. """
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""Returns neat descriptive information. """
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
"""Print the mileage information of the car. """
print(f"This car has {self.odometer_reading} miles on it")
def update_odometer(self,mileage):
"""Set the odometer reading to the specified value. """
"""It is prohibited to adjust the odometer reading back. """
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll bakc an odometer!")
通过方法对属性值进行递增
时候需要将属性值递增特定的量,而不是将其设置为全新的值。假设我们购买了一辆二手车,且从购买到登记期间增加了100英里的里程。下面的方法让我们能够传递这个增量,并相应地增大里程表读数:
class Car:
"""A simple attempt to simulate a car. """
def __init__(self, make, model, year):
"""Initialize the attributes describing the car. """
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""Returns neat descriptive information. """
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
"""Print the mileage information of the car. """
print(f"This car has {self.odometer_reading} miles on it")
def update_odometer(self, mileage):
"""Set the odometer reading to the specified value. """
"""It is prohibited to adjust the odometer reading back. """
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll bakc an odometer!")
def increment_odometer(self, mileage):
"""Increases the odometer reading to the specified amount. """
self.odometer_reading += mileage
from class_car import Car
my_new_car = Car('audi', 'a4', 2019)
print(my_new_car.get_descriptive_name())
my_new_car.update_odometer(50)
my_new_car.read_odometer()
my_new_car.increment_odometer(100)
my_new_car.read_odometer()
练习题
class Restaurant:
def __init__(self, restaurant_name, cuisine_type):
"""Initialize the properties name and age. """
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
"""Simulate the puppy to crouch when commanded. """
print(f"{self.restaurant_name} is now siting")
def open_restaurant(self):
"""Simulate the puppy to roll when commanded. """
print(f"{self.cuisine_type} rolled over!")
def print_number_served(self):
print(f"This restaurant has served { self.number_served} is serverd.")
def update_number_serverd(self,number):
if number >= self.number_served:
self.number_served = number
else:
print(f"You cannot call back the number of diners.")
def increment_number_served(self,number):
if number >= 0:
self.number_served += number
else:
print(f"Please enter a positive integer.")
from class_restaurant import Restaurant
rest = Restaurant('banana', 'meta')
rest.print_number_served()
rest.update_number_serverd(10)
rest.print_number_served()
rest.increment_number_served(100)
rest.print_number_served()
class User:
def __init__(self, first_name, last_name):
"""Initialize the properties name and age. """
self.first_name = first_name
self.last_name = last_name
self.login_attempts = 0
def describe_user(self):
"""Simulate the puppy to crouch when commanded. """
print(f"username is {self.first_name} {self.last_name}.")
def greet_user(self):
"""Simulate the puppy to roll when commanded. """
print(f"Login please {self.last_name}, {self.login_attempts}")
def increment_login_attempts(self, number):
self.login_attempts += number
def reset_loging_attempts(self):
self.login_attempts = 0
继承
创建子类
from class_car import Car
class EletricCar(Car):
"""The uniqueness of electric vehicles. """
def __init__(self, make, model, year):
"""Initialize the parent type. """
super().__init__(make, model, year)
my_tesla = EletricCar('tesla', 'model', 2019)
my_tesla.update_odometer(10)
my_tesla.read_odometer()
给子类添加属性和方法
from class_car import Car
class EletricCar(Car):
"""The uniqueness of electric vehicles. """
def __init__(self, make, model, year):
"""Initialize the parent type. """
super().__init__(make, model, year)
self.battery_size = 75
def describe_battery(self):
"""Print a message describing the battery capacity. """
print(f"This car has a {self.battery_size}-kWh battery.")
my_tesla = EletricCar('tesla', 'model s',2019)
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()
my_tesla = EletricCar('tesla', 'model', 2019)
my_tesla.update_odometer(10)
my_tesla.read_odometer()
重写父类的方法
对于父类的方法,只要它不符合子类模拟的实物的行为,都可以进行重写。为此,可在子类中定义一个与要重写的父类方法同名的方法。这样,Python将不会考虑这个父类方法,而只关注你在子类中定义的相应方法
在父类定义方法
class Car:
"""A simple attempt to simulate a car. """
def __init__(self, make, model, year):
"""Initialize the attributes describing the car. """
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""Returns neat descriptive information. """
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
"""Print the mileage information of the car. """
print(f"This car has {self.odometer_reading} miles on it")
def update_odometer(self, mileage):
"""Set the odometer reading to the specified value. """
"""It is prohibited to adjust the odometer reading back. """
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll bakc an odometer!")
def increment_odometer(self, mileage):
"""Increases the odometer reading to the specified amount. """
self.odometer_reading += mileage
def fill_gas_tank(self):
"""Electric cars don't have a fuel tank. """
print("The gas is full")
在子类中重定义子类
from class_car import Car
class EletricCar(Car):
"""The uniqueness of electric vehicles. """
def __init__(self, make, model, year):
"""Initialize the parent type. """
super().__init__(make, model, year)
self.battery_size = 75
def describe_battery(self):
"""Print a message describing the battery capacity. """
print(f"This car has a {self.battery_size}-kWh battery.")
def fill_gas_tank(self):
"""Electric cars don't have a fuel tank. """
print("This car doesn't need a gas tank!")
my_tesla = EletricCar('tesla', 'model s',2019)
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()
my_tesla = EletricCar('tesla', 'model', 2019)
my_tesla.update_odometer(10)
my_tesla.read_odometer()
my_tesla.fill_gas_tank()
将实例用于属性
使用代码模拟实物时,你可能会发现自己给类添加的细节越来越多:属性和方法清单以及文件都越来越长。在这种情况下,可能需要将类的一部分提取出来,作为一个独立的类。可以将大型类拆分成多个协同工作的小类。
例如,不断给ElectricCar 类添加细节时,我们可能发现其中包含很多专门针对汽车电瓶的属性和方法。在这种情况下,可将这些属性和方法提取出来,放到一个名为Battery 的类中,并将一个Battery 实例作为ElectricCar 类的属性:
class Battery:
"""A simple attempt to simulate an electric car battery. """
def __init__(self, battery_size=75):
"""Initialize the properties of the battery. """
self.battery_size = battery_size
def describe_batterry(self):
"""Print a message describing the battery capacity. """
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 on a full charge.")
from class_car import Car
from class_battery import Battery
class EletricCar(Car):
"""The uniqueness of electric vehicles. """
def __init__(self, make, model, year):
"""Initialize the parent type. """
super().__init__(make, model, year)
self.battery = Battery()
def describe_battery(self):
"""Print a message describing the battery capacity. """
print(f"This car has a {self.battery_size}-kWh battery.")
def fill_gas_tank(self, percentage):
"""Electric cars don't have a fuel tank. """
print(f"邮箱还剩{percentage}.")
my_tesla = EletricCar('tesla','model s','2019')
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_batterry()
my_tesla.battery.get_range()
练习题
from class_restaurant import Restaurant
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type):
super().__init__(restaurant_name, cuisine_type)
self.flavors = ['strawberry', 'vanilla', 'durian']
def list_icecream(self):
for ices in self.flavors:
print(f"We have the following ice cream:{ices}")
from icecreamstand import IceCreamStand
my_ice = IceCreamStand('list', 'type')
my_ice.list_icecream()
from class_user import User
class Admin(User):
def __init__(self, first_name, last_name):
super().__init__(first_name, last_name)
self.privileges = f"can add post,can delete post, can ban user."
def show_privileges(self):
print(f"Admin privileges is {self.privileges}")
from class_admin import Admin
admin = Admin('chen', 'cheng')
admin.show_privileges()
一个模块包含多个类
导入类
from class_car import ElectricCar
my_tesla = ElectricCar('tesla','model s', 2019)
print(my_tesla.get_descriptive_name())
my_tesla.batterru.describe_batterry()
my_tesla.batterru.get_range()
类文件
class Car:
"""A simple attempt to simulate a car. """
def __init__(self, make, model, year):
"""Initialize the attributes describing the car. """
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""Returns neat descriptive information. """
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
"""Print the mileage information of the car. """
print(f"This car has {self.odometer_reading} miles on it")
def update_odometer(self, mileage):
"""Set the odometer reading to the specified value. """
"""It is prohibited to adjust the odometer reading back. """
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll bakc an odometer!")
def increment_odometer(self, mileage):
"""Increases the odometer reading to the specified amount. """
self.odometer_reading += mileage
class Battery:
"""A simple attempt to simulate an electric car battery. """
def __init__(self, battery_size=75):
"""Initialize the properties of the battery. """
self.range = 100
self.battery_size = battery_size
def describe_batterry(self):
"""Print a message describing the battery capacity. """
print(f"This car has a {self.battery_size}-kWh battery.")
def get_range(self):
if self.battery_size == 75:
self.range = 260
elif self.battery_size == 100:
self.range = 315
print(f"This car is {self.range} miles on a full chared")
class ElectricCar(Car):
def __init__(self,make,model,year):
super().__init__(make,model,year)
self.batterru = Battery()
一个模块导入多个类
from class_car import ElectricCar,Car
my_tesla = ElectricCar('tesla','model s', 2019)
print(my_tesla.get_descriptive_name())
my_tesla.batterru.describe_batterry()
my_tesla.batterru.get_range()
my_beetle = Car('volkswagen','beetle',2019)
print(my_beetle.get_descriptive_name())
导入整个模块
import class_car
my_tesla = ElectricCar('tesla','model s', 2019)
print(my_tesla.get_descriptive_name())
my_tesla.batterru.describe_batterry()
my_tesla.batterru.get_range()
my_beetle = Car('volkswagen','beetle',2019)
print(my_beetle.get_descriptive_name())
导入模块的所有类
from class_car import *
my_tesla = ElectricCar('tesla','model s', 2019)
print(my_tesla.get_descriptive_name())
my_tesla.batterru.describe_batterry()
my_tesla.batterru.get_range()
my_beetle = Car('volkswagen','beetle',2019)
print(my_beetle.get_descriptive_name())
练习题
from class_restaurant import Restaurant as res
my_res = res('Harlot','master')
my_res.print_number_served()
from class_admin import Admin
my_admin = Admin('chen','cheng')
my_admin.show_privileges()
Python标准库
from random import randint
print(randint(1,6))
from random import choice
playes = ['charles','martina','michael','florence','eli']
first_up = choice(playes)
print(first_up)
练习题
from random import randint
class Die:
def __init__(self,sides = 6):
self.sides = sides
def roll_die(self):
print(randint(1, self.sides))
my_dice=Die(20)
my_dice.roll_die()
练习9-14:彩票 创建一个列表或元组,其中包含10个数和5个字母。从这个列表或元组中随机选择4个数或字母,并打印一条消息,指出只要彩票上是这4个数或字母,就中大奖了
from random import choices
lottery_numbers=['1','2','3','4','5','6','7','8','9','10']
middle_number = []
i = 0
while i <=4:
num = choices(lottery_numbers)
middle_number.append(num)
i += 1
print(f"中奖号码是{middle_number}.")
from random import choices
lottery_numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
middle_number = []
my_ticket = ['5', '7', '9', '3']
while True:
i = 0
n = 0
while i <= 4:
num = choices(lottery_numbers)
middle_number.append(num)
i += 1
print(middle_number)
if my_ticket == middle_number:
print(f"Congratulations on your jackpot, the jackpot number is {middle_number}")
print(f"total attempts {n} times")
break
else:
middle_number.clear()
print(middle_number)
n += 1