python中的封装继承多态_python基础(八):封装、继承、多态

面向对象

特征

封装、继承、多态

封装示例

1.析构函数

c++有相似概念,repr函数输出对象时不输出全部信息

class Book:

count = 0

#def __init__(self,title,price = 0.0,author=None,count = 0):

##构造函数

def __init__(self, title, price=0.0, author=None):

self.title = title

self.price = price

self.author = author

Book.count += 1

##析构函数,删除对象使用

def __del__(self):

Book.count -= 1

##控制台上直接使用对象执行

def __repr__(self):

return ''.format(self.title,id(self))

##打印时执行

def __str__(self):

return '[BOOK{},PRICE{}]'.format(self.title,self.price)

def print_info(self):

print(self.title,self.price,self.author)

def cls_method(cls):

print('类函数')

def static_method():

print('静态函数,逻辑上与实例无关')

if __name__ == '__main__':

book = Book('python base',price = 20,author = 'Tom')

#Book.count += 1

book2 = Book('Flask')

# Book.count += 1

book3 = Book('ASP.net')

#Book.count += 1

del(book3)

print(Book.count)

Book.cls_method(book)

Book.static_method()

输出

2

类函数

静态函数,逻辑上与实例无关

2.过滤装饰器

property,setter,deleter

import datetime

class Student:

def __init__(self,name,birthdate):

self.name = name

self.birthdate = birthdate

@property

##属性,好处进一步过滤

def age(self):

return datetime.date.today().year - self.birthdate.year

@age.setter

##装饰器

def age(self,value):

raise AttributeError('禁止赋值年龄!')

@age.deleter

def age(self):

raise ArithmeticError('年龄不能删除')

# def get_age(self):

# return datetime.date.today().year - self.birthdate.year

if __name__ == '__main__':

s = Student('Tom',datetime.date(1992,3,1))

print(s.birthdate)

print(s.age)

s.birthdate = datetime.date(1996,8,26)

print(s.birthdate)

print(s.age)

输出

1992-03-01

27

1996-08-26

23

更改年龄报错

import datetime

class Student:

def __init__(self,name,birthdate):

self.name = name

self.birthdate = birthdate

@property

##属性,好处进一步过滤

def age(self):

return datetime.date.today().year - self.birthdate.year

@age.setter

##装饰器

def age(self,value):

raise AttributeError('禁止赋值年龄!')

@age.deleter

def age(self):

raise ArithmeticError('年龄不能删除')

if __name__ == '__main__':

s = Student('Tom',datetime.date(1992,3,1))

print(s.birthdate)

print(s.age)

s.birthdate = datetime.date(1996,8,26)

s.age = 20

print(s.birthdate)

print(s.age)

输出

Traceback (most recent call last):

1992-03-01

File “F:/teacher/pythonbasetry/day7/Test.py”, line 33, in

s.age = 20

27

File “F:/teacher/pythonbasetry/day7/Test.py”, line 17, in age

raise AttributeError(‘禁止赋值年龄!’)

AttributeError: 禁止赋值年龄!

继承示例

1. Employee和Programer类 单继承

import datetime

class Employee:

def __init__(self,name,birthdate,department,salary):

self.name = name

self.birthdate = birthdate

self.department = department

self.salary = salary

def give_raise(self,percent,bonus=.0):

self.salary = self.salary * (1 + percent + bonus)

@property

def age(self):

return datetime.date.today().year - self.birthdate.year

def __repr__(self):

return ''.format(self.name)

def working(self):

print ('员工 : {} 工作ing'.format(self.name))

class Programer(Employee):

def __init__(self,name,birthdate,department,salary,specialty,project):

##基类

super().__init__(name,birthdate,department,salary)

self.specialty = specialty

self.project = project

def working(self):

print('程序猿:{} 在开发:{}'.format(self.name,self.project))

if __name__ == '__main__':

p = Programer('kitty',datetime.date(1996,8,26),'tech',8000,'python','cv')

print(p)

print(p.department)

print(p.birthdate)

print(p.salary)

p.give_raise(.2,.1)

print(p.salary)

p.working()

print(p.age)

输出

tech

1996-08-26

8000

10400.0

程序猿:kitty 在开发:cv

23

注:注意super中参数顺序

报错

return datetime.date.today().year - self.birthdate.year AttributeError: ‘str’ object has no attribute ‘year’

是因为参数顺序错误

2. Employee、Programer、HR类 多继承易混乱

import datetime

class Employee:

def __init__(self,name,birthdate,department,salary):

self.name = name

self.birthdate = birthdate

self.department = department

self.salary = salary

def give_raise(self,percent,bonus=.0):

self.salary = self.salary * (1 + percent + bonus)

@property

def age(self):

return datetime.date.today().year - self.birthdate.year

def __repr__(self):

return ''.format(self.name)

def working(self):

print ('员工 : {} 工作ing'.format(self.name))

class Programer(Employee):

def __init__(self,name,birthdate,department,salary,specialty,project):

##基类

super().__init__(name,birthdate,department,salary)

self.specialty = specialty

self.project = project

def working(self):

print('程序猿:{} 在开发:{}'.format(self.name,self.project))

class HR(Employee):

def __init__(self,name,birthdate,department,salary,level = 1):

##多继承不建议supersuper().__init__()

Employee.__init__(self,name,birthdate,department,salary)

self.level = level

def working(self):

print('人事:{}面试ing'.format(self.name))

if __name__ == '__main__':

p = Programer('kitty',datetime.date(1996,8,26),'tech',8000,'python','cv')

print(p)

print(p.department)

print(p.birthdate)

print(p.salary)

p.give_raise(.2,.1)

print(p.salary)

p.working()

print(p.age)

hr = HR('haha',datetime.date(1992,8,22),'人事',9000,2)

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,name,birthdate,department:Department,salary):

self.name = name

self.birthdate = birthdate

self.department = department

self.salary = salary

def give_raise(self,percent,bonus=.0):

self.salary = self.salary * (1 + percent + bonus)

@property

def age(self):

return datetime.date.today().year - self.birthdate.year

def __repr__(self):

return ''.format(self.name)

def working(self):

print ('员工 : {} 工作ing'.format(self.name))

class Programer(Employee):

def __init__(self,name,birthdate,department,salary,specialty,project):

##基类

super().__init__(name,birthdate,department,salary)

self.specialty = specialty

self.project = project

def working(self):

print('程序猿:{} 在开发:{}'.format(self.name,self.project))

class HR(Employee):

def __init__(self,name,birthdate,department,salary,level = 1):

##多继承不建议supersuper().__init__()

Employee.__init__(self,name,birthdate,department,salary)

self.level = level

def working(self):

print('人事:{}面试ing'.format(self.name))

if __name__ == '__main__':

dep = Department('tech','010','Lily')

#department use dep

p = Programer('kitty',datetime.date(1996,8,26),dep,8000,'python','cv')

p.give_raise(.2,.1)

print(p.salary)

print(p.department)

print(p.department.phone)

未经允许不得转载:作者:571-雪同学,

转载或复制请以 超链接形式 并注明出处 拜师资源博客。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值