Python OOP-5
【摘要】 12. 所用软件
画图软件:Faststone capture
13. 抽象类
抽象方法:没有具体实现内容的方法称为抽象方法 抽象方法的主要意义是规范了子类的行为和接口 抽象的使用需要借助abc模块 import abc
1 抽象类:包含抽象方法的类叫做抽象类,通常成为ABC类 抽象类的使用 抽象类可以包含抽象方法,也可以包含具体方法抽象类中可以有方...
12. 所用软件
- 画图软件:Faststone capture
13. 抽象类
-
抽象方法:没有具体实现内容的方法称为抽象方法
-
抽象方法的主要意义是规范了子类的行为和接口
-
抽象的使用需要借助abc模块
import abc
- 1
-
抽象类:包含抽象方法的类叫做抽象类,通常成为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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
ruochen
- 1
# 类属性 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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
haha
我被读取了
haha
- 1
- 2
- 3
# 抽象
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()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
问一下对方
Kiss me
- 1
- 2
# 抽象类的实现
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......")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
# 函数名可以当变量使用
def sayHello(name): print("{}你好".format(name))
sayHello("A")
liumang = sayHello
liumang("A")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
A你好
A你好
- 1
- 2
# 自己组装一个类
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()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
Saying... ...
Saying... ...
Saying... ...
- 1
- 2
- 3
# 组装类例子 2
# 自己组装一个类
from types import MethodType
class A(): pass
def say(self): print("Saying... ...") a = A()
a.say = MethodType(say, A)
a.say()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
Saying... ...
- 1
help(MethodType)
- 1
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
type(8)
- 1
int
- 1
# 利用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()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
Saying... ...
- 1
# 元类演示
# 元类写法是固定的,他必须继承于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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
我是元类 '000000'
- 1
- 2
- 3
- 4
- 5
- 6
- 7
文章来源: ruochen.blog.csdn.net,作者:若尘,版权归原作者所有,如需转载,请联系作者。
原文链接:ruochen.blog.csdn.net/article/details/90442599
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)