PythonCookBook 笔记 chapter-08-类02

1,利用委托替代继承

class Player:
    def attack(self, x):
        print('Player attack: ', x)
        
class Damage:
    def __init__(self):
        self._player = Player()
        
    def damage(self, x):
        print("damage: ", x)
        self._player.attack(x)

    def __getattr__(self, name):
        return getattr(self._player, name)

d = Damage()
d.damage(100)

out:
    damage:  100
    Player attack:  100

2,定义多个构造函数

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod # 类方法
    def Monitor(cls): #cls通常用作类方法的第一参数。通常用self来传递当前类对象的实例,cls传递当前类对象
        return cls("Admin", 100)

    def __str__(self):
        return 'Student: ({!s})'.format(self.name)


a = Student('Leon', 66)
print(a)
print(Student.Monitor())


out:
    Student: (Leon)
    Student: (Admin)

2,状态机

class LightState: # 状态基类
    @staticmethod
    def turn_on(light):
        raise NotImplementedError()

    @staticmethod
    def turn_off(light):
        raise NotImplementedError()

    @staticmethod
    def do_work(light):
        raise NotImplementedError()

class LightOnState: # 关灯状态,可以进行打开操作
    @staticmethod
    def turn_on(light):
        raise RuntimeError('Already on')

    @staticmethod
    def turn_off(light):
        light.new_state(LightOffState) # 将灯打开

    @staticmethod
    def do_work(light):
        print('The light is on, just do it !!!')

class LightOffState: # 灯亮状态,可以进行工作和关灯操作
    @staticmethod
    def turn_on(light):
        light.new_state(LightOnState)

    @staticmethod
    def turn_off(light):
        raise RuntimeError('Already off')

    @staticmethod
    def do_work(light):
         raise RuntimeError('light is not on')
        
class Light:
    def __init__(self):
        self.new_state(LightOffState) # 初始化状态

    def new_state(self, newstate): # 更改状态
        self._state = newstate # 委托操作,将特定操作交给另一个对象实现

    def turn_on(self): # 委托代理操作
        return self._state.turn_on(self)

    def turn_off(self):
        return self._state.turn_off(self)

    def do_work(self):
        return self._state.do_work(self)

l = Light()
print(l._state)
l.turn_on()
l.do_work()
l.turn_off()
l.do_work()

out:
    <class '__main__.LightOffState'>
    The light is on, just do it !!!
    Traceback (most recent call last):...
    RuntimeError: light is not on

另一种实现时直接修改实例的__class__属性

class Light: # 基类
    def __init__(self):
        self.new_state(LightOffState)

    def new_state(self, newstate):
        self.__class__ = newstate # 直接更改__class__属性

    def turn_on(self):
        raise NotImplementedError()

    def turn_off(self):
        raise NotImplementedError()

    def do_work(self):
        raise NotImplementedError()

class LightOnState(Light): # 继承Light类
    def turn_on(light):
        raise RuntimeError('Already on')

    def turn_off(light):
        light.new_state(LightOffState)

    def do_work(light):
        print('The light is on, just do it !!!')

class LightOffState(Light):
    def turn_on(light):
        light.new_state(LightOnState)

    def turn_off(light):
        raise RuntimeError('Already off')

    def do_work(light):
         raise RuntimeError('light is not on')

l = Light()
l.turn_on()
l.do_work()
l.turn_off()
l.do_work()

猜你喜欢

转载自blog.csdn.net/vitas_fly/article/details/80323830
今日推荐