Programming Languages Dan Grossman 2013 Classes and Objects.

Slides:



Advertisements
Similar presentations
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Advertisements

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE341: Programming Languages Lecture 19 Introduction To Ruby; Dynamic OOP; "Duck Typing" Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 21 Late Binding; OOP as a Racket Pattern Dan Grossman Fall 2011.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
CSE341: Programming Languages Introduction To Ruby; Dynamic OOP; "Duck Typing" Alan Borning Autumn 2012 (slides borrowed from Dan Grossman)
Basic Elements of C++ Chapter 2.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Introduction to Ruby CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
The Java Programming Language
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
CSE 341 Programming Languages Introduction to Ruby Zach Tatlock Spring 2014.
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
+ Ruby and other programming Languages Ronald L. Ramos.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Introduction to Ruby (adapted from CSE 341, Dan Grossman) 1.
1 CSC241: Object Oriented Programming Lecture No 02.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Programming Languages Dan Grossman 2013 Everything is an Object.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 15 Macros
Java Primer 1: Types, Classes and Operators
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
Function There are two types of Function User Defined Function
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
CSE341: Programming Languages Lecture 15 Macros
Alan Borning Spring 2018 (slides borrowed from Dan Grossman)
Python Primer 2: Functions and Control Flow
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
Week 3 Object-based Programming: Classes and Objects
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Zach Tatlock Winter 2018.
CSE341: Programming Languages Section 9 Dynamic Dispatch Manually in Racket Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 15 Macros
Let bindings Motivation: Functions without local variables can be poor style and/or really inefficient. Syntax: let b1 b2 ... bn in e end where each bi is.
CSE341: Programming Languages Lecture 15 Macros
Controlling Program Flow
CSE341: Programming Languages Lecture 15 Macros
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 15 Macros
Presentation transcript:

Programming Languages Dan Grossman 2013 Classes and Objects

The rules of class-based OOP In Ruby: 1.All values are references to objects 2.Objects communicate via method calls, also known as messages 3.Each object has its own (private) state 4.Every object is an instance of a class 5.An object’s class determines the object’s behavior –How it handles method calls –Class contains method definitions Java/C#/etc. similar but do not follow (1) (e.g., numbers, null) and allow objects to have non-private state Jan-Mar 20132Dan Grossman, Programming Languages

Defining classes and methods Define a new class called with methods as defined Method returns its last expression –Ruby also has explicit return statement Syntax note: Line breaks often required (else need more syntax), but indentation always only style Jan-Mar 20133Dan Grossman, Programming Languages class Name def method_name1 method_args1 expression1 end def method_name2 method_args2 expression2 end … end

Creating and using an object ClassName.new creates a new object whose class is ClassName e.m evaluates e to an object and then calls its m method –Also known as “sends the m message” –Can also write e.m() Methods can take arguments, called like e.m(e1,…,en) –Parentheses optional in some places, but recommended Jan-Mar 20134Dan Grossman, Programming Languages

Variables Methods can use local variables –Syntax: starts with letter –Scope is method body No declaring them, just assign to them anywhere in method body (!) Variables are mutable, x=e Variables also allowed at “top-level” or in REPL Contents of variables are always references to objects because all values are objects Jan-Mar 20135Dan Grossman, Programming Languages

Self self is a special keyword/variable in Ruby Refers to “the current object” –The object whose method is executing So call another method on “same object” with self.m(…) –Syntactic sugar: can just write m(…) Also can pass/return/store “the whole object” with just self (Same as this in Java/C#/C++) Jan-Mar 20136Dan Grossman, Programming Languages