'#面向对象的特征 封装 继承 多态#
class Book:
def init(self,title,price=0.0,author=None):
self.title=title
self.price=price
self.author=author
def print_info(self):
print (self.title,self.price,self.author)
if name==‘main’:
book=Book(‘python’,author=‘jack’,price=90)
book.print_info()
import datetime
class Student:
def init(self,name,birthdate):
self.name=name
self.birthdate=birthdate
def get_age(self):
return datetime.date.today().year-self.birthdate.year
if name==‘main’:#主函数标识符
s=Student(‘tom’,datetime.date(1995,9,24))
print (s.birthdate)
print (s.get_age())
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)
# print (‘圆的面积是:{}’.format(c.get_area()))调用函数
print (‘面积:{}’.format(c.area))#调用字段
import datetime
import importlib
class Employee:
def init(self,name,birthdate,salary,department):
self.name=name
self.birthdate=birthdate
self.department=department
self.salary=salary
def repr(self):
return ‘<员工:{}>’.format(self.name)
def working(self):
print (‘员工:{},在工作’.format(self.name))
def give_raise(self,percent,bonus=.0):
self.salary=self.salary *(1 + percent + bonus)
class Programer(Employee):
def init(self,name,birthdate,salary,department,specialty,project):
super().init(name,birthdate,salary,department)
self.specialty=specialty
self.project=project
def working(self):
print (‘程序员:{}在开发项目{}’.format(self.name,self.project))
@property
def age(self):
return datetime.date.today().year-self.birthdate.year
class HR(Employee):
def init(self,name,birthdate,salary,department,qualification_level=1):
Employee.init(self,name,birthdate,salary,department)
self.qualification_level=qualification_level
def working(self):
print (‘人事{}正在面试’.format(self.name))
if name==‘main’:
p=Programer(‘pp’,datetime.date(1993,3,3),8000,‘技术部’,‘FLASK’,‘crm’)
# print§
# print (p.department)
# print (p.salary)
# p.give_raise(.2,.1)
# print (p.salary)
# p.working()
# print (p.age)
hr=HR(‘qq’,datetime.date(1992,2,1),6000,‘人事部’,qualification_level=3)
hr.give_raise(.1)
print (hr.salary)
hr.working()