LearnPython3theHardWay__Excercise 41 Learning to Speak Object-Oriented

**ex40- 44都是类的知识 **

我们先接触面向对象的专业词汇


Word Drills
class: 类,告诉python,创建一个新类型的东西
object:对象
instance:实例
def : 定义函数
self: 实例变量
inheritance: 继承
composition: 合成,一个类可以由另一些类组成
attribute: 属性,通常是变量
is-a: 表示某事物继承自另一事物,如“鲑鱼”是“鱼”。
has-a: 表示某物由其他事物组成或具有某种特性,如“鲑鱼有嘴”。

短语练习

  • class X(Y)

    • Make a class named X that is-a Y
  • class X(object): def init(self, J)

    • class X has-a init that takes self and J parameters.
  • class X(object): def M(self, J)

    • class X has-a function named M that takes self and J parameters.
  • foo = X()

    • Set foo to an instance of class X.
  • foo.M(J)

    • From foo, get the M function, and call it with parameters self, J.
  • foo.K = Q

    • From foo, get the K attribute, and set it to Q.

A Reading Test

把下面代码复制下来并运行。英语好可以做一做。用上面的方法描述类。

import random
from urllib.request import 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":
    PHRASES_FIRST = True
else:
    PHRASES_FIRST = False

# load up the words from the website
for word in urlopen(WORD_URL).readlines():
    WORDS.append(str(word.strip(), encoding="utf-8"))


def convert(snippet, phrase):
    class_names = [w.capitalize() for w in
                  random.sample(WORDS, snippet.count("%%%"))]
    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")

Reading more code

去阅读更多的代码,更多类。看类的文档,然后执行以下操作:
1、看每个类的名称,看看其他类有没有继承
2、列出它拥有的每一个函数以及它们所取的参数。
3、列出它的所有属性。
4、对于给出的每个属性,是属于什么类的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值