cs61a spring 类与继承笔记

Object-Oriented Programming

1.Defining Classes

class <name>:
    <suite>

When a class statement is executed, a new class is created and bound to in the first frame of the current environment. The suite is then executed. Any names bound within the of a class statement, through def or assignment statements, create or modify attributes of the class.

(1)constructor

When the class statement is executed, the method init will first called to initializes objects.

>>> class Account:
        def __init__(self, account_holder):
            self.balance = 0
            self.holder = account_holder

The init method for Account has two formal parameters. The first one, self, is bound to the newly created Account object. The second parameter, account_holder, is bound to the argument passed to the class when it is called to be instantiated.

(2)instantiate

>>> a = Account('Kirk')

(3)Identity

every object that is an instance of a user-defined class has a unique identity. Object identity is compared using the is and is not operators.

(4)Methods

Object methods are also defined by a def statement in the suite of a class statement.

>>> class Account:
        def __init__(self, account_holder):
            self.balance = 0
            self.holder = account_holder
        def deposit(self, amount):
            self.balance = self.balance + amount
            return self.balance
        def withdraw(self, amount):
            if amount > self.balance:
                return 'Insufficient funds'
            self.balance = self.balance - amount
            return self.balance

2.Message Passing and Dot Expressions

Methods, which are defined in classes, and instance attributes, which are typically assigned in constructors, are the fundamental elements of object-oriented programming.

(1) Dot expressions

<expression> . <name>

(2) Methods and functions

When a method is invoked on an object, that object is implicitly passed as the first argument to the method. That is, the object that is the value of the to the left of the dot is passed automatically as the first argument to the method named on the right side of the dot expression. As a result, the object is bound to the parameter self.

the different between method and function:

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

These two results differ only in the fact that the first is a standard two-argument function with parameters self and amount. The second is a one-argument method, where the name self will be bound to the object named spock_account automatically when the method is called, while the parameter amount will be bound to the argument passed to the method.

>>> Account.deposit(spock_account, 1001)  # The deposit function takes 2 arguments
1011
>>> spock_account.deposit(1000)           # The deposit method takes 1 argument
2011

(3) Naming Conventions

Class names: using the CapWords convention
Method names : using lowercased words separated by underscores.

3.Class Attributes

Class attributes are created by assignment statements in the suite of a class statement, outside of any method definition.
The following class statement creates a class attribute for Account with the name interest:

>>> class Account:
        interest = 0.02            # A class attribute
        def __init__(self, account_holder):
            self.balance = 0
            self.holder = account_holder
        # Additional methods would be defined here

(1) Attribute names

To evaluate a dot expression: <expression> . <name>

  1. Evaluate the <expression> to the left of the dot, which yields the object of the dot expression.
  2. <name> is matched against the instance attributes of that object; if an attribute with that name exists, its value is returned.
  3. If <name> does not appear among instance attributes, then <name> is looked up in the class, which yields a class attribute value.
  4. That value is returned unless it is a function, in which case a bound method is returned instead

(2) Attribute assignment

All assignment statements that contain a dot expression on their left-hand side affect attributes for the object of that dot expression.

>>> kirk_account.interest = 0.08
>>>> spock_account.interest
0.04
>>> Account.interest = 0.05  # changing the class attribute
>>> spock_account.interest     # changes instances without like-named instance attributes
0.05
>>> kirk_account.interest     # but the existing instance attribute is unaffected
0.08

4.Inheritance

When Python resolves a name in a dot expression:

  1. If it names an attribute in the class, return the attribute value.
  2. Otherwise, look up the name in the base class, if there is one.
>>> class Account:
        """A bank account that has a non-negative balance."""
        interest = 0.02
        def __init__(self, account_holder):
            self.balance = 0
            self.holder = account_holder
        def deposit(self, amount):
            """Increase the account balance by amount and return the new balance."""
            self.balance = self.balance + amount
            return self.balance
        def withdraw(self, amount):
            """Decrease the account balance by amount and return the new balance."""
            if amount > self.balance:
                return 'Insufficient funds'
            self.balance = self.balance - amount
            return self.balance

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

>>> checking = CheckingAccount('Sam')
>>> checking.deposit(10)
10
>>> checking.withdraw(5)
4
>>> checking.interest
0.01

example: https://goo.gl/RnvXYx
p1
p2

5. Multiple Inheritance


原文地址:
http://composingprograms.com/pages/25-object-oriented-programming.html

原文地址:https://www.cnblogs.com/siucaan/p/9623165.html