在这个课程中会学习面向对象的术语,然后巩固练习,就这样...
然后看不懂请百度,老外的思维确实有点难以理解...
专有词汇:
- 类(class):告诉Python创建新类型的东西。
- 对象(object):两个意思,即最基本的东西,或者某样东西的实例。
- 实例(instance):这是让Python创建一个类时得到的东西。
- def:这是在类里面定义函数的方法。
- self:在类的函数中,self指代被访问的对象或者实例的一个变量。
- 继承(inheritance):指一个类可以继承另一个类的特性,和父子关系类似。
- 组合(composition):指一个类可以将别的类作为它的部件构建起来,有点像车子和车轮的关系。
- 属性(attribute):类的一个属性,它来自于组合,而且通常是一个变量。
- 是什么(is-a):用来描述继承关系,如 Salmond is-a Fish(鲑鱼是一种鱼,鱼是父类,鲑鱼是子类)
- 有什么(has-a):用来描述某个东西是由另外一些东西组成的,或者某个东西有某个特征,如说Salmond has-a mouth(鲑鱼有一张嘴)。
措辞练习:
接下来Zed给我们除了一些代码,以及用来描述代码的句子。
- class X(Y): 创建一个将X的类,它是Y的一种,继承Y类的特性。
- class X(object):
- def __init__(self,J): 类X有一个__init__,它接受self和J作为参数。
- class X(object):
- def M(self,J): 类X有一个叫M的函数,它接受self和J作为参数。
- foo = X(): 将foo设为类X的一个实例。
- foo.M(J): 从foo中找到M函数,并使用self和J参数调用它。
- foo.K = Q: 从foo中获取K属性,并将其设为Q。
还有一段看起来更懵,不写了,是在看不懂就百度吧。
阅读测试:
这里有一小段Python代码,利用这些代码去记忆上述的专属词汇。
将这段代码命名为oop_test.py,你可以直接运行oop_test.py来练习,熟练之后你就可以用oop_test.py english两个参数来运行,这样可以反向练习。
另外这段代码调用了urllib的库,使用了其中的urlopen函数,该函数是用来打开网站获取文件对象的。
-
import random
# '''random函数,可以生成随机浮点数,整数,字符串,甚至帮你随机选择列表中的一个元素,打乱一组数据等'''
-
from urllib.request
import urlopen
#''' urlopen函数,可以帮助我们打开网址'''
-
import sys
-
-
WORD_URL =
"http://learncodethehardway.org/words.txt"
-
WORDS = []
-
-
PHRASES = {
-
"class %%%(%%%):":
-
"Make a class named %%% that is-a %%%.",
-
"class %%%(object):\n\tdef __init__(self,***)":
-
"class %%% has-a __init__ that takes self and *** params.",
-
"class %%%(object):\n\tdef ***(self,@@@)":
-
"class %%% has-a function *** that takes self and @@@ params.",
-
"*** = %%%()":
-
"Set *** to an instance of class %%%.",
-
"***.***(@@@)":
-
"From *** get the *** function, call it with params self, @@@.",
-
"***.*** = '***'":
-
"From *** get the *** attribute and set it to '***'."
-
}
-
-
# do they want to drill phrases first. ///他们想先操练短语吗?
-
if len(sys.argv) ==
2
and sys.argv[
1] ==
"english":
# '''判断argv参数是否是2个且第二个参数是english,如果是则为真,不是则为假'''
-
PHRASES_FIRST =
True
-
else:
-
PHRASES_FIRST =
False
-
-
# load up the words form the website. ///从网站下载单词。
-
for word
in urlopen(WORD_URL).readlines():
# '''打开网址,读取网址的所有行,每一行为一个元素,以列表的形式保存,读取列表中的所有元素(也就是单词)'''
-
WORDS.append(str(word.strip(), encoding=
"utf-8"))
# '''去除单词的首尾空格或换行符,以utf-8编码,以字符串的类型添加到列表WORDS中'''
-
-
-
def convert(snippet, phrases):
-
class_names = [w.capitalize()
for w
in random.sample(WORDS, snippet.count(
"%%%"))]
#''' 先统计snippet中‘%%%’出现的次数n,再随机返回WORDS中n个元素,然后再'''
-
other_names = random.sample(WORDS, snippet.count(
"***"))
-
results = []
-
param_names = []
-
-
for i
in range(
0, snippet.count(
"@@@")):
-
param_count = random.randint(
1,
3)
-
param_names.append(
', '.join(
-
random.sample(WORDS, param_count)))
-
-
for sentence
in snippet, phrase:
-
result = sentence[:]
-
-
#fake class names
-
for word
in class_names:
-
result = result.replace(
"%%%", word,
1)
-
-
#fake other names
-
for word
in other_names:
-
result = result.replace(
"***", word,
1)
-
-
#fake parameter lists
-
for word
in param_names:
-
result = result.replace(
"@@@", word,
1)
-
-
results.append(result)
-
-
return results
-
-
-
# keep going until they hit CTRL-D
-
try:
-
while
True:
-
snippets = list(PHRASES.keys())
-
random.shuffle(snippets)
-
-
for snippet
in snippets:
-
phrase = PHRASES[snippet]
-
question, answer = convert(snippet, phrase)
-
if PHRASES_FIRST:
-
question, answer = answer, question
-
-
print(question)
-
-
input(
"> ")
-
print(
f"ANSWER: {answer}\n\n")
-
except EOFError:
-
print(
"\nBye")
-
运行这段代码,然后把面向对象的专属词汇翻译为日常语言就行,也可以反向练习。
END!!!