面向对象
2018-09-03 15:40:21 0 举报
AI智能生成
面向对象封装继承多态笔记,案例,思维导图
作者其他创作
大纲/内容
成员
变量
类变量/静态字段
实例变量
方法
实例方法
在类中创建,用对象去调用的方法<div>公有,私有</div>
类方法
@classmethod<div>公有,私有<br></div>
静态方法
@staticmethod<div>公有,私有<br></div>
特殊方法
__new__<div>__init__</div><div>__add__</div><div>__call__</div><div>__getitem__</div><div>__setitem__</div><div>__delitem__</div><div>__str__</div><div>__iter__</div><div>__dict__</div><div>__doc__</div><div>__enter__</div><div>__exit__</div><div><br></div>
属性
@property<div>公有,私有<br></div>
内置函数
issubclass
isinstance
type
callable
封装
封装数据
<pre style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: Menlo; font-size: 9pt;"><span style="color: rgb(0, 128, 128); font-weight: bold;">class Data:<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;"> def __init__(self,eat,drink,study):<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;"> self.eat = eat<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;"> self.drink = drink<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;"> self.study = study<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;">obj = Data("hamburger","salve","python")</span></pre>
封装方法/功能
<pre style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: Menlo; font-size: 9pt;"><span style="color: rgb(0, 128, 128); font-weight: bold;">class Foo:<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;"> def eat(self): #定义吃大餐的功能/方法<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;"> pass<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;"> def drink(self):#定义喝的功能<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;"> pass<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;"> def work(self): #定义工作的功能<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;"> pass<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;"> ...<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;">obj = Foo()<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;">obj.eat()调用吃的功能<br></span><span style="color: rgb(0, 128, 128); font-weight: bold;">obj.drink()调用喝东西的功能</span></pre>
继承
单继承
obj = Foo(Base)
多继承
obj = Foo(Base1,Base2...)<br>
super()
<div>super().f1()</div>多继承中,按继承顺序顺延找下一个f1方法<div>这个继承顺序可以打印类名.__mro__方法查看</div>
多态
<pre style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: Menlo; font-size: 9pt;"><span style="color: rgb(0, 128, 128); font-weight: bold;">动态语言调用实例方法,不检查类型,只要方法存在,参数正确,就可以调用。</span></pre>
0 条评论
下一页