pythonベース(8):パッケージ、継承、マルチステート
46256 ワード
基礎(八)オブジェクト向け 特徴 パッケージ例 1.構造関数 2.ろ過装飾器 継承例 1. EmployeeとProgramerクラスオーダー継承 2. Employee、Programer、HR類の多継承は混乱しやすい マルチステート例 オブジェクト向け
とくせい
カプセル化、継承、マルチステート
パッケージの例
1.解析関数
c++には似たような概念があり、repr関数はオブジェクトを出力する際にすべての情報を出力しない
2種類の関数静的関数を出力し、論理的にインスタンスに関係なく
2.フィルター装飾器
property,setter,deleter
出力1992-03-01 27 1996-08-26 23変更年齢エラー
出力Traceback(most recent call last):1992-03-01 File「F:/teacher/pythonbasetry/day 7/Test.py」、line 33、in s.age=20 27 File「F:/teacher/pythonbasetry/day 7/Test.py」、line 17、in age raise AttributeError(‘付与年齢禁止!’)AttributeError:付与年齢禁止!
継承例
1.EmployeeとProgramerクラスオーダーの継承
出力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類の多継承は混乱しやすい
マルチステートの例
同じタイプの異なるインスタンスが同じメッセージに対して異なる応答を行うは、関連 を含む.
とくせい
カプセル化、継承、マルチステート
パッケージの例
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/day 7/Test.py」、line 33、in s.age=20 27 File「F:/teacher/pythonbasetry/day 7/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)