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
()
a
.name
= "ruochen"
print(a
.name
)
del a
.name
ruochen
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"
def fdel(self
):
pass
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... ...
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
def say(self
):
print("Saying... ...")
def talk(self
):
print("Talking... ...")
A
= type("AName", (object, ), {"class_say":say
, "class_talk":talk
})
a
= A
()
dir(a
)
a
.class_say
()
Saying... ...
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'