Python面向对象编程-学习笔记(二)

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 判断第一个参数类是否为第二个类的子类
 

 6.特殊(Magic/Dunder)方法

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'

     def fullname(self):
         return '{} {}'.format(self.first, self.last)

     def apply_raise(self):
         self.pay = int(self.pay * Employee.raise_amount)

   def __repr__(self):
     #对象的明确表示,用于调试和日志记录,显示需要被开发人员看到的内容
     return "Employee('{}','{}',{})".format(self.first,self.last,self.pay)

   def __str__(self):
     #对象的可读表示,用于想最终用户显示内容
     return '{}-{}'.format(self.fullname(),self.email)

emp_1=Employee('Corey','Schafer', 50000)
print(emp_1) #此时将显示emp_1.__repr__()方法的结果,虽然没有直接调用:
        #Employee('Corey','Schafer',50000)
        #也可以通过
        #print(repr(emp_1))
        #print(emp_1.__repr__())
        #直接访问调用

print(1+2) #完全等于 print(int.__add__(1,2))
print('a'+'b') #完全等于print(str.__add__('a','b')

 自定义类的加法

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'

     def fullname(self):
         return '{} {}'.format(self.first, self.last)

     def apply_raise(self):
         self.pay = int(self.pay * Employee.raise_amount)
   def __add__(self,other): #注意:other是关键字
     return self.pay+other.pay

   def __len__(self):
     return len(self.fullname())
emp_1= Employee('Corey','Schafer', 50000)
emp_2= Employee('Test','User', 60000)
print(emp_1,emp_2) #结果:110000
print(len(emp_1)) #结果:13,也就是'Corey Schafer'的长度

return NotImplemented 未实现

7.属性装饰器(Setters,Getters,Deleters)

class Employee:
      def __init__(self, first, last, pay):
          self.first = first
          self.last = last
          self.pay= pay
          self.email = first + '.' + last + '@company.com'

    def fullname(self):
          return '{} {}'.format(self.first, self.last)
emp_1=Employee('John','Smith')
emp_1.first='Jim'
print(emp_1.first) #Jim
print(emp_1.email) #John.Smith@company.com  由于email属性是在初始化方法中确定的,因此email属性并未更改导致数据错误
print(emp_1.fullname()) #Jim Smith

一种解决办法:增加email()方法
    def email(self):
       return '{}.{}@company.com'.format(self.first,self.last)
但该方法需要把
print(emp_1.email)
改为
print(emp_1.email())

另一种装饰器方法
    @property #允许程序像属性一样访问方法
    def email(self):
       return '{}.{}@company.com'.format(self.first,self.last)

 

部分方法被设置成property后,只能获取该方法属性,无法设置方法属性,此时要用到setter

@property
def fullname(self): 
      return '{} {}'.format(self.first, self.last)

@fullname.setter
def fullname(self,name):
      first,last=name.split(' ')
      self.first=first
      self.last=last

@fullname.deleter #删除器,属性被删除时会执行其包含的代码
def fullname(self):
  print('Delete Name')
self.first=None
  self.last=None

emp_1.fullname='Jim Smith' #此时,该赋值语句不再报错
del emp_1.fullname #如果没有删除器,此代码无法运行
posted @ 2023-07-29 16:36  尼古拉-卡什  阅读(9)  评论(0编辑  收藏  举报