Skip to content

Commit b98f9bf

Browse files
author
Spencer.Luo
committed
change story
1 parent 878487e commit b98f9bf

File tree

1 file changed

+93
-2
lines changed

1 file changed

+93
-2
lines changed

pattern/Adapter.py

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,74 @@ def receptionistSuitable(self, person):
106106
"""
107107
return person.getHeight() >= 165;
108108

109+
# Version 1.0
110+
# =======================================================================================================================
111+
from abc import ABCMeta, abstractmethod
112+
# 引入ABCMeta和abstractmethod来定义抽象类和抽象方法
113+
114+
class SocketEntity:
115+
"""接口类型定义"""
116+
117+
def __init__(self, numOfPin, typeOfPin):
118+
self.__numOfPin = numOfPin
119+
self.__typeOfPin = typeOfPin
120+
121+
def getNumOfPin(self):
122+
return self.__numOfPin
123+
124+
def setNumOfPin(self, numOfPin):
125+
self.__numOfPin = numOfPin
126+
127+
def getTypeOfPin(self):
128+
return self.__typeOfPin
129+
130+
def setTypeOfPin(self, typeOfPin):
131+
self.__typeOfPin = typeOfPin
132+
133+
134+
class ISocket(metaclass=ABCMeta):
135+
"""插座类型"""
136+
137+
def getName(self):
138+
"""插座名称"""
139+
pass
140+
141+
def getSocket(self):
142+
"""获取接口"""
143+
pass
144+
145+
146+
class ChineseSocket(ISocket):
147+
"""国标插座"""
148+
149+
def getName(self):
150+
return "国标插座"
151+
152+
def getSocket(self):
153+
return SocketEntity(3, "八字扁型")
154+
155+
156+
class BritishSocket:
157+
"""英标插座"""
158+
159+
def name(self):
160+
return "英标插座"
161+
162+
def socketInterface(self):
163+
return SocketEntity(3, "T字方型")
164+
165+
class AdapterSocket(BritishSocket, ISocket):
166+
"""插座转换器"""
167+
168+
def getName(self):
169+
return "英标插座转换器"
170+
171+
def getSocket(self):
172+
socket = super().socketInterface()
173+
socket.setTypeOfPin("八字扁型")
174+
return socket
175+
176+
109177

110178
# Version 2.0
111179
#=======================================================================================================================
@@ -416,6 +484,29 @@ def testReader():
416484
reader.closeFile()
417485

418486

419-
testPerson()
487+
def canChargeforDigtalDevice(name, socket):
488+
if socket.getNumOfPin() == 3 and socket.getTypeOfPin() == "八字扁型":
489+
isStandard = "符合"
490+
canCharge = "可以"
491+
else:
492+
isStandard = "不符合"
493+
canCharge = "不能"
494+
495+
print("[%s]:\n针脚数量:%d,针脚类型:%s; %s中国标准,%s给大陆的电子设备充电!"
496+
% (name, socket.getNumOfPin(), socket.getTypeOfPin(), isStandard, canCharge))
497+
498+
def testSocket():
499+
chineseSocket = ChineseSocket()
500+
canChargeforDigtalDevice(chineseSocket.getName(), chineseSocket.getSocket())
501+
502+
britishSocket = BritishSocket()
503+
canChargeforDigtalDevice(britishSocket.name(), britishSocket.socketInterface())
504+
505+
adapterSocket = AdapterSocket()
506+
canChargeforDigtalDevice(adapterSocket.getName(), adapterSocket.getSocket())
507+
508+
509+
# testPerson()
420510
# testAdapter()
421-
# testReader()
511+
# testReader()
512+
testSocket()

0 commit comments

Comments
 (0)