一. 面向对象
1. 特征
- 封装
- 继承
- 多态
(1) 属于同一类型的不同事例
class Book:
count = 0
#初始化执行
def __init__(self,title,price=0.0,author=None): #self自身的东西等于传过来的东西
self.title = title
self.price = price
self.author = author
Book.count +=1
#删除对象执行
def __del__(self):
Book.count -= 1
def print_info(self):
print(self.title, self.price,self.author)
def __repr__(self): # 定义对象在交互式提示符下显示方式
return '<book {} at {}>'.format(self.title, id(self))
def __str__(self): # 打印时执行
return '<book {} at {}>'.format(self.title, id(self))
def cls_method(cls):
print('类函数,与实例无关')
#@staticmethod #加上可以通过实例调用
def ststic_method():
print('静态函数,逻辑上与实例无关')
if __name__ =='__main__':
book = Book('Python classical',price=29.0,author='Tom')
book2 = Book('Flask')
book3 = Book('ASP.net')
del(book3)
print('number of book:{}'.format(Book.count))
Book.cls_method(book)
Book.ststic_method()
过滤值
import datetime
class Student:
def __init__(self,name,gender,birthdate):
self.name = name
self.gender = gender
self.birthdate = birthdate
@property #表明属性,可以进一步过滤
def age(self):
return datetime.date.today().year - self.birthdate.year
@age.setter #设置器
def age(self,value):
raise AttributeError('禁止赋值年龄!')
@age.deleter #删除器
def age(self):
raise AttributeError('年龄不能删除')
def get_age(self): #过滤,不可以进一步过滤
return datetime.date.today().year - self.birthdate.year
if __name__ == '__main__':
s = Student('Tom',20,datetime.date(1992,3,1))
print(s.birthdate)
print(s.get_age())
print(s.age)
继承
import datetime
class Employee:
def __init__(self,department,name,birthdate,salary):
self.department = department
self.name = name
self.birthdate = birthdate
self.salary = salary
@property
def age(self):
return datetime.date.today().year-self.birthdate.year
def give_raise(self,percent,bonus=.0):
self.salary = self.salary * (1 + percent + bonus)
def __repr__(self):
return'<employee:{}>'.format(self.name)
def working(self):
print('enployee:{} is working',format(self.name))
#继承
class Programmer(Employee):
def __init__(self,department,name,birthdate,salary,speciality,project):
super().__init__(department,name,birthdate,salary)
self.speciality = speciality
self.project = project
def working(self): #多态
print('Programmer: {} is developing program{}'.format(self.name,self.project))
class HR(Employee):
def __init__(self,department,name,birthdate,salary,qualification_level=1):
Employee.__init__(self,department,name,birthdate,salary)
self.qualifaction_level = qualification_level
def working(self):
print('HR:{} is interviewing '.format(self.name))
if __name__ == '__main__':
p = Programmer('Technology', 'Peter', datetime.date(1990, 3, 3), 8000, 'Flask', 'CRM')
print(p.department)
print(p.salary)
p.give_raise(.2,.1)
print(p.salary)
p.working()
print(p.age)
hr = HR('HR','Marry',datetime.date(1992,4,4),6000,qualification_level=3)
hr.give_raise(.1)
print(hr.salary)
hr.working()
类和类的关联
import datetime
class Department:
def __init__(self,department,phone,manager):
self.department = department
self.phone = phone
self.manager = manager
def __repr__(self):
return'<department:{}>'.format(self.department)
class Employee:
def __init__(self,department:Department,name,birthdate,salary):
self.department = department
self.name = name
self.birthdate = birthdate
self.salary = salary
@property
def age(self):
return datetime.date.today().year-self.birthdate.year
def give_raise(self,percent,bonus=.0):
self.salary = self.salary * (1 + percent + bonus)
def __repr__(self):
return'<employee:{}>'.format(self.name)
def working(self):
print('enployee:{} is working',format(self.name))
#继承
class Programmer(Employee):
def __init__(self,department,name,birthdate,salary,speciality,project):
super().__init__(department,name,birthdate,salary)
self.speciality = speciality
self.project = project
def working(self): #多态
print('Programmer: {} is developing program{}'.format(self.name,self.project))
class HR(Employee):
def __init__(self,department,name,birthdate,salary,qualification_level=1):
Employee.__init__(self,department,name,birthdate,salary)
self.qualifaction_level = qualification_level
def working(self):
print('HR:{} is interviewing '.format(self.name))
if __name__ == '__main__':
dep = Department('Techonology','010-8771652','Dasan')
p = Programmer(dep,'Peter',datetime.date(1990,3,1),8000,'Flask','XMall')
p.give_raise(.2,.1)
print(p.salary)
print(p.department.phone)
print(p.department.manager)