14、python class继承

一、创建父类

class Emp:
	def __init__(self, No, name, empType, birthday, gender):
		self.No = No
		self.name = name
		self.emp_type = empType
		self.birthday = birthday
		self.gender = gender

	def get_work(self, jobId):
		print(f'{self.name} do jobId')

	def pay_salary(self, salary):
		print(f'pay {self.No} {salary}')

	def check_in(self):
		print(f'{self.name} 现在考勤打卡')

二、继承父类

#注意继承父类的语法  Manager(Emp)
class Manager(Emp):
	def __init__(self, No, name, empType, birthday, gender, title):
		# 调用父类的__init__方法
		super().__init__(No, name, empType, birthday, gender)
		self.title = title

重写父类的方法

class Manager(Emp):
	def __init__(self, No, name, empType, birthday, gender, title):
		super().__init__(No, name, empType, birthday, gender)
		self.title = title
	#重写check_in方法
	def check_in(self):
		print(f'{self.name} 不需要考勤打卡')

三、在一个类中初始化另一个类

1、定义一个Position类

class Position:
	def __init__(self, positionName, positionLevel):
		self.positionName = positionName
		self.positionLevel = positionLevel

2、在Manager类中创建Position类对象

class Manager(Emp):
	def __init__(self, No, name, empType, birthday, gender, title):
		super().__init__(No, name, empType, birthday, gender)
		self.title = title
		#创建
		self.position = Position('部门经理','7')

	def check_in(self):
		print(f'{self.name} 不需要考勤打卡')


liSi = Manager('001','LiSi','正式','1987-10-02','男','中级')
print(liSi.position.positionName)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值