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()


  1. 创建一条名字为'Willie' 、年龄为6 的小狗。
  2. ython使用实参'Willie' 和6 调用Dog 类的方法__init__() 。
  3. 方法__init__() 创建一个表示特定小狗的实例,并使用提供的值来设置属性name 和age 。
  4. 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} .")

练习9-1:餐馆 创建一个名为Restaurant 的类,为其方法__init__()设置属性restaurant_name 和cuisine_type 。创建一个名为describe_restaurant() 的方法和一个名为open_restaurant() 的方法,前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。

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()


练习题

练习9-4:就餐人数 在为完成练习9-1而编写的程序中,添加一个名为number_served 的属性,并将其默认值设置为0。根据这个类创建一个名为restaurant 的实例。打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。

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()

练习9-5:尝试登录次数 在为完成练习9-3而编写的User 类中,添加一个名为login_attempts 的属性。编写一个名为increment_login_attempts() 的方法,将属性login_attempts 的值加1。再编写一个名为reset_login_attempts() 的方法,将属性login_attempts 的值重置为0。

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()

现在,如果有人对电动汽车调用方法fill_gas_tank() ,Python将忽略Car 类中的方法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.")

在ElectricCar 类中,添加了一个名为self.battery 的属性(见❹)。这行代码让Python创建一个新的Battery 实例(因为没有指定容量,所以为默认值75),并将该实例赋给属性self.battery 。每当方法__init__() 被调用时,都将执行该操作,因此现在每个ElectricCar 实例都包含一个自动创建的Battery 实例。

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()

练习题

练习9-6:冰激凌小店 冰激凌小店是一种特殊的餐馆。编写一个名为IceCreamStand 的类,让它继承为完成练习9-1或练习9-4而编写的Restaurant 类。这两个版本的Restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为flavors 的属性,用于存储一个由各种口味的冰激凌组成的列表。编写一个显示这些冰激凌的方法。创建一个IceCreamStand 实例,并调用这个方法。

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()

练习9-7:管理员 管理员是一种特殊的用户。编写一个名为Admin 的类,让它继承为完成练习9-3或练习9-5而编写的User 类。添加一个名为privileges 的属性,用于存储一个由字符串(如"can add post" 、"candelete post" 、"can ban user" 等)组成的列表。编写一个名为show_privileges() 的方法,显示管理员的权限。创建一个Admin 实例,并调用这个方法

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())

练习题

练习9-10:导入Restaurant 类 将最新的Restaurant 类存储在一个模块中。在另一个文件中,导入Restaurant 类,创建一个Restaurant 实例并调用Restaurant 的一个方法,以确认import 语句正确无误

from class_restaurant import Restaurant as res
my_res = res('Harlot','master')
my_res.print_number_served()

练习9-11:导入Admin 类 以为完成练习9-8而做的工作为基础。将User类、Privileges 类和Admin 类存储在一个模块中,再创建一个文件,在其中创建一个Admin 实例并对其调用方法show_privileges() ,以确认一切都能正确运行

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)


Warning

练习题

练习9-13:骰子 创建一个Die 类,它包含一个名为sides 的属性,该属性的默认值为6。编写一个名为roll_die() 的方法,它打印位于1和骰子面数之间的随机数。创建一个6面的骰子再掷10次。

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}.")

练习9-15:彩票分析 可以使用一个循环来明白前述彩票大奖有多难中奖。为此,创建一个名为my_ticket 的列表或元组,再编写一个循环,不断地随机选择数或字母,直到中大奖为止。请打印一条消息,报告执行循环多少次才中了大奖

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