Classes and Objects; Inheritance

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

Week 2 Classes, Objects and lists review Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Classes Special thanks to Roy McElmurry, Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Unit 8 Classes and Objects; Inheritance Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
Python Programming Chapter 14: Classes and Methods Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Classes 1 COMPSCI 105 S Principles of Computer Science.
Unit 2 Expressions and variables; for loops Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
1 Object state: fields. Fields field: A variable inside an object that represents part of its internal state.  Each object will have its own copy of.
CSC 142 Computer Science II Zhen Jiang West Chester University
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Adapted from slides by Marty Stepp and Stuart Reges
COMPSCI 107 Computer Science Fundamentals
Object Oriented Programming
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Classes and Objects – digging deeper
COMPSCI 107 Computer Science Fundamentals
Week 8 Classes and Objects
CSC240 Computer Science III
Overloading and Constructors
Classes and Objects, State and Behavior
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
CSc 110, Autumn 2016 Lecture 31: Encapsulation
Object initialization: constructors
Building Java Programs
Programmazione ad alto livello con Python
Week 8 Classes and Objects
Adapted from slides by Marty Stepp and Stuart Reges
Classes Special thanks to Roy McElmurry, Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
expressions, variables, for loops
Building Java Programs
expressions, variables, for loops
while loops; logic; random numbers; tuples
Built-In Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work.
Building Java Programs
Classes, Objects and lists
Classes and objects Readings: 8.1.
CSE 142 Lecture Notes Defining New Types of Objects
Building Java Programs
Building Java Programs
Handout-4b More on Classes
Building Java Programs
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Terry Scott University of Northern Colorado 2007 Prentice Hall
Building Java Programs
Classes and objects Readings: 8.1.
Building Java Programs
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Building Java Programs
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Building Java Programs
Week 2 Classes and Objects
Presentation transcript:

Classes and Objects; Inheritance Unit 8 Classes and Objects; Inheritance Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0

OOP, Defining a Class Python was built as a procedural language OOP exists and works fine, but feels a bit more "tacked on" Java probably does classes better than Python (gasp) Declaring a class: class name: statements 2

Fields name = value Example: class Point: x = 0 y = 0 # main p1 = Point() p1.x = 2 p1.y = -5 can be declared directly inside class (as shown here) or in constructors (more common) Python does not really have encapsulation or private fields relies on caller to "be nice" and not mess with objects' contents point.py 1 2 3 class Point: x = 0 y = 0 3

Using a Class import class client programs must import the classes they use point_main.py 1 2 3 4 5 6 7 8 9 10 from Point import * # main p1 = Point() p1.x = 7 p1.y = -3 ... # Python objects are dynamic (can add fields any time!) p1.name = "Tyler Durden"

Object Methods def name(self, parameter, ..., parameter): statements self must be the first parameter to any object method represents the "implicit parameter" (this in Java) must access the object's fields through the self reference class Point: def translate(self, dx, dy): self.x += dx self.y += dy ... 5

"Implicit" Parameter (self) Java: this, implicit public void translate(int dx, int dy) { x += dx; // this.x += dx; y += dy; // this.y += dy; } Python: self, explicit def translate(self, dx, dy): self.x += dx self.y += dy Exercise: Write distance, set_location, and distance_from_origin methods. 6

Exercise Answer point.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 from math import * class Point: x = 0 y = 0 def set_location(self, x, y): self.x = x self.y = y def distance_from_origin(self): return sqrt(self.x * self.x + self.y * self.y) def distance(self, other): dx = self.x - other.x dy = self.y - other.y return sqrt(dx * dx + dy * dy)

Calling Methods A client can call the methods of an object in two ways: (the value of self can be an implicit or explicit parameter) 1) object.method(parameters) or 2) Class.method(object, parameters) Example: p = Point(3, -4) p.translate(1, 5) Point.translate(p, 1, 5)

Constructors def __init__(self, parameter, ..., parameter): statements a constructor is a special method with the name __init__ Example: class Point: def __init__(self, x, y): self.x = x self.y = y ... How would we make it possible to construct a Point() with no parameters to get (0, 0)? 9

toString and __str__ def __str__(self): return string equivalent to Java's toString (converts object to a string) invoked automatically when str or print is called Exercise: Write a __str__ method for Point objects that returns strings like "(3, -14)" return "(" + str(self.x) + ", " + str(self.y) + ")"

Complete Point Class point.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 from math import * class Point: def __init__(self, x, y): self.x = x self.y = y def distance_from_origin(self): return sqrt(self.x * self.x + self.y * self.y) def distance(self, other): dx = self.x - other.x dy = self.y - other.y return sqrt(dx * dx + dy * dy) def translate(self, dx, dy): self.x += dx self.y += dy def __str__(self): return "(" + str(self.x) + ", " + str(self.y) + ")"

__truediv__(self, other) Operator Overloading operator overloading: You can define functions so that Python's built-in operators can be used with your class. See also: http://docs.python.org/ref/customization.html Operator Class Method - __neg__(self, other) + __pos__(self, other) * __mul__(self, other) / __truediv__(self, other) Operator Class Method == __eq__(self, other) != __ne__(self, other) < __lt__(self, other) > __gt__(self, other) <= __le__(self, other) >= __ge__(self, other) Unary Operators - __neg__(self) + __pos__(self)

Exercise Exercise: Write a Fraction class to represent rational numbers like 1/2 and -3/8. Fractions should always be stored in reduced form; for example, store 4/12 as 1/3 and 6/-9 as -2/3. Hint: A GCD (greatest common divisor) function may help. Define add and multiply methods that accept another Fraction as a parameter and modify the existing Fraction by adding/multiplying it by that parameter. Define +, *, ==, and < operators.

Generating Exceptions raise ExceptionType("message") useful when the client uses your object improperly types: ArithmeticError, AssertionError, IndexError, NameError, SyntaxError, TypeError, ValueError Example: class BankAccount: ... def deposit(self, amount): if amount < 0: raise ValueError("negative amount") 14

Inheritance class name(superclass): statements Example: class Point3D(Point): # Point3D extends Point z = 0 ... Python also supports multiple inheritance class name(superclass, ..., superclass): (if > 1 superclass has the same field/method, conflicts are resolved in left-to-right order) 15

Calling Superclass Methods methods: class.method(object, parameters) constructors: class.__init__(parameters) class Point3D(Point): z = 0 def __init__(self, x, y, z): Point.__init__(self, x, y) self.z = z def translate(self, dx, dy, dz): Point.translate(self, dx, dy) self.z += dz 16