国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語(yǔ)言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Python設(shè)計(jì)模式編程中解釋器模式的簡(jiǎn)單程序示例分享

Python設(shè)計(jì)模式編程中解釋器模式的簡(jiǎn)單程序示例分享

2020-08-15 11:51ponder008 Python

這篇文章主要介紹了Python設(shè)計(jì)模式編程中解釋器模式的簡(jiǎn)單程序示例分享,解釋器模式強(qiáng)調(diào)用抽象類來(lái)表達(dá)程序中將要實(shí)現(xiàn)的功能,需要的朋友可以參考下

模式特點(diǎn):給定一個(gè)語(yǔ)言,定義它的文法的一種表示,并定義一個(gè)解釋器,這個(gè)解釋器使用該表示來(lái)解釋語(yǔ)言中的句子。

我們來(lái)看一下下面這樣的程序結(jié)構(gòu):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Context:
  def __init__(self):
    self.input=""
    self.output=""
 
class AbstractExpression:
  def Interpret(self,context):
    pass
 
class Expression(AbstractExpression):
  def Interpret(self,context):
    print "terminal interpret"
 
class NonterminalExpression(AbstractExpression):
  def Interpret(self,context):
    print "Nonterminal interpret"
 
if __name__ == "__main__":
  context= ""
  c = []
  c = c + [Expression()]
  c = c + [NonterminalExpression()]
  c = c + [Expression()]
  c = c + [Expression()]
  for a in c:
    a.Interpret(context)

那么它所體現(xiàn)出的類圖是這樣的:

Python設(shè)計(jì)模式編程中解釋器模式的簡(jiǎn)單程序示例分享

 

再來(lái)看一個(gè)例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#encoding=utf-8
#
#by panda
 
def printInfo(info):
  print unicode(info, 'utf-8').encode('gbk'),
 
#上下文類:演奏內(nèi)容
class PlayContext():
  text = None
  PlayText = None
 
#抽象表達(dá)式類
class Expression():
  def Interpret(self, context):
    if len(context.PlayText) == 0:
      return
    else:
      playKey = context.PlayText[0:1]
      context.PlayText = context.PlayText[2:]
      tmp = context.PlayText.index(' ') #找出第一個(gè)空格出現(xiàn)的位置
      playValue = context.PlayText[0:tmp]
      context.PlayText = context.PlayText[tmp+1:]
      self.Excute(playKey,playValue)
   
  def Excute(self,playKey,playValue):
    pass
 
#音高
class Pitch(Expression):
  pitch = None
  def Excute(self, key, value):
    value = int(value)
    if value == 1:
      self.pitch = '低音'
    elif value == 2:
      self.pitch = '中音'
    elif value == 3:
      self.pitch = '高音'
    printInfo(self.pitch)
     
#音符
class Note(Expression):
  Notes = {
  'C':1,  
  'D':2,
  'E':3,  
  'F':4,  
  'G':5,  
  'A':6,  
  'B':7,  
  }
  note = None
  def Excute(self, key, value):   
    self.note = self.Notes[key]
    printInfo('%d' % self.note)
 
 
def clientUI():
  context = PlayContext()
  context.PlayText = "O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3 "
  expression = None;
  while(len(context.PlayText) > 0):
    str = context.PlayText[0:1];
    if(str == 'O'):
      expression = Pitch()
    elif(str == 'C' or str == 'D' or str == 'E' or str == 'F' or str == 'G' or str == 'A' or str == 'B' or str == 'P'):
      expression = Note()
    expression.Interpret(context)
       
  return
 
if __name__ == '__main__':
  clientUI();


類圖:

Python設(shè)計(jì)模式編程中解釋器模式的簡(jiǎn)單程序示例分享

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 国产亚洲精品久久久久久无几年桃 | 日本aⅴ毛片成人实战推荐 伊人久久在线 | 国产一区二区三区欧美 | 日本三级电影网站 | 精品美女久久 | 日韩精品一区二区三区在线观看视频网站 | 免费看黄色一级 | 艹久久| 黑森林av凹凸导航 | 人妖天堂狠狠ts人妖天堂狠狠 | 日韩有码在线观看 | 中文字幕综合在线 | 国产精品久久久久久久久久久久冷 | 欧美 日韩 国产 在线 | 国产精品日韩一区二区 | 四虎免费在线播放 | 成人精品国产 | 国产精品国产三级国产aⅴ中文 | 日本精品一区二区三区视频 | 国产乱码精品一区二区三区av | www,四虎 | 嫩草精品 | 久久精品国产99国产精2020新增功能 | 色狠狠网 | 国产一区二区黑人欧美xxxx | 成人h动漫精品一区二区器材 | 久久伊 | 欧洲一级毛片 | 日本福利网站 | 国产亚洲精品女人久久久久久 | 精品日韩一区二区三区 | av亚洲在线 | 日韩国产一区 | 国产情侣免费视频 | 国产成人精品久久二区二区 | 一区二区三区国产视频 | 无码一区二区三区视频 | 日本一区二区在线视频 | 尤物在线观看网站 | 国内自拍网站 | 亚洲视频一区二区三区在线观看 |