加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python面向对象

发布时间:2020-12-16 23:57:39 所属栏目:Python 来源:网络整理
导读:__init__()方法是一种特殊的方法,被称为类的构造函数或初始化方法,当创建了这个类的实例时就会调用该方法 = = = = = = ( ( ( ( ( ( a1=Ticket( , , , , , ) a2=Ticket( , , ) (type(a1),a1) (a1.fdate) a1.code = (a1.code) a1.ftime = (a1.ftime) (a1.tti

__init__()方法是一种特殊的方法,被称为类的构造函数或初始化方法,当创建了这个类的实例时就会调用该方法
======(((((( a1=Ticket(,,,,,) a2=Ticket(,,) (type(a1),a1) (a1.fdate) a1.code = (a1.code) a1.ftime = (a1.ftime) (a1.ttime)

2.类的封装

类中把某些属性和方法隐藏起来(或者说定义成私有的),只在类的内部使用、外部无法访问

在python中用双下划线的方式实现隐藏属性(设置成私有的)

===qp self.= s1 [self.,self.,self. self. self. (.format(self.,self.,self.,===,=txt[:-2].split()[1= record =record.split(2],rlist[1],rlist[3],rlist[4])) =input(%=list(filter( len(fstations)==( len(fstations)==1(% len(fstations)>1( i ((i+1),,fstations[i].getCn(),sep==int(input()) - 1 (cfz=getstation(<span style="color: #800000">"<span style="color: #800000">出发<span style="color: #800000">"<span style="color: #000000">)
ddz
=getstation(<span style="color: #800000">"
<span style="color: #800000">到达
<span style="color: #800000">"
)

3.类的继承

面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过继承机制。通过继承创建的新类称为子类或派生类,被继承的类称为基类、父类或超类。

父类的属性和方法子类可以使用

prettytable ====== ===PrettyTable( ===</span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; insert(self): ptable</span>=PrettyTable(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;车次 出发站 到达站 出发时间 到达时间 软卧 硬卧 硬座 无座</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;.split()) ptable.add_row([self.id,self.ruanw,self.yingw,self.yingzuo,self.wu]) </span><span style="color: #0000ff"&gt;print</span><span style="color: #000000"&gt;(ptable)

g1=Gd(<span style="color: #800000">"<span style="color: #800000">北京<span style="color: #800000">",<span style="color: #800000">"<span style="color: #800000">上海<span style="color: #800000">",<span style="color: #800000">"<span style="color: #800000">10:00<span style="color: #800000">",<span style="color: #800000">"<span style="color: #800000">18:00<span style="color: #800000">",<span style="color: #800000">"<span style="color: #800000">G11<span style="color: #800000">",2,3,4<span style="color: #000000">)
g2=Gd(<span style="color: #800000">"<span style="color: #800000">北京<span style="color: #800000">",<span style="color: #800000">"<span style="color: #800000">G22<span style="color: #800000">",4<span style="color: #000000">)
t1=Tkz(<span style="color: #800000">"<span style="color: #800000">北京<span style="color: #800000">",<span style="color: #800000">"<span style="color: #800000">K112<span style="color: #800000">",4,6<span style="color: #000000">)
t2=Tkz(<span style="color: #800000">"<span style="color: #800000">北京<span style="color: #800000">",<span style="color: #800000">"<span style="color: #800000">郑州<span style="color: #800000">",<span style="color: #800000">"<span style="color: #800000">11:00<span style="color: #800000">",<span style="color: #800000">"<span style="color: #800000">K111<span style="color: #800000">",5,6<span style="color: #000000">)
g1.insert()
g2.insert()

4.多态

<span style="color: #0000ff">from prettytable <span style="color: #0000ff">import<span style="color: #000000"> PrettyTable
<span style="color: #0000ff">class<span style="color: #000000"> Ticket():
<span style="color: #0000ff">def <span style="color: #800080">init<span style="color: #000000">(self,self.to_time)
<span style="color: #0000ff">class<span style="color: #000000"> Gd(Ticket):
<span style="color: #0000ff">def <span style="color: #800080">init<span style="color: #000000">(self,wu)
self.yideng=<span style="color: #000000">yideng
self.erdeng=<span style="color: #000000">erdeng
<span style="color: #0000ff">def<span style="color: #000000"> display(self):
ptable=PrettyTable(<span style="color: #800000">"<span style="color: #800000">车次 出发站 到达站 出发时间 到达时间 一等座 二等座 无座<span style="color: #800000">"<span style="color: #000000">.split())
ptable.add_row([self.id,wu)
self.ruanw=<span style="color: #000000">ruanw
self.yingw=<span style="color: #000000">yingw
self.yingzuo=<span style="color: #000000">yingzuo

</span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; display(self):
    ptable</span>=PrettyTable(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;车次 出发站 到达站 出发时间 到达时间 软卧 硬卧 硬座 无座</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;.split())
    ptable.add_row([self.id,6<span style="color: #000000"&gt;)

<span style="color: #0000ff">for i <span style="color: #0000ff">in<span style="color: #000000"> [g1,g2,t1,t2]:
i.display()

通过上面的代码可以看到,不同的子类子类以及父类都有"display"方法,在代码末尾不同的对象调用相同的方法名,但结果却显示不同,这就是多态。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读