多态:
同一类型的不同实例对同一消息做出的不同反应。
第一种方法
import math
class Circle:
def __init__(self,radius):
self.radius = radius
def get_area(self):
return math.pi * self.radius ** 2
c = Circle(6)
print("圆的面积是:{}".format(c.get_area()))
结果是:
圆的面积是:113.09733552923255
第二种方法
import math
class Circle:
def __init__(self,radius):
self.radius = radius
# def get_area(self):
# return math.pi * self.radius ** 2
@property
def area(self):
return math.pi * self.radius ** 2
c = Circle(6)
# print("圆的面积是:{}".format(c.get_area()))
print("圆的面积是:{}".format(c.area))
结果是:
圆的面积是:113.09733552923255
class Employee:
def __init__(self,department,name,birthdate,salary):
self.department = department
self.name = name
self.birthdate = birthdate
self.salary = salary
def __repr__(self):
return "<员工:{}>".format(self.name)
def working(self):
print("员工:{},在工作".format(self.name))
class Programer(Employee):
def __init__(self,department,name,birthdate,salary,specialty,project):
super().__init__(department,name,birthdate,salary)
self.specialty = specialty
self.project = project
def working(self):
print("程序员:{}在开发项目{}".format(self.name,self.project))
import datetime
p = Programer("技术部","peter",datetime.date(1990,3,1),8000,"python","CRM")
print(p.salary)
print(p.working())
print(p)
结果是:
8000
程序员:peter在开发项目CRM
None
<员工:peter>
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 Programer(Employee):
def __init__(self,department,name,birthdate,salary,specialty,project):
super().__init__(department,name,birthdate,salary)
self.specialty = specialty
self.project = project
def working(self):
print("程序员:{}在开发项目{}".format(self.name,self.project))
import datetime
p = Programer("技术部","peter",datetime.date(1990,3,1),8000,"python","CRM")
print(p.salary)
print(p.working())
print(p)
p.give_raise(.2,.1)
print(p.salary)
结果是:
8000
程序员:peter在开发项目CRM
None
<员工:peter>
10400.0
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 "<员工:{}>".format(self.name)
def working(self):
print("员工:{},在工作".format(self.name))
class Programer(Employee):
def __init__(self,department,name,birthdate,salary,specialty,project):
super().__init__(department,name,birthdate,salary)
self.specialty = specialty
self.project = project
def working(self):
print("程序员:{}在开发项目{}".format(self.name,self.project))
import datetime
p = Programer("技术部","peter",datetime.date(1990,3,1),8000,"python","CRM")
print(p.salary)
print(p.working())
print(p)
p.give_raise(.2,.1)
print(p.salary)
print(p.age)
结果是:
8000
程序员:peter在开发项目CRM
None
<员工:peter>
10400.0
29
class Employee:
import datetime
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 "<员工:{}>".format(self.name)
class HR(Employee):
def __init__(self,department,name,birthdate,salary,qualifcation_level=1):
Employee.__init__(self,department,name,birthdate,salary)
self.qualifcation_level = qualifcation_level
def working(self):
print("人事:{}在面试新员工".format(self.name))
if __name__ == "__main__":
import datetime
hr = HR("人事部","tom",datetime.date(1985,3,3),9000,qualifcation_level=3)
hr.give_raise(.1)
print(hr.salary)
hr.working()
结果是:
9900.0
人事:tom在面试新员工