【Python基礎】lpthw - Exercise 41 學習面向對象術語

  1、專有詞彙python

  • 類(class):告訴python建立新類型的東西。
  • 對象(object):兩個意思,即最基本的東西,或者某樣東西的實例。
  • 實例(instance):讓python建立一個類時獲得的東西。
  • def:在類中定義函數。
  • self:在類的函數中,self指代被訪問的對象或實例的一個變量。
  • 繼承(inheritance):指一個類能夠繼承另外一個類的特性,和父子關係相似。
  • 組合(composition):指一個類能夠將別的類做爲它的部件構建起來。
  • 屬性(attribute):類的一個屬性,它來自於組合,並且一般是一個變量。
  • 是什麼(is-a):用來描述繼承關係。
  • 有什麼(has-a):用來描述某個東西是由另外一些東西組成的,或者某個東西有某個特徵。

  2、措辭練習web

  • class X(Y):建立一個叫X的類,它是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

  3、練習用代碼api

 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     PHRASES_FIRST = True
26 else:
27     PHRASES_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         param_names.append(', '.join(
43             random.sample(WORDS, param_count)))
44 
45     for sentence in snippet, phrase:
46         result = sentence[:]
47 
48         # fake class class_names
49         for word in class_names:
50             result = result.replace("%%%", word, 1)
51 
52         # fake other class_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 
62     return results
63 
64 # keep going until they hit CTRL+D
65 try:
66     while True:
67         snippets = list(PHRASES.keys())
68         random.shuffle(snippets)
69 
70         for snippet in snippets:
71             phrase = PHRASES[snippet]
72             question, answer = convert(snippet, phrase)
73             if PHRASES_FIRST:
74                 question, answer = answer, question
75 
76             print(question)
77 
78             input("> ")
79             print(f"ANSWER: {answer}\n\n")
80 except EOFError:
81     print("\nBye.")
View Code

  4、閱讀更多代碼app

  找到更多的代碼,用前述措辭閱讀,找到全部帶類的文件,而後完成下列步驟:dom

  1. 針對每個類,指出它的名稱,以及它是繼承於哪些類的。ide

  2. 列出每一個類中的全部函數,以及這些函數的參數。函數

  3. 列出類中用在self上的全部屬性。url

  4. 針對每個屬性,指出它是來自哪一個類。spa

相關文章
相關標籤/搜索