面向对象编辑OOP3

多态:
同一类型的不同实例对同一消息做出的不同反应。
第一种方法

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在面试新员工

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值