目 录
其他文章:
Python 面向对象编程 OOP--01--类和实例的基本概念
Python 面向对象编程 OOP--02--类中的单下划线和双下划线 5 种情况
Python 面向对象编程 OOP--03--类的组合和派生
Python 面向对象编程 OOP--04--多重继承和方法解释顺序 MRO
Python 面向对象编程 OOP 05--类、实例和其他对象的内建函数
Python 面向对象编程 OOP--06--super() 是一个描述器类
在新代码中利用既有的类,有两种方法。
- 第一种是组合,就是让不同的类混合并且假如其他类中,来增加功能和代码重用性。这种适用于由多个小类组成一个大类的情况,并且不需要对小类进行太多修改。
- 第二种是通过派生,在需要设计相同的类,但是功能不太一样的时候,使用派生会更合理些。
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--类和实例的基本概念 中说了类的一些特殊方法,如果对这些特殊方法进行定制,那么就会得到有特殊功能的类。
我觉得这篇文章写得挺好的,推荐大家看。
'''
要是大家觉得写得还行,麻烦点个赞或者收藏吧,想个博客涨涨人气,非常感谢!
'''