from math import pi class Circle: def __init__(self,r): self.r = r def area(self): return pi*self.r**2 def perimeter(self): return 2*pi*self.r c1 = Circle(5) print(c1.area()) #78.53981633974483 print(c1.perimeter()) #31.41592653589793
from math import pi class Ring: def __init__(self,r1,r2): if r1>=r2: self.outer = r1 self.inner = r2 else: self.inner = r1 self.outer = r2 def area(self): return pi * self.outer ** 2 - pi * self.inner ** 2 def perimeter(self): return 2 * pi * self.outer + 2 * pi * self.inner r1 = Ring(5,2) print(r1.area()) #65.97344572538566 print(r1.perimeter()) #43.982297150257104
from math import pi class Cylinder: def __init__(self,r,h): self.r = r self.h = h def area(self): c_area = pi * self.r**2 rec_area = 2 * pi *self.r * self.h return c_area*2 + rec_area def volume(self): c_area = pi * self.r ** 2 return c_area*self.h c1 = Cylinder(5,8) print(c1.area()) #408.4070449666731 print(c1.volume()) #628.3185307179587
什么是耦合?
模块间的依赖性就是耦合,两个功能函数之间的依赖程度
紧耦合:紧耦合就是模块或者系统之间关系太紧密,存在相互调用。紧耦合系统的缺点在于更新一个模块的结果导致其它模块的结果变化,难以重用特定的关联模块。
松耦合通讯机制提供了紧耦合机制所没有的许多优点,并且它们有助于降低客户端和远程服务之间的依赖性。
类的组合
from math import pi class Circle: def __init__(self,r): self.r = r def area(self): return pi*self.r**2 def perimeter(self): return 2*pi*self.r c= Circle(5) print(c.area()) #78.53981633974483 print(c.perimeter()) #31.41592653589793 # 圆环类 class Ring: def __init__(self,r1,r2): c1 = Circle(r1) c2 = Circle(r2) if r1>=r2: self.outer = c1 # 组合 # self.outer.area() # c1.area() self.inner = c2 # 组合 else: self.inner = c1 # 组合 self.outer = c2 # 组合 def area(self): return self.outer.area() -self.inner.area() def perimeter(self): return self.outer.perimeter() + self.inner.perimeter() r = Ring(5,2) print(r.area()) #65.97344572538566 print(r.perimeter()) #43.982297150257104 # 圆柱类 class Cylinder: def __init__(self,r,h): self.c = Circle(r) # 组合 self.h = h def area(self): c_area = self.c.area() rec_area = self.c.perimeter() * self.h return c_area*2 + rec_area def volume(self): c_area = self.c.area() return c_area*self.h C = Cylinder(5,8) print(C.area()) #408.4070449666731 print(C.volume()) #628.3185307179587
组合
一个类的对象是另一个类对象的属性
圆形类的对象 是圆环类对象的outer属性的值
计算圆形相关数据的公式只和Circle类在一起
其余的用到公式的地方都是通过circle类来使用的
公式与其他类之间的关系是一个“松耦合”关系
class Date: def __init__(self,year,month,day): self.year = year self.month = month self.day = day def date(self): return '%s-%s-%s'%(self.year,self.month,self.day) # class Student: def __init__(self,name,num,birth,in_shcool,start_day): self.name = name self.num = num self.birth = birth # 组合 self.in_school = in_shcool # 组合 self.start_day = start_day # 组合 d1 = Date(1999,10,27) d2 = Date(2019,1,9) d3 = Date(2019,2,9) alex = Student('alex',10086,d1,d2,d3) print(alex.birth.year) #1999 print(alex.birth.month) #10 print(alex.birth.day) #27 print(alex.birth.date()) #1999-10-27 alex.start_day.month +=1 print(alex.start_day.date()) #2019-3-9
class Student: def __init__(self,name,num,course): self.name = name self.num = num self.course = course class Course: def __init__(self,name,price,period): self.name = name self.price = price self.period = period python = Course('python',25000,'6 months') s1 = Student('alex',10085,python) s2 = Student('taibai',10084,python) print(s1.__dict__) #{'name': 'alex', 'num': 10085, 'course': <__main__.Course object at 0x000001EB9FA5D9E8>} print(s1.course.price) #25000 python.price = 30000 python.period = '7 months' print(s2.course.period) #7 months