Python 面向对象编程 OOP--03--类的组合和派生

目      录

1. 类的组合

2. 类的派生

2.1 派生示范

 2.2 从标准类派生

2.2.1 不可变类型的派生

2.2.2 可变类型的派生

2.2.3 用特殊方法定制类


其他文章:

Python 面向对象编程 OOP--01--类和实例的基本概念

Python 面向对象编程 OOP--02--类中的单下划线和双下划线 5 种情况

Python 面向对象编程 OOP--03--类的组合和派生

Python 面向对象编程 OOP--04--多重继承和方法解释顺序 MRO

Python 面向对象编程 OOP 05--类、实例和其他对象的内建函数

Python 面向对象编程 OOP--06--super() 是一个描述器类

        在新代码中利用既有的类,有两种方法。

  1.         第一种是组合,就是让不同的类混合并且假如其他类中,来增加功能和代码重用性。这种适用于由多个小类组成一个大类的情况,并且不需要对小类进行太多修改。
  2.         第二种是通过派生,在需要设计相同的类,但是功能不太一样的时候,使用派生会更合理些。

1. 类的组合

        从下面的代码可以看出,Home 这个类由 Man 和 House 组合,Home 的父类依然是 object,这个实现起来非常简单,Home 并不是 Man 和 House 的子类。

class Man(object):
    '''
    This class is used to describle human!
    '''
    def __init__(self,name,birthYear):
        self.name=name
        self.birthYear=birthYear
    
class House(object):
    '''
    This class is used to describle house!
    '''
    def __init__(self,location,size,value):
        self.loc=location
        self.size=size
        self.value=value
        
class Home(object):
    '''
    This class is used to describle home,
    including different man and one house at least!
    '''
    def __init__(self,name,birthYear,location,size,value):
        self.homeowner=Man(name,birthYear)
        self.house=House(location,size,value)

allen_home=Home("allen",1982,"BeiJing",100,500)
print(allen_home.homeowner.name)
print(allen_home.homeowner.birthYear)
print(allen_home.house.loc)
print(allen_home.house.size)
print(allen_home.house.value)

2. 类的派生

2.1 派生示范

      派生是在创建子类的过程中出现的,并且和继承是一起的,以下面的代码为例。

      先创建了父类 Man,然后创建了两个子类 Teacher、Singer。   

  •       Teacher 未修改 __init__ 方法,即沿用父类 Man 的__init__,这叫继承
  •       Singer 显示的修改 __init__ 方法,不但沿用父类 Man 的__init__,还多了适用于 Singer的修改。请注意,如果在显式修改 __init__ 方法时候,还要保留父类的__init__,一定要显示的写出来!下面这两种写法都可以。

                a) super(Singer,self).__init__(name,age)

                b) super().__init__(name,age)

  •       Teacher/Singer 显示修改了  work 方法,即覆盖了父类 Man 的 work 方法,这叫派生
  •       Teacher/Singer 的父类都是 Man。
  •       Teacher/Singer 调用 work 方法的结果是不一样的,这叫多态
class Man(object):
    '''
    This class is used to describle human!
    '''
    def __init__(self,name,age):
        self.name=name
        self.age=age
    
    def work(self):
        print("Now a man is working")

class Teacher(Man):
    '''
    This class is used to describle worker!
    '''
    #此处未修改 __init__ ,即沿用父类 Man 的
    #这叫继承
    
    
    #重新定义 work 方法,这是派生
    def work(self):
        print("Now a teacher is teaching!")

class Singer(Man):
    '''
    This class is used to describle singer!
    '''
    def __init__(self,name,age,country):
        # 调用父类的初始化方法 __init__
        super().__init__(name,age)
        self.country=country

    #重新定义 work 方法,这是派生
    def work(self,song):
        print("Now a singer is singing "+song+" !")

t=Teacher("teach",33)
s=Singer("sin",28,"China")
t.work()
s.work("甜蜜蜜")

 

 2.2 从标准类派生

2.2.1 不可变类型的派生

        下面举了float 和 str 的两种类型改写,看看就好。

class UpdatedFloat(float):
    def __new__(cls,val):
        return(float.__new__(cls,round(val,2)))
class UpdatedStr(str):
    def __new__(cls,string):
        return(str.__new__(cls,string[0:5]))
a=UpdatedFloat(1.123456)
b=UpdatedStr("abcdefghi")
print(a)
print(b)

2.2.2 可变类型的派生

        以列表为例,这个 Updated List 和普通的列表没什么区别,唯一的区别就是每次往里面添加元素时候,会有提示。

class UpdatedList(list):
    def append(self,ele):
        super().append(ele)
        print("One element is appended!")
a=UpdatedList()
a.append("a")
a.append("b")
a

2.2.3 用特殊方法定制类

      在 Python 面向对象编程 OOP--类和实例的基本概念 中说了类的一些特殊方法,如果对这些特殊方法进行定制,那么就会得到有特殊功能的类。

        我觉得这篇文章写得挺好的,推荐大家看。

        ​​​​​​python中的用来定制类的特殊方法的含义

'''

要是大家觉得写得还行,麻烦点个赞或者收藏吧,想个博客涨涨人气,非常感谢!

'''

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江南野栀子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值