CS61A 18sp -- Lecture16 (Inheritance) 笔记

Lecture16 Inheritance

一. Attributes

  1. Looking Up Attributes by Name <expression> . <name>

To evaluate a dot expression:

  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 not, 《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
  1. Class Attributes

Class attributes are “shared” across all instances of a class because they are attributes of the class, not the instance

在这里插入图片描述

二、Attribute Assignment

• If the object is aninstance, then assignment sets an instance attribute
• If the object is a class, then assignment sets a class attribute

举例:
在这里插入图片描述
在这里插入图片描述

三、Inheritance

  1. Looking Up Attribute Names on Classes
    在这里插入图片描述
    规则:

To look up a name in a class:
<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.

四、Object-Oriented Design

  1. Designing for Inheritance
    在这里插入图片描述

  2. Inheritance and Composition

Inheritance is best for representing is-a relationships
• E.g., a checking account is a specific type of account
• So, CheckingAccount inherits from Account
Composition is best for representing has-a relationships
• E.g., a bank has a collection of bank accounts it manages
• So, A bank has a list of accounts as an attribute

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值