Python的內建方法和類的繼承舉例
阿新 • • 發佈:2018-12-06
1.類的內建方法
Python內部類:
所謂內部類,就是在類的內部定義的類,主要目的是為了更好的抽象現實世界。
例子:
汽車是一個類,汽車的底盤輪胎也可以抽象為類,將其定義到汽車內中,而形成內部類,
更好的描述汽車類,因為底盤輪胎是汽車的一部分。
內部類例項化方法:
方法1:直接使用外部類呼叫內部類
方法2:先對外部類進行例項化,然後再例項化內部類
out_name = outclass_name()
in_name = out_name.inclass_name()
in_name.method()
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有屬性 class Chinese(object): print("I am chinese") def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" @classmethod #呼叫類的方法 def test(self): print ("this is class method") @staticmethod #呼叫類的方法 def test1(): print ("this is static method") jack = People.Chinese()
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有屬性 class Chinese(object): name ="I am a Chinese." def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" @classmethod #呼叫類的方法 def test(self): print ("this is class method") @staticmethod #呼叫類的方法 def test1(): print ("this is static method") jack = People.Chinese() #外部類呼叫內部類 print jack.name #外部類呼叫內部類物件
另一種方法,外部類呼叫內部類物件
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有屬性 class Chinese(object): name ="I am a Chinese." def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" @classmethod #呼叫類的方法 def test(self): print ("this is class method") @staticmethod #呼叫類的方法 def test1(): print ("this is static method") ren = People() #例項化外部類 jack = ren.Chinese() #例項化內部類 print jack.name #列印內部類屬性 或 print People.Chinese.name print People.Chinese().name
魔術方法:
str(self)
建構函式與解構函式
建構函式:
用於初始化類的內部狀態,Python提供的建構函式是__init__():
__init__():方法是可選的,如果不提供,python會給出一個預設的__init__方法。
解構函式:
用於釋放物件佔用的資源,python提供的解構函式是__del__():
__del__():也是可選的,如果不提供,則python會在後臺提供預設解構函式。
建構函式str
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
__age = 30 #私有屬性
class Chinese(object):
name ="I am a Chinese."
def __str__(self):
return "This is People class"
def think(self):
self.color = "black"
print "I am a %s " % self.color
print ("I am a thinker")
print self.__age
def __talk(self):
print "I am talking with Tom"
@classmethod #呼叫類的方法
def test(self):
print ("this is class method")
@staticmethod #呼叫類的方法
def test1():
print ("this is static method")
ren = People() #例項化外部類
print ren #預設執行__str__
init(self)初始化類:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
__age = 30 #私有屬性
class Chinese(object):
name ="I am a Chinese."
def __str__(self):
return "This is People class"
def __init__(self,c='white'): #類例項化時自動執行
self.color = c
self.think()
def think(self):
self.color = "black"
print "I am a %s " % self.color
print ("I am a thinker")
print self.__age
def __talk(self):
print "I am talking with Tom"
@classmethod #呼叫類的方法
def test(self):
print ("this is class method")
@staticmethod #呼叫類的方法
def test1():
print ("this is static method")
jack = People('green')
ren = People() #例項化外部類
print ren.color #通過物件訪問屬性是初始化後的值
print People.color #通過類訪問還是原來的值
[[email protected] 20180110]# python test1.py
I am a black
I am a thinker
30
black
yellow
解構函式del():釋放資源
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
__age = 30 #私有屬性
class Chinese(object):
name ="I am a Chinese."
def __str__(self):
return "This is People class"
def __init__(self,c='white'): #類例項化時自動執行
print ("initing...")
self.color = c
self.think()
f = open('test.py')
def think(self):
self.color = "black"
print "I am a %s " % self.color
print ("I am a thinker")
print self.__age
def __talk(self):
print "I am talking with Tom"
@classmethod #呼叫類的方法
def test(self):
print ("this is class method")
@staticmethod #呼叫類的方法
def test1():
print ("this is static method")
def __del__(self):
print ("del....")
self.f.close()
jack = People('green')
ren = People() #例項化外部類
print ren.color #通過物件訪問屬性是初始化後的值
print People.color #通過類訪問還是原來的值
垃圾回收機制:
Python採用垃圾回收機制來清理不再使用的物件;python提供gc模組釋放不再使用的物件。
Python採用“引用計數”的演算法方式來處理回收,即:當然某個物件在其作用域內不再被其
他物件引用的時候,python就自動化清除物件。
gc模組collect()可以一次性收集所有待處理的物件(gc.collect)
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
__age = 30 #私有屬性
class Chinese(object):
name ="I am a Chinese."
def __str__(self):
return "This is People class"
def __init__(self,c='white'): #類例項化時自動執行
print ("initing...")
self.color = c
self.think()
f = open('test.py')
def think(self):
self.color = "black"
print "I am a %s " % self.color
print ("I am a thinker")
print self.__age
def __talk(self):
print "I am talking with Tom"
@classmethod #呼叫類的方法
def test(self):
print ("this is class method")
@staticmethod #呼叫類的方法
def test1():
print ("this is static method")
def __del__(self):
print ("del....")
self.f.close()
print gc.collect() 如果是0是沒有回收的。
jack = People('green')
ren = People() #例項化外部類
print ren.color #通過物件訪問屬性是初始化後的值
print People.color #通過類訪問還是原來的值
2.類的繼承
類的繼承
繼承是面向物件的重要特性之一,
繼承關係繼承是相對兩個類而言的父子關係
子類繼承了父類的所有公有屬性和方法,
繼承,實現了程式碼重用
使用繼承
繼承可以重用已經存在的資料和行為,減少程式碼的重複編寫,
Python在類名後使用一對括號來表示繼承關係,括號中的即類為父類
class Myclass(ParentClass),
如果父類定義了__init__方法,子類必須顯式呼叫父類的__init__方法,
ParentClass.__init__(self,[args...])
如果子類需要擴充套件父類的行為,可以新增__init__方法的引數.
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
def think(self):
self.color = "black"
print "I am a %s " % self.color
print ("I am a thinker")
class Chinese(People):
pass
cn = Chinese()
print cn.color
cn.think()
父類中有建構函式:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
def __init__(self):
print "Init..."
self.dwell = 'Earth'
def think(self):
print "I am a %s " % self.color
print ("I am a thinker")
class Chinese(People):
pass
cn = Chinese()
print cn.dwell
cn.think()
引數大於兩個:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
def __init__(self,c):
print "Init..."
self.dwell = 'Earth'
def think(self):
print "I am a %s " % self.color
print ("I am a thinker")
class Chinese(People):
def __init__(self):
People.__init__(self,'red')
pass
cn = Chinese()
Super 函式
class A(object):
def __init__(self):
print "enter A"
print "leave A"
class B(object):
def __init__(self):
print "enter B"
super(B,self),__init__()
print "leave B"
b = B()
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
def __init__(self,c):
print "Init..."
self.dwell = 'Earth'
def think(self):
print "I am a %s " % self.color
print ("I am a thinker")
class Chinese(People):
def __init__(self):
super(Chinese,self).__init__('red')
pass
cn = Chinese()
cn.think()
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
def __init__(self,c):
print "Init..."
self.dwell = 'Earth'
def think(self):
print "I am a %s " % self.color
print ("I am a thinker")
class Chinese(People):
def __init__(self):
super(Chinese,self).__init__('red')
def talk(self):
print "I like taking."
cn = Chinese()
cn.think()
cn.talk()
多重繼承
Python支援多重繼承,第一個類可以繼承多個父類
語法:
class class_name(Parent_c1,Parent_c2,...)
注意:
當父類中出現多個自定義的__init__的方法時,
多重繼承,只執行第一個累的__init_方法,其他不執行。
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
def __init__(self):
print "Init..."
self.dwell = 'Earth'
def think(self):
print "I am a %s " % self.color
print ("My home is %s ") % self.dwell
class Martian(object):
color = 'red'
def __init__(self):
self.dwell = 'Martian'
class Chinese(People,Martian):
def __init__(self):
People.__init__(self)
cn = Chinese()
cn.think()
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
def __init__(self):
self.dwell = 'Earth'
self.color = 'yellow'
def think(self):
print "I am a %s " % self.color
print ("My home is %s ") % self.dwell
class Martian(object):
color = 'red'
def __init__(self):
self.dwell = 'Martian'
def talk(self):
print "I like talking"
class Chinese(Martian,People):
def __init__(self):
People.__init__(self)
cn = Chinese()
cn.think()
cn.talk()