12. 所用软件
- 画图软件:Faststone capture
13. 抽象类
- 形象办法:没有具体实现内容的办法称为形象办法
- 形象办法的次要意义是标准了子类的行为和接口
-
形象的应用须要借助abc模块
import abc
- 抽象类:蕴含形象办法的类叫做抽象类,通常成为ABC类
-
抽象类的应用
- 抽象类能够蕴含形象办法,也能够蕴含具体方法
- 抽象类中能够有办法也能够有属性
- 抽象类不容许间接实例化
- 必须继承才能够应用,且继承的子类必须实现所有继承来的形象办法
- 假设子类没有实现所有继承的形象办法,子类也不能实例化
- 抽象类的次要作用是设定类的规范,以便于开发的时候具备对立的标准
14. 自定义类
- 类其实是一个类定义和各种办法的自由组合
- 能够定义类和函数,而后本人通过类来间接赋值
- 能够借助于MethodType实现
- 借助于type实现
-
利用元类实现- MetaClass
- 元类是类
- 被用来发明别的类
# 变量的三种用法 class A(): def __init__(self): self.name = "haha" self.age = 18 a = A() # 属性的三种用法 # 1. 赋值 # 2. 读取 # 3. 删除 a.name = "ruochen" print(a.name) del a.name # print(a.name)
ruochen
# 类属性 property # 利用场景: # 对变量除了一般的三种操作,还想减少一些附加的操作,那么能够通过property实现 class A(): def __init__(self): self.name = "haha" self.age = 18 # 此性能,是对类变量读取操作的时候应该执行的函数性能 def fget(self): print("我被读取了") return self.name # 模仿的是对变量进行写操作的时候执行的性能 def fset(self, name): print("我被写入了,然而还能够做好多事件") self.name = name + "1" # fdel模仿的是删除变量的时候进行的操作 def fdel(self): pass # property的四个参数程序的固定的 # 第一个参数代表读取的时候须要调用的函数 # 第二个参数代表写入的时候须要调用的函数 # 第三个是删除 name2 = property(fget, fset, fdel, "这是一个property的例子") a = A() print(a.name) print(a.name2)
haha 我被读取了 haha
# 形象 class Animel(): def sayHello(self): pass class Dog(Animel): def sayHello(self): print("问一下对方") class Person(Animel): def sayHello(self): print("Kiss me") d = Dog() d.sayHello() p = Person() p.sayHello()
问一下对方 Kiss me
# 抽象类的实现 import abc # 申明一个类并且指定以后类的元类 class Hunam(metaclass=abc.ABCMeta): # 定义一个形象的办法 @abc.abstractmethod def smoking(self): pass # 定义类形象办法 @abc.abstractclassmethod def drink(): pass # 定义动态形象办法 @abc.abstractstaticmethod def play(): pass def sleep(self): print("Sleeping......")
# 函数名能够当变量应用 def sayHello(name): print("{}你好".format(name)) sayHello("A") liumang = sayHello liumang("A")
A你好 A你好
# 本人组装一个类 class A(): pass def say(self): print("Saying... ...") class B(): def say(self): print("Saying... ...") say(9) A.say = say a = A() a.say() b = B() b.say()
Saying... ... Saying... ... Saying... ...
# 组装类例子 2 # 本人组装一个类 from types import MethodType class A(): pass def say(self): print("Saying... ...") a = A() a.say = MethodType(say, A) a.say()
Saying... ...
help(MethodType)
Help on class method in module builtins: class method(object) | method(function, instance) | | Create a bound instance method object. | | Methods defined here: | | __call__(self, /, *args, **kwargs) | Call self as a function. | | __delattr__(self, name, /) | Implement delattr(self, name). | | __eq__(self, value, /) | Return self==value. | | __ge__(self, value, /) | Return self>=value. | | __get__(self, instance, owner, /) | Return an attribute of instance, which is of type owner. | | __getattribute__(self, name, /) | Return getattr(self, name). | | __gt__(self, value, /) | Return self>value. | | __hash__(self, /) | Return hash(self). | | __le__(self, value, /) | Return self<=value. | | __lt__(self, value, /) | Return self<value. | | __ne__(self, value, /) | Return self!=value. | | __reduce__(...) | Helper for pickle. | | __repr__(self, /) | Return repr(self). | | __setattr__(self, name, value, /) | Implement setattr(self, name, value). | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __func__ | the function (or other callable) implementing a method | | __self__ | the instance to which a method is bound
type(8)
int
# 利用type造一个类 # 先定义类应该具备的成员函数 def say(self): print("Saying... ...") def talk(self): print("Talking... ...") # 利用type来创立一个类 A = type("AName", (object, ), {"class_say":say, "class_talk":talk}) # 而后能够像失常一样应用类 a = A() dir(a) a.class_say()
Saying... ...
# 元类演示 # 元类写法是固定的,他必须继承于type # 元类个别命名以MetaClass结尾 class CMetaClass(type): # 留神以下写法 def __new__(cls, name, bases, attrs): # 本人的业务解决 print("我是元类") attrs['id'] = '000000' attrs['addr'] = "1" return type.__new__(cls, name, bases, attrs) # 元类定义完就能够应用,应用留神写法 class Teacher(object, metaclass=CMetaClass): pass t = Teacher() t.__dict__ t.id
我是元类 '000000'