08

'#面向对象的特征 封装 继承 多态#

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值