Python3.5面向?qū)ο笈c繼承圖文實(shí)例詳解
本文實(shí)例講述了Python3.5面向?qū)ο笈c繼承。分享給大家供大家參考,具體如下:
1、編程的方式

2、面向?qū)ο蟮幕靖拍?/strong>



3、類的基本概念



4、類的定義與調(diào)用——簡(jiǎn)單代碼舉例
注:建議類名的開頭字母用大寫,在Python中,類內(nèi)的函數(shù)稱作方法,類外的函數(shù)還是稱作函數(shù)。




#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
#類
class Person:
i = 10 #屬性(變量)
def eat(self): #方法(函數(shù))
print("eating...")
pass
#類的調(diào)用
a = Person()
a.eat()
運(yùn)行結(jié)果:
eating...
class Person():
#對(duì)象屬性 構(gòu)造方法 在實(shí)例化對(duì)象時(shí)會(huì)自動(dòng)調(diào)用
# 實(shí)例化的對(duì)象就具有name和age兩個(gè)屬性
#self是指當(dāng)前的對(duì)象 self不是關(guān)鍵字可以被代替,但是習(xí)慣使用self指代當(dāng)前對(duì)象
def __init__(self,name,age):
# 通過構(gòu)造方法聲明了兩個(gè)對(duì)象屬性
#對(duì)象.name屬性 = name參數(shù)
self.name = name
self.age = age
#聲明一個(gè)類方法
def speak(self):
print("Hello,my name is %s,and I'm %d years old" %(self.name,self.age))
#創(chuàng)建實(shí)例對(duì)象 會(huì)觸發(fā)構(gòu)造方法
people01 = Person("Jack",18) #通過Person類實(shí)例化出一個(gè)people對(duì)象
print(people01) #打印Person對(duì)象在內(nèi)存中的地址
print(people01.name,people01.age) #打印對(duì)象的屬性
#給對(duì)象添加屬性
people01.sex = "F"
print(people01.sex)
#類方法的調(diào)用
people01.speak()
運(yùn)行結(jié)果:
<__main__.Person object at 0x0059C5B0>
Jack 18
F
Hello,my name is Jack,and I'm 18 years old
5、類的方法


示例代碼:
#方法——靜態(tài)方法
class S():
#實(shí)例(對(duì)象)屬性
def __init__(self,name,age): #self一般指實(shí)例對(duì)象
self.name = name
self.age = age
@staticmethod #用staticmethod裝飾器修飾 表示test2為靜態(tài)方法
def test2(): #不能傳入self 對(duì)象的引用
print("test2...")
s1 = S("Joe",18)
s1.test2() #通過實(shí)例調(diào)用靜態(tài)方法
S.test2() #通過類名調(diào)用靜態(tài)方法
#方法——類方法
class C():
#類屬性
country = "China"
#實(shí)例(對(duì)象)屬性
def __init__(self,name,age):
self.name = name
self.age = age
@classmethod #用classmethod裝飾器修飾 表示test3為類方法
def test3(cls): #cls指的是類
print("test3...",cls.country) #類方法調(diào)用類屬性
c1 = C("Jack",18)
c1.test3() #通過實(shí)例調(diào)用類方法
C.test3() #通過類名調(diào)用類方法
運(yùn)行結(jié)果:
test1...
test2...
test2...
test3... China
test3... China

(1)構(gòu)造方法:構(gòu)造方法不能重載(被覆蓋)
在Python中內(nèi)置,每一個(gè)類都有一個(gè)默認(rèn)的不帶參數(shù)的構(gòu)造方法,不需要人為的單獨(dú)調(diào)用,在調(diào)用類的同時(shí)就運(yùn)行了構(gòu)造方法。
構(gòu)造方法的作用:初始化數(shù)據(jù)、創(chuàng)建對(duì)象(構(gòu)造方法的調(diào)用)

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
class Person:
def __init__(self):
print("構(gòu)造方法")
pass
Person() #類的調(diào)用--創(chuàng)建對(duì)象
運(yùn)行結(jié)果
構(gòu)造方法
帶參數(shù)的構(gòu)造方法:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
class Person:
# def __init__(self):
# print("構(gòu)造方法")
def __init__(self,x):
print("帶參數(shù)的構(gòu)造方法:",x)
def add(self,x,y):
print(x+y)
pass
zs = Person("hello") #類的調(diào)用--創(chuàng)建對(duì)象
zs.add(1,2)
運(yùn)行結(jié)果:
帶參數(shù)的構(gòu)造方法: hello
3
(2)面向?qū)ο蟮乃悸?/strong>

(3)類方法
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
#類方法
class Person:
def eat(self): #類方法
print("eating...")
pass
a = Person() #類方法調(diào)用
a.eat()
運(yùn)行結(jié)果:
eating...
(4)私有方法
只允許在類的內(nèi)部使用,專門為類服務(wù)的。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
class Person:
def eat(self): # 類方法
print("eating...")
self.__sleep() # 調(diào)用私有方法
def __sleep(self): #私有方法--類的外部不能使用
print("sleeping...")
pass
b = Person()
b.eat()
運(yùn)行結(jié)果:
eating...
sleeping...
6、屬性



示例屬性、類屬性代碼:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
#屬性
class a():
def __init__(self,name,age):
self.name = name #實(shí)例屬性
self.age = age
#類內(nèi)部使用實(shí)例屬性 self.實(shí)例屬性名
def get(self):
print(self.name)
a1 = a("Jack",18)
#類內(nèi)部使用實(shí)例屬性 self.實(shí)例屬性名
a1.get()
#類外部使用實(shí)例屬性 對(duì)象名.實(shí)例屬性名
print(a1.name)
#類屬性 在__init__()方法外聲明
#類內(nèi)部使用 類名.屬性名 調(diào)用
#類外部使用通過 類名.屬性名 或者 對(duì)象名.屬性名 方式調(diào)用
class b():
name = "Janne" #類屬性
#類內(nèi)部使用類屬性——類名.屬性名
def get(self):
print(b.name)
#類外部使用類屬性 通過 類名.屬性名
print(b.name)
#類外部使用類屬性 通過 對(duì)象名.屬性名
b1 = b()
print(b1.name)
#類內(nèi)部使用類屬性——類名.屬性名
b1.get()
運(yùn)行結(jié)果:
Jack
Jack
Janne
Janne
Janne
(1)類屬性/類變量:在類的外部可以調(diào)用
(2)私有變量/私有屬性:只能在類的內(nèi)部,通過self使用
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
#屬性/變量
class Person:
i = 10 #類屬性/類變量
__j = 20 #私有屬性/私有變量
def eat(self): # 類方法
print("eating...")
print(self.__j) # 調(diào)用私有變量
pass
b = Person()
print(b.i) #通過引用調(diào)用(建議)
print(Person.i) #可通過類名調(diào)用
b.eat()
運(yùn)行結(jié)果:
10
10
eating...
20

class GirlFriend():
#聲明對(duì)象屬性 通過構(gòu)造方法
def __init__(self,name,age,phone,pwd):
#給對(duì)象的屬性(變量名)前面加上 __ 成為了私有的屬性
self.__name = name
self.__age = age
self.__phone = phone
self.__pwd = pwd
#通過預(yù)留的接口 對(duì)私有屬性名進(jìn)行訪問或修改
def getInfo(self,pwd):
if pwd == "1234":
print("My girlfriend is %s,and she's %d years old,Her telephone number is %d"%(self.__name,self.__age,self.__phone))
else:
print("you failed...")
def setName(self,name):
self.__name = name #類內(nèi)修改私有屬性
gf = GirlFriend("Janne",18,13511112222,"1234")
gf.setName("Malianna")
gf.getInfo("1234")
運(yùn)行結(jié)果:
My girlfriend is Malianna,and she's 18 years old,Her telephone number is 13511112222

(3)特殊的類屬性

7、繼承
Python中支持多繼承,作用:復(fù)用,不建議使用多繼承(類對(duì)象爆炸)、

繼承示例代碼:
#繼承
#父類
class Animal():
def __init__(self,name,food,drinks):
self.name = name
self.food = food
self.drinks = drinks
def eat(self):
print("%s 愛吃 %s" %(self.name,self.food))
def drink(self):
print("%s 愛喝 %s" %(self.name,self.drinks))
#子類
class Dog(Animal):
def sound(self):
print("wonf wonf...")
class Cat(Animal):
def sound(self):
print("miao miao...")
dogs = Dog("哮天犬","骨頭","雪碧")
dogs.eat()
dogs.drink()
dogs.sound()
print("========================")
cats = Cat("波斯貓","魚","可樂")
cats.eat()
cats.drink()
cats.sound()
運(yùn)行結(jié)果:
哮天犬 愛吃 骨頭
哮天犬 愛喝 雪碧
wonf wonf...
========================
波斯貓 愛吃 魚
波斯貓 愛喝 可樂
miao miao...
示例一:
多繼承

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
#多繼承
class Run3000:
def run(self):
print("run 3000")
class Jump3:
def jump(self):
print("jump 3")
class Sport(Run3000,Jump3): #繼承
pass
sport = Sport()
sport.run()
sport.jump()
運(yùn)行結(jié)果:
run 3000
jump 3
示例二:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
class Father:
def __init__(self):
print("father 構(gòu)造")
def teach(self):
print("father teaching")
class Child(Father):
pass
zs = Child() #子類繼承與父類,創(chuàng)建子類前先創(chuàng)建父類
zs.teach()
運(yùn)行結(jié)果:
father 構(gòu)造
father teaching
子類中重寫父類的方法:重寫體現(xiàn)多態(tài)

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
class Father:
def __init__(self):
print("father 構(gòu)造")
def teach(self):
print("father teaching")
class Child(Father):
def teach(self): #方法重寫
print("child teaching")
zs = Child() #子類繼承與父類,創(chuàng)建子類前先創(chuàng)建父類
zs.teach()
運(yùn)行結(jié)果:
father 構(gòu)造
child teaching
新式類:
如果父類的構(gòu)造方法帶參數(shù),則需要子類通過super操作去完成調(diào)用。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
#新式類
class Father(object):
def __init__(self,i):
print("father 構(gòu)造"+ i)
def teach(self):
print("father teaching")
class Child(Father):
def __init__(self):
super(Child,self).__init__("hello")
def teach(self): #方法重寫
print("child teaching")
zs = Child() #子類繼承與父類,創(chuàng)建子類前先創(chuàng)建父類
zs.teach()
#運(yùn)行結(jié)果:
father 構(gòu)造hello
child teaching
運(yùn)行結(jié)果:
father 構(gòu)造hello
child teaching
多繼承又不完全,父類都有構(gòu)造方法時(shí),當(dāng)子類多繼承時(shí),只有一個(gè)父類的構(gòu)造方法被調(diào)用。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
class Run3000:
def __init__(self):
print("run 3000 構(gòu)造方法")
def run(self):
print("run 3000")
class Jump3:
def __init__(self):
print("jump 3 構(gòu)造方法")
def jump(self):
print("jump 3")
class Sport(Run3000,Jump3): #繼承
pass
sport = Sport()
sport.run()
sport.jump()
運(yùn)行結(jié)果:
run 3000 構(gòu)造方法
run 3000
jump 3
8、面向?qū)ο缶幊?/strong>
(1)定義

(2)示例代碼——人開車


更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python面向?qū)ο笾惖睦^承詳解
- Python面向?qū)ο蠓庋b繼承和多態(tài)示例講解
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)之繼承、多態(tài)原理與用法詳解
- Python面向?qū)ο笾^承原理與用法案例分析
- Python 面向?qū)ο笾庋b、繼承、多態(tài)操作實(shí)例分析
- Python面向?qū)ο笾^承和多態(tài)用法分析
- Python3.5面向?qū)ο蟪绦蛟O(shè)計(jì)之類的繼承和多態(tài)詳解
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)類的封裝與繼承用法示例
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)多繼承和多態(tài)用法示例
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)之類的定義與繼承簡(jiǎn)單示例
- Python面向?qū)ο笾惖亩x與繼承用法示例
- Python面向?qū)ο箢惥帉懠?xì)節(jié)分析【類,方法,繼承,超類,接口等】
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)OOP入門教程【類,實(shí)例,繼承,重載等】
- Python面向?qū)ο笾^承和組合用法實(shí)例分析
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)之繼承與多繼承用法分析
- Python面向?qū)ο箢惖睦^承實(shí)例詳解
- Python面向?qū)ο箢惱^承和組合實(shí)例分析
- Python 面向?qū)ο缶幊痰娜筇匦灾^承
相關(guān)文章
Python對(duì)字符串實(shí)現(xiàn)去重操作的方法示例
字符串去重是python中字符串操作常見的一個(gè)需求,最近在工作中就又遇到了,所以下面這篇文章主要給大家介紹了關(guān)于Python對(duì)字符串實(shí)現(xiàn)去重操作的相關(guān)資料,文中給出了詳細(xì)的介紹,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-08-08
Python+Pika+RabbitMQ環(huán)境部署及實(shí)現(xiàn)工作隊(duì)列的實(shí)例教程
RabbitMQ是一個(gè)消息隊(duì)列服務(wù)器,在本文中我們將學(xué)習(xí)到Python+Pika+RabbitMQ環(huán)境部署及實(shí)現(xiàn)工作隊(duì)列的實(shí)例教程,需要的朋友可以參考下2016-06-06
python 爬蟲出現(xiàn)403禁止訪問錯(cuò)誤詳解
這篇文章主要介紹了 python 爬蟲解決403禁止訪問錯(cuò)誤的相關(guān)資料,需要的朋友可以參考下2017-03-03
Python BeautifulSoup [解決方法] TypeError: list indices must be
這篇文章主要介紹了Python BeautifulSoup [解決方法] TypeError: list indices must be integers or slices, not str,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Python中的內(nèi)置函數(shù)isdigit()
這篇文章主要介紹了Python中的內(nèi)置函數(shù)isdigit(),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Python中連通域分割Two-Pass算法的原理與實(shí)現(xiàn)詳解
兩遍掃描法(?Two-Pass?),正如其名,指的就是通過掃描兩遍圖像,將圖像中存在的所有連通域找出并標(biāo)記,本文將詳細(xì)介紹Two-Pass算法的原理與實(shí)現(xiàn),需要的可以參考下2023-12-12
GPU狀態(tài)監(jiān)測(cè)?nvidia-smi?命令的用法詳解
這篇文章主要介紹了GPU狀態(tài)監(jiān)測(cè)?nvidia-smi?命令的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

