本系列博文基于廖雪峰老师的官网Python教程,笔者在大学期间已经阅读过廖老师的Python教程,教程相当不错,官网链接: 廖雪峰官方网站.请需要系统学习Python的小伙伴到廖老师官网学习,笔者的编程环境是Anaconda+Pycharm,Python版本:Python3.
1.使用__slots__
# 正常情况下定义类# 1.定义classclass Employee(object): pass# 2.给实例绑定属性employee1 = Employee()employee1.name = "Willard"print("employee1.name属性为:", employee1.name)# 3.给实例绑定方法def setSalary(self, salary): self.salary = salary from types import MethodTypeemployee1.setSalary = MethodType(setSalary, employee1) # 给实例绑定方法employee1.setSalary(20000) # 调用实例方法print("employee1.salary结果为:", employee1.salary)# Tips:# a.给一个实例绑定方法,对另外一个实例不起作用;employee2 = Employee()# employee2.setSalary(10000) # 抛出错误# b.如果要给所有实例绑定方法,则需要给class绑定方法def setRank(self, rank): self.rank = rank Employee.setRank = setRankemployee1.setRank("A")print("employee1等级:", employee1.rank)employee2.setRank("B")print("employee2等级:", employee2.rank)
# 结果输出:employee1.name属性为: Willardemployee1.salary结果为: 20000employee1等级: Aemployee2等级: B
# 限制实例的属性,如:只允许对Employee实例添加name和salary属性;# 在定义class时,定义一个特殊的__slots__变量,限制该class实例能添加的属性;class Employee(object): __slots__ = ("name", "salary") employee1 = Employee()employee1.name = "ChenJD"employee1.salary = 20000# employee1.rank = "A" # 限制了只能添加name和salary属性,抛出错误# Tips:使用__slots__定义的属性仅对当前类实例起作用,对继承的子类不起作用;class OtherEmployee(Employee): passotherEmployee1 = OtherEmployee()otherEmployee1.rank = "A" # 可以绑定rank属性print("otherEmployee1可以绑定rank属性:",otherEmployee1.rank)# 结果输出:# otherEmployee1可以绑定rank属性: A
2.使用@property# 绑定参数时,对参数进行检查class Employee(object): def getSalary(self): return self._salary def setSalary(self, value): if not isinstance(value, int): raise ValueError("Salary must be an integer.") if value < 0: raise ValueError("Salary must be > 0.") self._salary = value employee1 = Employee()employee1.setSalary(20000)print("employee1.getSalary结果:", employee1.getSalary())# employee1.setSalary(-100) # 抛出错误# 使用@property装饰器# 1.把一个getter方法变成属性,只需要加上@property;# 2.@property本身又创建了另外一个装饰器@salary.setter,负责把一个setter方法变成属性赋值;class Employee(object): @property def salary(self): return self._salary @salary.setter def salary(self, value): if not isinstance(value, int): raise ValueError("Salary must be integer.") if value < 0: raise ValueError("Salary must be > 0.") self._salary = value employee2 = Employee()employee2.salary = 15000print("employee2.salary的结果:", employee2.salary)# employee1.setSalary(-100) # 抛出错误# 结果输出:# employee1.getSalary结果: 20000# employee2.salary的结果: 15000
3.多重继承# 设计一个动物分类:Dog(狗)、Bat(蝙蝠)、Parrot(鹦鹉)、Ostrich(鸵鸟);# 1.按照哺乳动物和鸟类归类"""Animal---Mammal------Dog------Bat---Bird------Parrot------Ostrich"""# 2.按照能跑和能飞归类"""Animal---Runnable------Dog------Ostrich---Flyable------Parrot------Bat"""# 采用多重继承分类# a.主要的类按照哺乳类和鸟类划分class Animal(object): pass# 大类class Mammal(Animal): passclass Bird(Animal): pass# 各种动物class Dog(Mammal, RunnableMixIn): passclass Bat(Mammal, FlyableMixIn): passclass Parrot(Bird, FlyableMixIn): pass# 让Ostrich除了继承Bird,同时继承Runnable,这种设计称为MixIn;# MixIn目的:给一个类增加多个功能;class Ostrich(Bird, RunnableMixIn): pass# 添加Runnable和Flyable功能class RunnableMixIn(object): def run(self): print("Running...") class FlyableMixIn(object): def fly(self): print("Flying...")