6. Classes & Objects Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
OBJECT ORIENTED PROGRAMMING (OOP) IN PYTHON David Moodie.
INFO 206 Lab Exercise 1 Introduction to Classes and Objects 1/18/2012i206 Lab 1 - Exercise1.
Guide to Programming with Python
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
I210 review (for final exam) Fall 2011, IUB. What’s in the Final Exam Multiple Choice (5) Short Answer (5) Program Completion (3) Note: A single-sided.
Inheritance. Inhertance Inheritance is used to indicate that one class will get most or all of its features from a parent class. class Dog(Pet): Make.
1 Survey of Computer Science CSCI 110, Spring 2011 Lecture 14 Recursion.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Python Programming in Context
Classes 1 COMPSCI 105 S Principles of Computer Science.
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
1. Starting 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Let’s Learn 3. Modules Saenthong School, January – February 2016
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
8. Functions 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
4. If Statements 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Object-Oriented Programming (OOP) in Python References: Chapter 8.
CSC 231: Introduction to Data Structures Python and Objects – Day 3 Dr. Curry Guinn.
CMSC201 Computer Science I for Majors Lecture 18 – Classes and Modules (Continued, Part 3) Prof. Katherine Gibson Based on slides from the.
4. If Statements 1 Have your program make choices. Depending on a test have a program do different things Computer Programming BSc in Digital.
Adapted from slides by Marty Stepp and Stuart Reges
Let’s Learn 12. Packaging a Game Saengthong School, June – August 2016
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Python programming - Defining Classes
Guide to Programming with Python
Adapted from slides by Marty Stepp and Stuart Reges
Object-Oriented Programming (OOP) in Python
CSSE 120—Rose Hulman Institute of Technology
CS-104 Final Exam Review Victor Norman.
12. Classes & Objects Let's Learn Python and Pygame
Lecture VI Objects The OOP Concept Defining Classes Methods
4. If Statements Let's Learn Python and Pygame
Object Oriented Programming in Python
Engineering Computing
Object-oriented programming
Creating and Deleting Instances Access to Attributes and Methods
What are variables? Using input()
Writing Functions( ) (Part 5)
Let's Learn Python and Pygame
Guide to Programming with Python
Teach A-Level Computer Science: Object-Oriented Programming in Python
Python Classes Python has Classes, Methods, Overloaded operators, as well as Inheritance. Its somewhat 'loose' in the since it does not have true encapsulation,
6. Lists Let's Learn Python and Pygame
Lesson Objectives Aims Key Words:
Week 8 Classes and Objects
BSc in Digital Media, PSUIC
Object Oriented Programming in Python
7. Functions Computer Programming BSc in Digital Media, PSUIC
12. Classes & Objects a class factory makes objects + functions
7. Functions Let's Learn Python and Pygame
Basic Inheritance Damian Gordon.
Classes, Objects and lists
An Introduction to Object Orientated Programming
What are variables? Using input()
CSE 231 Lab 11.
Prof. Katherine Gibson Prof. Jeremy Dixon
Lecture 18 Python OOP.
A Level Computer Science Topic 6: Introducing OOP
What are variables? Using input()
Introduction to Object-Oriented Programming (OOP)
Migrating to Object-Oriented Programs
Introduction to Object-Oriented Programming (OOP) II
Presentation transcript:

6. Classes & Objects Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus makes objects functions library factory + a class

Outline 1.What is an Object? 2.What is a Class? 3.Car Class 4.Person Class 5.Inheritance 6.Inheritance in Python 7.OOP 2

 An object groups together all the data related to one thing.  e.g. each Person object might contain name and birthday data for a person 1. What is an Object? 3 name birth day p1 Person object "Alice" 5/6/2001 name p2 Person object "John" name p3 Person object "Bill" the p1, p2, p3 Person objects birth day birth day 3/2/2002 2/2/2002

 A class is two things:  a factory for making objects  a library of functions that objects can use  A Car class: 2. What is a Class? 4 makes Car objects functions library factory +

Possible Car Objects 5 dist type car1 Car object 2 "ferrarri" color "red" dist type car1 Car object 5 "porsche" color "green" dist type car1 Car object "bean" color "yellow" could be lots of other things in a real Car object

class Car: def __init__(self, dist, type, color): # factory method (constructor) self.dist = dist self.type = type self.color = color def __str__(self): # returns a string version of the object return "(" + str(self.dist) + ", " + self.type + ", " + self.color + ")" def drive(self, miles): if miles < 0: print("Drive distance cannot be negative") else: self.dist += miles def setType(self, type): if type in ["ferrari", "porsche", "bean", "toyota"]: self.type = type else: print(type, "not supported") def setColor(self, color): self.color = color def getColor(self): return self.color 3.Car Class (car.py) 6 will create an object with 3 pieces of data class contains 6 functions (including the constructor)

# create 3 Car objects car1 = Car(2, "ferrari", "red") # uses __init__() car2 = Car(5, "porsche", "green") car3 = Car(10000, "bean", "yellow") car1.drive(100) print("Car1:", car1) # uses __str__() print("Car1's type:", car1.type) print("1. Car1's color:", car1.getColor()) print("2. Car1's color:", car1.color) print() car3.setColor("blue") car3.drive(-9999) print("Car3: ", car3) Test Code 7

 self is the first argument of every function definition  tells the function which object's data to use  self is not included when a function is called  Instead Python puts the name before the ".":  car1.getColor() means call getColor() with car1 's data What is self? 8

 Directly: car1.color  With a function: car1.getColor()  Using a function means that the data can be changed before being returned.  e.g. def getColor(): return self.color.upper() Two Ways to Access (get) Data 9

 Directly: car1.dist = -1  With a function: car1.drive(9999)  Using a function means that the data can be protected from "bad" changes.  e.g. def drive(self, miles): if miles < 0: print("Drive distance cannot be negative") else: self.dist += miles Two ways to Change (set) Data 10 Using a function is better programming style.

import datetime class Person(): def __init__(self, name): self.name = name self.birthday = None def __str__(self): return self.name + ": " + str(self.birthday) def getName(self): return self.name def setBirthday(self, day, month, year): self.birthday = datetime.date(year, month, day) def getAge(self): # returns self's current age in days if self.birthday == None: print("No birthday information") return 0 else: return (datetime.date.today() – self.birthday).days 4. Person Class 11 will create an object with 2 pieces of data class contains 5 functions (including the constructor)

# create two Person objects me = Person("Andrew Davison") print(me) print("My name:", me.getName()) me.setBirthday(23, 7, 1962) print(me) tom = Person("Tom Cruise") tom.setBirthday(3, 7, 1962) print(tom) people = [me, tom] for p in people: print(" ", p, "; today:", p.getAge()) Test Code 12

 "a" inherits from "b" when "a" is like "b" but with extra features (and maybe a few changes).  "a" is a specialized version of "b" 5. Inheritance 13 animal mammalinsect bearcatdogantcockroachmosquito inherits

14 vehicle cartruck sedanvanSUVpickupsemitow bus inherits

 Create a new class by inheriting an existing class  no need to start programming from nothing e.g. Student class inherits Person class  The Student class can add extra data and functions to the ones inherited from Person. 6. Inheritance in Python 15

class Student( Person ): def __init__(self, name, id): super().__init__(name) # initialize Person data self.id = id def __str__(self): return super().__str__() + " (" + str(self.id )+ ")" # return all data def setId(self, id): self.id = id Student Inherits Person 16 will create an object with 3 pieces of data class contains 8 functions (including the 2 constructor s ) super() means use the function from the inherited class

 A Person object has 3 pieces of data:  name and birthday inherited from Student  id from Person  The Person class contains 8 functions:  5 functions inherited from Student  3 functions from Person  there are two __init() and __str__() functions  use super() to call the inherited ones How much data & functions? 17

# test code s1 = Student('Alice', 10023) s1.setBirthday(5, 6, 2001) s2 = Student('John', 10015) s2.setBirthday(3, 2, 2002) s3 = Student('Bill', 10029) s3.setBirthday(2, 2, 2002) print(s1) print("Student 2:", s2.name, ",", s2.id) print() students = [s1,s2,s3] for s in students: print(" ", s, "; today:", s.getAge()) Test Code 18 name birth day s1 Student object "Alice" 5/6/2001 id can call getAge() can use name directly

 OOP stands for Object Oriented Programming  It means that the programming language has the features:  classes and objects  inheritance  polymorphism  this can mean a few different things. e.g.  a data structure (e.g. a list) can contain different kinds of objects  the same function can work on different kinds of objects  Python is an OOP language  Other OOP languages are Java and C++ 7. OOP 19