码迷,mamicode.com
首页 > 其他好文 > 详细

练习41:学着去说面向对象

时间:2020-07-29 21:49:24      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:readlines   init   name   str   return   efi   out   dom   sed   

一 词汇训练

  • 类(class):告诉python创建一个新类型的东西。(Tell python to make a new type of thing).
  • 对象(object):两种含义:最基本类型的东西,任何实例。(the most basic type of thing,and any instance of something)。
  • 实例(instance):当你告诉python创建一个类的时候你所得到的东西。(What you get when you tell pythong to create a class).
  • def:你如何在类里面定义一个函数。(How you define a function inside a class)。
  • self:在一个类的函数里面,self是被访问的实例/对象的一个变量。(inside the functions in a class,self is a variable for the instance/object being accessed)。
  • 继承(inheritance):关于一个类能从另一个类那里继承它的特征的概念,很像你和你的父母。(The concept that one class can inhert traits from other class,much like you and your parents)。
  • 组合(composition):关于一个类可以由其它一些类构成的概念,很像一辆车包含几个轮子。(The concept that a class can be composed of other classes as parts,much like how a car has wheels)。
  • 属性(attribute):类所拥有的从组合那里得到的特性,通常是变量。(A property classes have that are from composition and are usually variables)。
  • is-a:一种用来表达某物继承自一种东西的表述,就像“三文鱼是一种鱼”。(A phrase to say that something are from inherits from another,as in a "salmon" is a "fish")
  • has-a:一种用来表达某物是由一些东西组成或具有某种特性的表述,就像“三文鱼有一个嘴巴。”(A phrase to say that something is composed of other things or has a trait,as in "a salmon has-a mouth.")

二 短语训练

  • class X(Y) :创建一个名为 X 并继承自 Y 的类。(“Make a class named X that is-a Y.”)
  • class X(object): def __init__(self, J):类 X 有一个带有 self 和 J 参数的 __init__ 函数。(“class X has-a __init__ that takes self and J parameters.”)
  • class X(object): def M(self, J) :类 X 有一个带有 self 和 J 参数的 M 函数。(“class X has-a function named M that takes self and J parameters.”)
  • foo = X() :设 foo 为类 X 的一个实例。(“Set foo to an instance of class X.”)
  • foo.M(J)  从 foo 那里获取 M 函数,并用 self 和 J 参数来调用它。(“From foo, get the M function, and call it with parameters self, J.”)
  • foo.K = Q  从 foo 那里获取 K 属性,并设它为 Q。(“From foo, get the K attribute, and set it to Q.”)

三 代码

1 相关知识

2 代码

 1 import random
 2 from urllib.request import urlopen
 3 import sys
 4 
 5 WORD_URL = "http://learncodethehardway.org/words.txt"
 6 WORDS = []
 7 
 8 PHRASES = {
 9     "class %%%(%%%):":
10         "Make a class named %%% that is-a %%%.",
11     "class %%%(object):\n\tdef __init__(self,***)":
12         "class %%% has-a __init__ that takes self and *** params.",
13     "class %%%(object):\n\tdef ***(self,@@@)":
14         "class %%% has-a function *** that takes self and @@@ params.",
15     "*** = %%%()":
16             "Set *** to an instance of class %%%.",
17     "***.***(@@@)":
18             "From *** get the *** function,call it with params self @@@.",
19     "***.*** = ‘***‘":
20             "From *** get the *** attribute and set it to ‘***‘."
21 }
22 
23 # do they want to drill phrases first
24 if len(sys.argv) == 2 and sys.argv[1] == "english":
25     PHRASE_FIRST = True
26 else:
27     PHRASE_FIRST = False
28 
29 # load up the words from the website
30 for word in urlopen(WORD_URL).readlines():
31     WORDS.append(str(word.strip(),encoding="utf-8"))
32 
33 def convert(snippet,phrase):
34     class_names = [w.capitalize() for w in
35     random.sample(WORDS,snippet.count("%%%"))]
36     other_names = random.sample(WORDS,snippet.count("***"))
37     results = []
38     param_names = []
39     
40     for i in range(0,snippet.count("@@@")):
41         param_count = random.randint(1,3)
42         para_names.append(,.join(
43             random.sample(WORDS,param_count)))
44     
45     for sentence in snippet,phrase:
46         result = sentence[:]
47         
48         # fake class names
49         for word in class_names:
50             result = result.replace("%%%",word,1)
51         
52         # fake other names
53         for word in other_names:
54             result = result.replace("***",word,1)
55         
56         # fake parameter lists
57         for word in param_names:
58             result = result.replace("@@@",word,1)
59         
60         results.append(result)
61     return results
62 
63 # keep going until they hit CTRL-D
64 try:
65     while True:
66         snippets = list(PHRASES.keys())
67         random.shuffle(snippets)
68         
69         for snippet in snippets:
70             phrase = PHRASES[snippet]
71             question,answer = convert(snippet,phrase)
72             if PHRASE_FIRST:
73                 question,answer = answer,question
74             
75             print(question)
76             
77             input("> ")
78             print(f"ANSWER:{answer}\n\n")
79 except EOFError:
80     print("\nBye")

练习41:学着去说面向对象

标签:readlines   init   name   str   return   efi   out   dom   sed   

原文地址:https://www.cnblogs.com/luoxun/p/13399256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!