Python 입문 학습 노트 - 20. 대상 지향 - 고급 - 설명자 및 디자인 모델
24054 단어 Python 입문 학습 노트
####################################
# 1.
'''
###
> , (`__get__,__set__,__delete__`) , ,
###
> ( , , )
> , ,
###
>
'''
#
class PersonName():
__name = 'abc'
def __get__(self, instance, owner):
# print(self,instance,owner)
return self.__name
def __set__(self, instance, value):
# print(self,instance,value)
self.__name = value
def __delete__(self, instance):
# print(self,instance)
# del self.__name
print(' ')
#
class Person():
#
# ()
# ,
name = PersonName()
#
zs = Person()
print(zs.name)
zs.name = ' '
print(zs.name)
del zs.name
print(zs.name)
####################################
# 2.
'''
# : 0-100
:
1。 __init__
#
if score >= 0 and score <= 100:
self.score = score
。
2。 setattr
score ,
def __setattr__(self, key, value):
# score
if key == 'score':
#
if value >= 0 and value <= 100:
object.__setattr__(self, key, value)
else:
print(' ')
else:
object.__setattr__(self,key,value)
, , ,
?
3。
1. Score
2. score
3. ok
'''
# , id, ,
# class Student():
# def __init__(self,id,name,score):
# self.id = id
# self.name = name
# # self.score = score
#
# #
# if score >= 0 and score <= 100:
# self.score = score
# else:
# print(' ')
#
# def returnMe(self):
# info = f'''
# :{self.id}
# :{self.name}
# :{self.score}
# '''
# print(info)
#
#
# def __setattr__(self, key, value):
# # score
# if key == 'score':
# #
# if value >= 0 and value <= 100:
# object.__setattr__(self, key, value)
# else:
# print(' ')
# else:
# object.__setattr__(self,key,value)
#
class Score():
def __get__(self, instance, owner):
return self.__score
def __set__(self, instance, value):
if value >= 0 and value <= 100:
self.__score = value
else:
print(' ')
# score
class Student():
score = Score()
def __init__(self,id,name,score):
self.id = id
self.name = name
self.score = score
def returnMe(self):
info = f'''
:{self.id}
:{self.name}
:{self.score}
'''
print(info)
#
zs = Student(1011,' ',99)
zs.returnMe()
zs.score = -20
zs.score = 88
zs.returnMe()
####################################
# 3.
#
'''
class ScoreManage():
def __get__(self, instance, owner):
pass
def __set__(self, instance, value):
pass
def __delete__(self, instance):
pass
class Student():
score = ScoreManage()
'''
'''
# , property
class Student():
#
def getscore(self):
print('getscore')
def setscore(self,value):
print('setscore',value)
def delscore(self):
print('delscore')
# property , 1。__get__,2。__set__,3。__delete__
score = property(getscore,setscore,delscore)
zs = Student()
# print(zs.score)
# zs.score = 200
# del zs.score
'''
'''
# @property
class Student():
__score = None
@property
def score(self):
print('get')
return self.__score
@score.setter
def score(self,value):
print('set')
self.__score = value
@score.deleter
def score(self):
print('delete')
del self.__score
zs = Student()
# print(zs.score)
zs.score = 199
print(zs.score)
del zs.score
'''
####################################
# 4. ( )
'''
, , 。
, :
, ,
?
,
,
, 。
, , 。
python ?
1。 , ?
__new__
2。
, None
3。 ?
, , ,
, ,
'''
class Demo():
# 2. , None
__obj = None
# 1.
def __new__(cls, *args, **kwargs):
# 3。 ,
if not cls.__obj:
# , ,
cls.__obj = object.__new__(cls)
#
return cls.__obj
#
a = Demo()
b = Demo()
print(a)
print(b)
'''
<__main__.demo object="" at="">
<__main__.demo object="" at="">
'''
####################################
# 5.Mixin
'''
, 'is-a'
:
,
,
,
?
, , ,
?
, , 。。。
, , , , 。。
? , 。
?
, , , ?
, 'is-a' ?
?
java interface
python ,
'''
# vehicle
class vehicle():
#
def huo(self):
print(' ')
#
def ren(self):
print(' ')
#
class FlyingMixin():
def fly(self):
print(' 。。。')
#
class cart(vehicle):
pass
#
class airplane(vehicle, FlyingMixin):
pass
#
class helicopter(vehicle, FlyingMixin):
pass
# Flying, , 。 。
# ,1。 , 'is-a' 2。
# , , Mixin ,
# , ,
'''
, , FlyingMixin
FlyingMixin Minin , , Mixin
Mixin (mix-in)
Mixin , 。
Mixin , , Mixin
python Mixin
Mixin , ,
Mixin , Mixin, , 。。
Mixin ?
1. Mixin , ,
2. Mixin ,
3. ( Mixin )
4. 。
'''
####################################
# 6.
'''
:
1. , 。
2. , 。
3. , 。 。
, , , ,
, ,
, ( , )
:
,
,
, ,
, , , ,
, , ,
'''
import abc
# , metaclass metaclass=abc.ABCMeta
class WriteCode(metaclass=abc.ABCMeta):
# ,
@abc.abstractmethod
def write_php(self):
pass
def write_java(self):
print(' java ')
def write_python(self):
print(' python ')
#
# obj = WriteCode()
# print(obj)
#TypeError: Can't instantiate abstract class WriteCode with abstract methods write_php
# , ,
class Demo(WriteCode):
def write_php(self):
print(' php ')
a = Demo()
print(a)
a.write_java()
a.write_php()
a.write_python()
'''
:
, 。。。。
, 。
, , 。
'''
####################################