cs61a lecturre18 Objects

本文概述了面向对象编程中的关键概念,包括类和对象的关系、方法组织、类声明、对象身份、方法调用、点表示法以及属性的访问。特别强调了新实例化、__init__方法、对象绑定和方法与函数的绑定等重要特性。

1.classes&objects

a class combines (and abstracts)data and functions,serves as a template for its insataances.

an object is an instantiation of aclass

    e.g. string is a buit-in class, append is a function

            int is a built in class, + is a function

2.object-oriented programming

a method for organizing modular programs

    absraction barriers

    bundling together information and related behavior

a metaphor for computatiion using distributed state

    each object has its own local state

    each object also knows how to manage its own local state, based on method calls

    method calls are messaages passed between objects

    several objects may all be instances of a common type.

    different types may relate to each other

specialized syntax &vocabulary to support this metaphor

3.the class statement

a class statement creates anew class and binds that class to <name> in the first frame of the current environment.

aissignment & def statements in <suite.> create attributes of the class (not names in frames)

when a class is called:

    1. a new instance of that class is created:

    2. The __init__ method of the class is called with the new object as its first argument (named self).along with any additional arguments provided in the call expression.

4.object identity

every object that is an instance of a user-defined clas has a unique identity:

identity operators "is" and "is not" test if two expressions evaluate to the same object

binding an object to a new name using assignment does not create a new object

e.g.

>>> list1 = [1, 2, 3]
>>> list2 = list1
>>> list2.append(9)
>>> list2
[1, 2, 3, 9]
>>> list1
[1, 2, 3, 9]

but

>>>a = 1
>>>b = a
>>>c = 1
>>>a is c 
True
>>>a = 2
>>>a is b
False

5.methods

methods are defined in th esuite of a class statement

6.invoking methods

7.dot expressions

objects receive messages via dot notation

dot notation accesses attributes of the instance or its class.

8.Attributes  

getattr() : the same as dot expressions

9.Methods and functions

Object + Function = Bound Method

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值