您的位置:首页 > 编程语言 > Python开发

Python学习_我该怎么操作类的继承

2017-11-18 16:32 323 查看
直接使用一个例子说明类继承

class User():
def __init__(self,first_name,last_name,*info):
self.first_name=first_name
self.last_name=last_name
self.info=info
def describe_user(self):
print('The user\'s name is '+self.first_name.title()+' '+self.last_name.title())
def greet_user(self):
for inf in self.info:
print(self.first_name.title()+' '+self.last_name.title() +' '+inf)

class super_man(User):
def __init__(self,first_name,last_name,*info):
super().__init__(first_name,last_name,*info)

wu=super_man('wu','li','nihao')
wu.describe_user()
wu.greet_user()


输出:

The user’s name is Wu Li

Wu Li nihao

注意:

1、在下边直接定义了一个super_man()类继承了User()类,就可以直接使用User类中的方法了

2、在子类中可以定义不同于父类的方法

3、可以对父类中的方法重写,当子类中的方法和父类中方法名称相同时可以实现对父类方法的重写操作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: