5.类的继承
class Employee: raise_amount =1.04 def __init__(self, first, last, pay): self.first = first self.last = last self.pay= pay self.email = first + '.' + last + '@company.com' class Developer(Employee): raise_amount=1.05
def __init__(self,first,last,pay,language):
super().__init__(first,last,pay)# 沿用父类的初始化方法
self.language=language #新增的初始化方法 print(help(Developer)) #打印本类的属性,包括 继承顺序、类中的属性和方法等
#如果没有自身的init方法,则实例化时会调用“距离”最近的父类的构造方法
dev_1=Developer('Corey','Schafer',50000,'Java')
dev_1=Developer('Oraly','Delphy',50000,'Python')
class manager(Employee):
def __init__(self,first,last,pay,employees=None):
super().__init__(first,last,pay)
if employees is None:
self.employees=[]
else:
self.employees=employees
def add_emp(self,emp):
if emp not in self.employees:
self.employees.append(emp)
def remove_emp(self,emp):
if emp in self.employees:
self.employees.remove(emp)
def print_emps(self):
for emp in self.employees:
print('-->',emp.GetFullName())
mgr_1=Manager('Whill','Smith',80000,[dev_1])
print(mgr_1.email)
mgr_1.add_emp(dev_2)
print(mgr_1.print_emps())
mgr_1.remove(dev_1)
print(mgr_1.print_emps())
print(isinstance(mgr_1,Manager)) # True 判断对象是否为特定类的实例
print(isinstance(mgr_1,Employee)) # True
print(isinstance(mgr_1,Developer)) # False
print(issubclass(Manager,Employee)) # True 判断第一个参数类是否为第二个类的子类
标签:__,Python,self,笔记,mgr,emp,面向对象编程,print,employees From: https://www.cnblogs.com/Nikole/p/17590045.html