面向对象编程OOP 03

封装应用一:求圆的面积
import math
class Circle:
def init(self,radius):
self.radius = radius
def get_area(self):
return math.pi * self.radius**2
c = Circle(4.0)
print(‘圆的面积是:{}’.format(c.get_area()))
返回的值为圆的面积是:50.26548245743669
—————————————————————————————
封装应用二:求圆的面积
import math
class Circle:
def init(self,radius):
self.radius = radius
@property #用属性来进行声明
def area(self):
return math.pi * self.radius ** 2
c = Circle(4.0)
print(‘圆的面积是:{}’.format(c.area))
返回的值为圆的面积是:50.26548245743669
—————————————————————————————
继承应用一:
import datetime
class Employee:
def init(self,department,name,birthday,salary):
self.department =department
self.name = name
self.birthday = birthday
self.salary = salary
@property
def age(self):
return datetime.date.today().year - self.birthday.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): #继承上面Employee
def init(self,department,name,birthday,salary,specialty,project):
super().init(department,name,birthday,salary) #直接用上面__init__的信息
self.specialty = specialty
self.project = project
def working(self):
print(‘程序员:{}在开发项目{}…’.format(self.name,self.project))
class HR(Employee):
def init(self,department,name,birthday,salary,qualification_level =1):
Employee.init(self,department,name,birthday,salary)
self.qualification_level = qualification_level
def working(self):
print(‘人事:{}正在面试新员工…’.format(self.name))
if name == ‘main’:
p = Programer(‘技术部’,‘Peter’,datetime.date(1990,3,3),8000,‘Flask’,‘CRM’)
print§ #__repr__return的值
print(p.department)
print(p.salary)
p.give_raise(.2,.1)
print(p.salary)
p.working()
print(p.age)
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 ‘<部门:{}>’.format(self.department)
class Employee:
def init(self,department:Department,name,birthday,salary):
self.department =department
self.name = name
self.birthday = birthday
self.salary = salary
@property
def age(self):
return datetime.date.today().year - self.birthday.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): #继承上面Employee
def init(self,department,name,birthday,salary,specialty,project):
super().init(department,name,birthday,salary) #直接用上面__init__的信息
self.specialty = specialty
self.project = project
def working(self):
print(‘程序员:{}在开发项目{}…’.format(self.name,self.project))
class HR(Employee):
def init(self,department,name,birthday,salary,qualification_level =1):
Employee.init(self,department,name,birthday,salary)
self.qualification_level = qualification_level
def working(self):
print(‘人事:{}正在面试新员工…’.format(self.name))
if name == ‘main’:
dep = Department(‘技术部’,‘010-87718391’,‘张大三’)
p = Programer(dep,‘Peter’,datetime.date(1990,3,3),8000,‘Python,Flask’,‘XMall’)
p.give_raise(.2,.1)
print(p.salary)
print(p.department.phone)
print(p.department.manager)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值