📂
CS-NoteBook
  • Introduction
  • CS144
    • concise introduction to Internet
      • 1.1 Networked Applications
      • 1.2 The 4 Layer Internet
      • 1.3 IP
      • 1.4 A Day in the Life of a Packet
      • 1.5 Principle: Packet Switching
      • 1.6 Principle:Layering
      • 1.7 Principle: Encapsulation
      • 1.8 Byte order and packet formats
      • 1.9 name and addresses:IPv4
      • 1.10 Longest Prefix Match for Link Layer
      • 1.11 Address Resolution Protocol(ARP)
      • 1.12 Summary
    • Transport Layer
      • 2.1 The TCP Service Model
      • 2.2 UDP service model
      • 2.3 ICMP(Internet Control Message Protocol 互联网报文控制协议)
      • 2.4 The End-to-End Principle
      • 2.5 Error Detection:3 schemes (Checksum,CRC and MAC)
      • 2.6 Finite State Machines(有限状态机)
      • 2.7 Flow Control
      • 2.8 Sliding window
      • 2.9 Retransmission Strategies
      • 2.10 TCP Header
      • 2.11 TCP Setup and Teardown
      • 2.12 Recap
    • Package Switching
      • 3.1 The history of Internet
      • 3.2 What is packet switching
      • [3.3 End-to-end delay and Queueing delay
      • 3.4 Playback Buffer(回放缓存区)
  • CS 61C
    • 1.4 C Memory Mangement, Usage
    • 1.5 Intro to Assembly Language, MIPS Intro
    • 1.5 extra bits operation
  • CS 61B
  • CS 61A
    • Function
    • Names
    • The Art of the Function
    • Control
    • Higher-Order Function
    • Recursive Function
    • List
    • Non-Local Assignment
    • Iterators
    • Objects
    • Data Abstraction
    • OOP
    • Inheritance
    • Representations
    • Decomposition
    • Scheme
    • Exceptions
    • Calculator
    • Interpreters
    • Declarative_Programming
    • Table
    • Aggregation
      • More_recursion
    • Databases
    • Distributed_Data
    • Tail Recursion
    • Exercises
      • lab00
      • lab01
      • hw01
      • tree Recursion example -- give Change
  • The Web DevelopMent Bootcamp
    • html5
    • css
    • bootstrap3
    • bootstrap4
    • javascript expression
    • javascript function
Powered by GitBook
On this page
  • Python 的 对象 系统
  • 继承
  • 在 classes 中寻找属性
  • 面向对象的设计
  • 继承和构成
  • 多重继承

Was this helpful?

  1. CS 61A

Inheritance

Python 的 对象 系统

  • Functions 是 对象

  • Bonds methods 也是对象(第一个参数 self 已经 bonds to an instance 的方法)

  • 点式表达式求到类属性的 Bonds methods 是 functions。

  • <instance>.<method_name>

    • 左边生成一个 object

    • 右边在 instance 属性内部找 name

    • 如果没有,找类属性

    • 该值将返回,除非它是一个函数(会返回绑定方法)

继承

  • 继承是一种将类联系在一起的技术

  • 一种常见的用法。两个类似的 classes 在 specialization 上的程度有差异。

  • specialized class 可以具有与一般类相同的属性,以及一些特殊情况下的行为。

    class <Name>(<Base Class>):
        <suite>
  • 概念上,新的 subclass 继承了基类的属性。

  • 子类可以 override 某些继承的属性

  • 利用继承,我们通过指定 subclass 与 base class 的不同之处来实现 subclass。

在 classes 中寻找属性

  • base classes 没有被复制到 subclass

  • 在 class 中查看名字:

    • 如果它命名了 class 中的一个属性,返回这个 attribute value

    • 如果没有,去 base class 中找

class CheckingAccount(Account):
    """A bank account that charges for withdrawals."""
    withdraw_fee = 1
    interest = 0.01
    def withdraw(self, amount):
        return Account.withdraw(self, amount + self.withdraw_fee)

>>> ch = CheckingAccount('Tom') # Calls Account.__init__
>>> ch.interest                 # Found in CheckingAccount
0.01
>>> ch.deposit(20)              # Found in Account
20
>>> ch.withdraw(5)              # Found in CheckingAccount
14

面向对象的设计

  • 不要重复自己,使用已经存在的实现方式

  • 已被 override 的属性仍可通过 class 对象访问。

  • 尽可能在实例上查找属性 (这是个好主意)

继承和构成

  • 继承最好用在 is-a 的关系上

    • 例如,支票账户是一种特殊的账户类型。

    • 因此,CheckingAccount继承自Account

  • 构成最适合表示 has-a 关系

    • 例如,一家银行管理着一系列的银行账户。

    • 所以,A银行有一个账户列表作为属性

多重继承

  • 子类有多个基类

    class SubclassName(BaseClass1, BaseClass2, BaseClass3, ...):

  • 先调用 subclass ,在调用 baseclass

  • 多重继承会使得程序变得很复杂,因此应该极少使用

PreviousOOPNextRepresentations

Last updated 4 years ago

Was this helpful?