多态
import math
class Circle:
def __init__(self,radius):
self.radius = radius
@property
def area(self):
return math.pi*self.radius **2
def get_area(self):
return math.pi * self.radius **2
c = Circle(4.0)
print("圆的面积是:{}".format(c.get_area()))
print("圆的面积是:{}".format(c.area))
class Employee:
def __init__(self,department,name,birthdate,salary):
self.department = department
self.name = name
self.birthdate = birthdate
self.salary = salary
def give_raise(self,percent,bonus = .0)
self.salary = self.salary *(1+ percent) + bonus
def __repr__(self):
return'<员工:{}>'.format(self.name)
def working(self):
print('员工:{},在工作...'.format(self.name))
class Programmer(Employee):
def__init__(self,department,name,birthdate,salary,specialty,project):
super().__init__(department,name,birthdate,salary)
(或Employee.__init__(self,department,name,birthdate,salary))
self.specialty = specialty
self.project = project
def working (self):
print('程序员:{}在开发项目:{}...'.format(self.name,self.project))
if __name__=='__main__':
p = Programmer('技术部',‘Peter’,datetime.date(1990,3,3),8000,'Flask','CRM')
多态:属于同一类型的不同实例,对同一消息做出不同响应
class Department:
def__init__(self, department,phone,manager)
self.department = department
self.phone = phone
self.manager = manager
class Employee:
def__init__(self, department: Department,name)
self.department = department
self.name = name
if __name__ =='__main__':
dep = Department ('技术部','010-87718391','张三')
p = Programmer(dep,'Peeter')
print (p.department)