📂
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
  • OOP
  • Classes
  • Object Construction
  • method
  • Attributes(属性)
  • Methods 和 Functions
  • Class Attributes

Was this helpful?

  1. CS 61A

OOP

OOP

  • 组织模块化编程的方法

    • 数据抽象

    • 将信息和相关行为捆绑在一起

  • 一种对使用分布式状态的计算的隐喻

    • 每个对象都有自己的本地状态

    • 每个对象也知道如何管理自己的本地状态(基于方法调用)

  • 方法调用 是 对象之间传递 的 信息

  • 几个对象可能都是一个共同类型的实例。

  • 不同的类型可能相互关联

Classes

  • 一个类作为它的实例的模板。

    class <name>:
      <suite> # 当类语句执行时,该 suite 被执行。
  • 当类语句执行时, suite 被执行。

  • 类声明创建了一个新的类,并将该类绑定到当前环境的第一个框架中的<name>。

  • <suite>中的Assignment & def语句创建了类的属性(而不是框架中的名称)。

    class Clown:
      """An illustration of a class statement. This class is not useful.
    
      >>> Clown.nose # 被立即执行了
      'big and red'
      >>> Clown.dance()
      'No thanks'
      """
      nose = 'big and red'
      def dance():
          return 'No thanks'

Object Construction

当一个 class 被调用

  • 该 class 的一个新实例被创建 (self 绑定为这个 instance)

  • 类的init方法被调用,新对象作为它的第一个参数(命名为self),以及在调用表达式中提供的任何附加参数。

    class Account:
      """An account has a balance and a holder.
      All accounts share a common interest rate.
    
      >>> a = Account('John')
      >>> a.holder
      'John'
      >>> a.balance
      10
      >>> a.interest
      0.02
      >>> Account.interest = 0.04
      >>> a.interest
      0.04
      """
    
      interest = 0.02  # A class attribute
    
      def __init__(self, account_holder):  # self 绑定 实例
          self.holder = account_holder     # 实例里有 holder
          self.balance = 0                 # 实例里有 balance

method

  • 所有被调用的方法都可以通过 self 参数访问对象,因此它们都可以访问和操作对象的状态。

  • 点符号自动提供方法的第一个参数。

    class Account:
      """
      >>> a = Account('John')
      >>> a.deposit(100) 
      100
      >>> a.withdraw(90)
      10
      >>> a.withdraw(90)
      'Insufficient funds'
      """
    
      interest = 0.02  # A class attribute
    
      def __init__(self, account_holder):
          self.holder = account_holder
          self.balance = 0
    
      def deposit(self, amount):
          """Add amount to balance."""
          self.balance = self.balance + amount
          return self.balance

Attributes(属性)

  • 使用 getattr(<instance>,<attribute(string)>) 获得对应属性的值

  • 使用 hasattr(<instance>,<attribute(string)>) 判断对应属性是否存在

  • 在一个对象中查找一个属性名可能会返回。

    • 对象的一个实例属性

    • 一个类属性

Methods 和 Functions

  • 绑定方法(Bound methods),它将函数和将被调用的对象结合在一起。

    • Object + Function = Bound Method

      >>> type(Account.deposit)
      <class 'function'>
      >>> type(tom_account.deposit)
      <class 'method'>

Class Attributes

  • 类的属性在类的所有实例中是 "共享 "的,因为它们是类的属性,而不是实例的属性。

  • 类的属性没有 copy 到实例中

  • 它改变了所有实例的类属性都改变了

PreviousData AbstractionNextInheritance

Last updated 4 years ago

Was this helpful?