南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)14-Object-Oriented Programming

Object-oriented Programming
Object-Oriented Programming

Why OOP?
Why OOP?

Object-Oriented Programming A(hopefully)more intuitive way of representing data A common method for organizing programs Formally split"global state"and "local state"for every object
Object-Oriented Programming ● A (hopefully) more intuitive way of representing data ● A common method for organizing programs ● Formally split "global state" and "local state" for every object

Classes Every object is an instance of a class A class is a type or category of objects A class serves as a blueprint for its instances Instance Tiffany:is a Human Class Tiffany has a :name and an:age Attributes Tiffany can:talk:to other Humans Behaviors
Classes • Every object is an instance of a class • A class is a type or category of objects • A class serves as a blueprint for its instances Tiffany is a Human Tiffany has a name and an age Instance Class Attributes Tiffany can talk to other Humans Behaviors

Example:Bank Accounts A class serves as a blueprint for its instances Tiff's Idea:All bank accounts have a balance Account and an account holder; the Account class should define those values for each newly created instance. Withdraw S5 Idea:All bank accounts should have Transfer $5 "withdraw"and "deposit"behaviors that all work in the same way. Alex's Better Idea:All bank accounts share a Account "withdraw"and "deposit"method. Deposit $5 All accounts might also share other characteristics like maximum withdrawal, or loan limits
Example: Bank Accounts Tiff’s Account Alex's Account Deposit $5 Withdraw $5 Transfer $5 A class serves as a blueprint for its instances Idea: All bank accounts have a balance and an account holder; the Account class should define those values for each newly created instance. Idea: All bank accounts should have “withdraw” and “deposit” behaviors that all work in the same way. Better Idea: All bank accounts share a “withdraw” and “deposit” method. All accounts might also share other characteristics like maximum withdrawal, or loan limits

The Class Statement Class Statements defines the blueprint Assignment def statements in the Class Statement create class class : attributes. Lots of...don't worry! Global frame Account class Account: Tables are not frames max_withdrawal 10 Account class def deposit(...): max_withdrawal 10 func deposit(...) 年 [p=G] def withdraw(.·.) deposit Class attribute func withdraw withdraw(...) [p=G] Bounds methods
The Class Statement class Account: max_withdrawal = 10 def deposit(...): ... def withdraw(...): ... ... class : Class Statements defines the blueprint Assignment & def statements in the Class Statement create class attributes. Global frame Account Account class func withdraw(...) [p=G] func deposit(...) [p=G] Tables are not frames Lots of … don’t worry! max_withdrawal 10 deposit withdraw Class attribute Bounds methods

The Class Statement Before we start implementing our methods,we need to talk about how to create Accounts. Idea:All bank accounts have a balance and an account holder.These are not shared across Accounts. When a class is called: 1.A new instance of that class is created. 2.The__init_method of a class is called with the new object as its first argument(named self),along with additional arguments provided in the call expression. _init_is called class Account: the constructor ··。 def __init__(self,account_holder): self.balance 0 self.holder account_holder
The Class Statement Before we start implementing our methods, we need to talk about how to create Accounts. Idea: All bank accounts have a balance and an account holder. These are not shared across Accounts. When a class is called: 1. A new instance of that class is created. 2. The __init__ method of a class is called with the new object as its first argument (named self), along with additional arguments provided in the call expression. class Account: ... def __init__(self, account_holder): self.balance = 0 self.holder = account_holder ... __init__ is called the constructor

The Class Statement Global frame Account instance class Account: Account balance 0 max_withdrawal 10 holder “Tiff" def __init__(self,account_holder): self.balance =0 Instance self.holder account_holder attributes Account class def deposit(self,amount): max_withdrawal 10 func __init__(..\) Class attribute __init__ [p=G] def withdraw(self,amount): deposit func deposit(...) withdraw [p=G] >>a Account('Tiff') func withdraw(...) f1:init [p=G] [p=G] Python Tutor link self account_holder "Tiff" RV None
The Class Statement Global frame Account Account class func deposit(...) [p=G] func __init__(...) [p=G] func withdraw(...) [p=G] Account instance f1: __init__ [p=G] self account_holder RV "Tiff" None a max_withdrawal 10 __init__ deposit withdraw balance 0 holder “Tiff” class Account: max_withdrawal = 10 def __init__(self, account_holder): self.balance = 0 self.holder = account_holder def deposit(self, amount): ... def withdraw(self, amount): ... >>> a = Account('Tiff') Instance attributes Class attribute Python Tutor link

Terminology:Attributes,Functions,and Methods All objects have attributes,which are name-value pairs Classes are objects too,so they have attributes Instance attribute:attribute of an instance Class attribute:attribute of the class of an instance
Terminology: Attributes, Functions, and Methods All objects have attributes, which are name-value pairs Classes are objects too, so they have attributes Instance attribute: attribute of an instance Class attribute: attribute of the class of an instance

Object Identity Python Tutor link Every object that is an instance of a user-defined class has a unique identity Identity operators "is"and "is not"test if two expressions evaluate to the same object(the arrows point to the same place) Binding an object to a new name using assignment does not create a new object
Object Identity ● Every object that is an instance of a user-defined class has a unique identity ● Identity operators “is” and “is not” test if two expressions evaluate to the same object (the arrows point to the same place) ● Binding an object to a new name using assignment does not create a new object Python Tutor link
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators & Generators.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)12-Mutable Functions & Growth.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)11-Mutable-Values.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)10-Trees.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)09-Data-Abstractions.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)08-Containers.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)07-Recursion Examples.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)06-Recursion.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)05-Higher-Order Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)04-Environment Diagrams.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)03-Control.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)02-Names & Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)01-Introduction(主讲:冯新宇).pptx
- 《计算机科学》相关教学资源(PPT课件讲稿)Modular Verification of Concurrent Assembly Code with Dynamic Thread Creation and Termination.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)Modular Verification of Assembly Code with Stack-Based Control Abstractions.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)An Open Framework for Foundational Proof-Carrying Code.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)Certifying Low-Level Programs with Hardware Interrupts and Preemptive Threads.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)On the Relationship between Concurrent Separation Logic and Assume-Guarantee Reasoning.ppt
- 《计算机科学》相关教学资源(参考文献)Technical Report TTIC-TR-2008-1(Local Rely-Guarantee Reasoning).pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)15-Inheritance.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)16-Linked Lists & Mutable Trees.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)17-Interfaces.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)18-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)19-More-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)20-Interpreters.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)21-Macros.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)22-Streams.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)23-SQL-I.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)24-SQL-II.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)25-Conclusion, and Final Exam Review.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)01 绪论 Introduction(主讲:曹洋).pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)02 傅里叶分析与卷积 Fourier Analysis and Convolution.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)03 数字图像处理基础 Basics of Digital Image Processing.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)04 图像模型 Basics of Image.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)05 空域滤波 Spatial Filtering.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)06 小波变换 Wavelet Analysis.pptx
- CodeIgniter 中国开发者社区:CodeIgniter4 中文手册(版本 4.0.0).pdf
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)07 图像复原 Image Restoration.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)08 自适应滤波 Adaptive Filter.pptx