封装应用一:求圆的面积
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)
面向对象编程OOP 03
最新推荐文章于 2020-04-21 19:15:37 发布