日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python中有重寫么

繼承父類方法

創(chuàng)新互聯(lián)專注于二連浩特網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供二連浩特營(yíng)銷型網(wǎng)站建設(shè),二連浩特網(wǎng)站制作、二連浩特網(wǎng)頁(yè)設(shè)計(jì)、二連浩特網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造二連浩特網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供二連浩特網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

子類可以直接調(diào)用父類的方法

class Person():
	def __init__(self):
		pass			
	def hello(self):
		print 'hello'		
class Student(Person):
	def __init__(self):
		pass				
s = Student()
s.hello()				# hello

繼承父類屬性

這里要注意, 如果要繼承父類的屬性, 一定要在子類的構(gòu)造函數(shù)里調(diào)用父類的構(gòu)造函數(shù), 否則會(huì)報(bào)錯(cuò)無(wú)法訪問(wèn), 因?yàn)楦割惖臉?gòu)造函數(shù)沒(méi)有被調(diào)用, 構(gòu)造函數(shù)中的屬性自然也就沒(méi)有被聲明

這時(shí)如果調(diào)用父類的屬性則會(huì)報(bào)錯(cuò), 報(bào)錯(cuò)內(nèi)容為Student實(shí)例沒(méi)有name屬性

# coding=utf-8

class Person():
	def __init__(self):
		self.name = '小明'
		self.age = 18	
		print('Person class init completed')		
	def hello(self):
		print 'hello'
class Student(Person):
	def __init__(self):
		print ('Student class init completed')		
s = Student()
print s.name

# Student class init completed
# Traceback (most recent call last):
#   File ".\classDemo.py", line 23, in 
#     print s.name
# AttributeError: Student instance has no attribute 'name'

下面是子類在構(gòu)造函數(shù)中調(diào)用父類構(gòu)造函數(shù)的情況, 子類實(shí)例可以訪問(wèn)父類屬性

# coding=utf-8

class Person():
	def __init__(self):
		self.name = u'小明'
		self.age = 18		
		print('Person class init completed')			
	def hello(self):
		print 'hello'		
class Student(Person):
	def __init__(self):
		Person.__init__(self)		
		print ('Student class init completed')				
s = Student()
print s.name

# Person class init completed
# Student class init completed
# 小明

方法重寫

有時(shí)候父類提供的方法不能滿足需求時(shí), 可以在子類中重寫父類的方法

在父類Person中, 構(gòu)造函數(shù)只定義了name和age兩個(gè)屬性, print_into()函數(shù)也只打印了name, age這兩個(gè)屬性

在子類Student中, 多了一個(gè)school屬性, 顯然父類的提供方法功能不夠了, 這時(shí)候, 子類就需要對(duì)父類的方法進(jìn)行重寫, 擴(kuò)充父類的功能

# coding=utf-8

class Person(object):
	def __init__(self, name, age):
		self.name = name
		self.age = age			
	def print_info(self):
		print 'name: ', self.name
		print 'age: ', self.age		
class Student(Person):
	def __init__(self, name, age, school):
		Person.__init__(self, name, age)		
		self.school = school		
	def print_info(self):
		super(Student, self).print_info()
        # python3 中可直接使用super()
        # Python2 一般為super(class, self), 且class要為新類
        # 新類就是由內(nèi)置類型派生出來(lái)的類
		print 'school: ', self.school				
s = Student(u'小明', 18, u'家里蹲大學(xué)')
s.print_info()
	
# name:  小明
# age:  18
# school:  家里蹲大學(xué)

更多技術(shù)請(qǐng)關(guān)注Python視頻教程。


網(wǎng)頁(yè)名稱:創(chuàng)新互聯(lián)Python教程:python中有重寫么
鏈接URL:http://www.5511xx.com/article/dpeggih.html