Day8 Python学习笔记&关键注意点
Part 1:Python学习笔记
===================
8.3. 特征
8.3.1.封装:将对象的所具备的行为写在函数里面。简单的说,一个类就是一个封装了数据以及操作这些数据的代码的逻辑实体。
-
示例1
1 class Book: 2 count = 0 3 4 # Iniate the function 5 def __init__(self,title,price=0.0,author=None): 6 self.title = title 7 self.price = price 8 self.author = author 9 Book.count += 1 10 11 # Delete the object function 12 def __del__(self): 13 Book.count -= 1 14 15 def __repr__(self): 16 return '<Book:{} at 0x{}>'.format(self.title,id(self)) 17 18 def __str__(self): 19 return '[Book:{},Price:{}]'.format(self.title,self.price) 20 21 def print_info(self): 22 print(self.title,self.price,self.author) 23 24 def cls_method(cls): 25 print('Class Function') 26 27 # @static method 28 def static_method(): 29 print('Static Function, have no relationship with instance in logic') 30 31 32 if __name__ == '__main__': 33 book = Book('Python Classic',price=29.0,author='Tom') 34 book2 = Book('Flask') 35 book3 = Book('ASP.net') 36 37 # del(book3) 38 39 # print('Book Number:{}'.format(Book.count)) 40 # print('Book Number:{}'.format(book2.count)) 41 # 42 # book2.count = 99 43 # print('Book Number:{}'.format(Book.count)) 44 # print('Book Number:{}'.format(book2.count)) 45 46 Book.cls_method(book2) 47 Book.static_method() 48 # book2.static_method() #This will work if enable @staticmethod in line 27
- 示例2: @property-定义:将一个方法伪装成一个属性,这里有提供了两种对私有的修改和删除方法,修改:方法名.setter 删除:方法名.deleter,具体的实例如下:
1 import datetime 2 3 class Student: 4 5 def __init__(self,name,birthdate): 6 self.name = name 7 # self.age = age 8 self.birthdate = birthdate 9 10 @property 11 def age(self): 12 return datetime.date.today().year - self.birthdate.year 13 14 @age.setter 15 def age(self,value): 16 raise AttributeError('Stop setting age!!!') 17 18 @age.deleter 19 def age(self): 20 raise AttributeError('Cannot delete age!!!') 21 22 23 # def get_age(self): 24 # return datetime.date.today().year - self.birthdate.year 25 26 27 if __name__ == '__main__': 28 s = Student('Tom',datetime.date(1992,3,1)) 29 # print(s.age) 30 print(s.birthdate) 31 print(s.age) 32 33 s.birthdate = datetime.date(1982,8,2) 34 # s.age = 20 #Cannot setting age that was defined in line 14-16 35 # print(s.birthdate) 36 # print(s.age) 37 38 del(s.name) 39 print(s.name) 40 41 # del(s.age) 42 # print(s.age) #Cannot delete age that was defined in line 18-20
8.3.2.继承:指提供了同一类对象共性的处理方法,子类继承父类共性的东西。这样有利于代码的复用性,即子类拥有父类的方法。如果多个类之间有重叠部分,在概念上有重叠和继承的关系,一定程度上利用代码重用。
8.3.3.多态:属于同一类型的不同实例,对同一个消息做出不同的响应。且以封装和继承为前提。
- 示例1
1 import math 2 3 class Circle: 4 def __init__(self,radius): 5 self.radius = radius 6 7 @property 8 def area(self): 9 return math.pi * self.radius **2 10 11 # 12 # def get_area(self): 13 # return math.pi * self.radius **2 14 15 16 c = Circle(4.0) 17 18 print('Circle area is:{}'.format(c.area))
- 示例2
1 # Sample 2: Ji Cheng 2 import datetime 3 4 class Department: 5 def __init__(self,department,phone,manager): 6 self.department = department 7 self.phone = phone 8 self.manager = manager 9 10 def __repr__(self): 11 return '<Department:{}>'.format(self.department) 12 13 14 class Employee: 15 def __init__(self,department:Department,name,birthdate,salary): 16 self.department = department 17 self.name = name 18 self.birthdate = birthdate 19 self.salary = salary 20 21 @property 22 def age(self): 23 return datetime.date.today().year - self.birthdate.year 24 25 def give_raise(self,percent,bonus=.0): 26 self.salary = self.salary * (1 + percent + bonus) 27 28 def __repr__(self): 29 return'<Employee:{}>'.format(self.name) 30 31 def working(self): 32 print('Employee:{},is working...'.format(self.name)) 33 34 35 class Programer(Employee): 36 def __init__(self,department,name,birthdate,salary,specialty,project): 37 super().__init__(department,name,birthdate,salary) 38 self.specialty = specialty 39 self.project = project 40 41 def working(self): 42 print('Programer:{} is developing a project:{}...'.format(self.name,self.project)) 43 44 45 class HR(Employee): 46 def __init__(self,department,name,birthdate,salary,qualification_level=1): 47 Employee.__init__(self,department,name,birthdate,salary) 48 self.qualification_level = qualification_level 49 50 def working(self): 51 print('HR:{} is interview new employee...'.format(self.name)) 52 53 54 if __name__ == '__main__': 55 # p = Programer('R&D department','Peter',datetime.date(1990,3,3),8000.00,'Flask','CRM') 56 # print(p) 57 # print(p.department) 58 # print(p.salary) 59 # p.give_raise(.2,.1) 60 # print(p.salary) 61 # p.working() 62 # print(p.age) 63 # 64 # hr = HR('HR Department','Marry',datetime.date(1992,4,4),6000,qualification_level=3) 65 # hr.give_raise(.1) 66 # print(hr.salary) 67 # hr.working() 68 69 dep = Department('R&D','010-87718391','Zhang da Shan') 70 p = Programer(dep,'Peter',datetime.date(1990,3,3),8000,'Python,Flask','Xmall') 71 p.give_raise(.2,.1) 72 print(p.salary) 73 print(p.department) 74 print(p.department.department) 75 print(p.department.phone) 76 print(p.department.manager)
Part 2:Python学习注意点
=====================
总结面向对象的三大特征:
1.封装:
- 面向对象第一步 : 将属性和方法封装到一个抽象的类中
- 外界使用类创建对象,然后让对象调用方法
- 对象方法的细节都封装在类的内部
2.继承:
- 实现代码的重用,相同的代码不需要重复写
- 子类继承自父类,具有传递性,可以继承父类的所有属性和方法,并且直接使用父类中已经封装好的方法
- 子类针对自己特有的需求,编写特定的代码,封装子类特有的属性和方法
- 如果子类重写了父类的方法,在运行中,只会调用在子类中重写的方法而不会调用父类方法
3.多态:
- 不同的子类对象调用相同的方法,产生不同的执行结果
- 多态可以增加代码的灵活度
- 以继承和重写父类的方法为前提
- 调用方法,不会影响到类的内部设计
浙公网安备 33010602011771号