资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

python中继承父类方法-创新互联

创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

专注于为中小企业提供网站设计制作、做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业巫溪免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

这篇文章主要介绍python中继承父类方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

继承父类方法

子类可以直接调用父类的方法

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

继承父类属性

这里要注意, 如果要继承父类的属性, 一定要在子类的构造函数里调用父类的构造函数, 否则会报错无法访问, 因为父类的构造函数没有被调用, 构造函数中的属性自然也就没有被声明

这时如果调用父类的属性则会报错, 报错内容为Student实例没有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'

下面是子类在构造函数中调用父类构造函数的情况, 子类实例可以访问父类属性

# 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
# 小明

方法重写

有时候父类提供的方法不能满足需求时, 可以在子类中重写父类的方法

在父类Person中, 构造函数只定义了name和age两个属性, print_into()函数也只打印了name, age这两个属性

在子类Student中, 多了一个school属性, 显然父类的提供方法功能不够了, 这时候, 子类就需要对父类的方法进行重写, 扩充父类的功能

# 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要为新类
        # 新类就是由内置类型派生出来的类
		print 'school: ', self.school				
s = Student(u'小明', 18, u'家里蹲大学')
s.print_info()
	
# name:  小明
# age:  18
# school:  家里蹲大学

以上是python中继承父类方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联-成都网站建设公司行业资讯频道!


标题名称:python中继承父类方法-创新互联
文章路径:http://cdkjz.cn/article/eosdg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220