import datetime
class Book:
count = 0
#初始化执行
def __init__(self,title,price = 0.0,publisher = '',pubdate = datetime.date.today()):#初始化器,构造函数初始化内成员用的
self.title = title,
self.price = price
self.publisher = publisher
self.pubdate = pubdate
Book.count += 1
#删除对象执行
def __del__(self):
Book.count -= 1
def __repr__(self):
print('<图书{}at{}>'.format(self.title,id(self)))
def __str__(self):
print('[图书{}at{}]'.format(self.title, id(self)))
def print_informance(self):
print('书名:{}'.format(self.title))
print('价格:{}'.format(self.price))
print('出版社:{}'.format(self.publisher))
print('出版日期:{}'.format(self.pubdate))
def cls_method(cls):
print('类函数')
def static_method:
print('静态函数,逻辑上与实例无关,不可以通过对象调用')
if __name__ == '__main__':
book1 = Book('Python入门', 88, '清华大学出版社', datetime.date(2020, 2, 2)) # 实例化
book2 = Book('QDN')
del (book1)
print('图书总数目:{}'.format(Book.count))
Book.cls_method(book1)
Book.static_method()
import datetime
class Studet():
def __init__(self,name,gender,brithdate):
self.name = name
self.gender = gender
self.brithdate = brithdate
#属性
@property
#属性之一:年龄
def age(self):
return datetime.date.today().year - self.brithdate.year
#添加设置器,设置年龄不能修改
@age.setter
def age(self,value):
raise ArithmeticError('年龄禁止赋值!')
#添加删除器,设置年龄不能删除
@age.deleter
def age(self):
raise ArithmeticError('年龄禁止删除!')
if __name__ == '__main__':
student1 = Studet('mateng','男',datetime.date(1994,5,31))
print(student1.name,student1.gender,student1.age)
#student1.age = 18
del (student1.age)
继承
import datetime
class Department:
def __init__(self,name,phone,manager):
self.name = name
self.phone = phone
self.manager = manager
def __repr__(self):
print('部门:{}'.format(self.name))
class Employee:
def __init__(self,dapartment:Department,name,birthdate,salary):
self.dapartment = dapartment
self.name = name
self.birthdate = birthdate
self.salary = salary
@property
def age(self):
return datetime.date.today().year - self.birthdate.year
def __repr__(self):
print('员工:{}'.format(self.name))
def give_raise(self,percent,bonus=0):
self.salary = self.salary * (1 + percent + bonus)
def working(self,):
print('员工{}在工作...'.format(self.name))
class Programer(Employee):
def __init__(self,dapartment:Department,name,birthdate,salary,speciality,project):
super().__init__(dapartment,name,birthdate,salary)#super是一种类继承方式,但是不建议使用
self.speciality = speciality
self.project = project
def working(self):
print('员工{}正在使用{}开发{}项目'.format(self.name,self.speciality,self.project))
class HR(Employee):
def __init__(self,dapartment:Department,name,birthdate,salary,level):
Employee.__init__(self,dapartment,name,birthdate,salary)#明确写出基类的名称,self必须写上,推荐使用这种方法
self.level = level
def working(self,):
print('HR{}正在面试应聘者'.format(self.name))
if __name__ == '__main__':
dep = Department('技术部','010-88888888','MaTeng')
p1 = Programer(dep,'Tom',datetime.date(1994,5,31),20000,'python','强化学习')
#print(p1.dapartment)
print(p1.name)
print(p1.age)
print(p1.salary)
p1.give_raise(2,1)
print(p1.salary)
p1.working()
p2 = HR(dep,'Mary',datetime.date(1998,8,8),8000,2)
print(p2.name)
print(p2.age)
print(p2.salary)
print(p2.level)
p2.working()