Classes 1 COMPSCI 105 S2 2015 Principles of Computer Science.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Overview of Data Structures and Algorithms
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
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.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
1 Writing a Good Program 5. Objects and Classes in C++
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:
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Classes 3 COMPSCI 105 S Principles of Computer Science.
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.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction. 2COMPSCI Computer Science Fundamentals.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Chapter 1 Object Orientation: Objects and Classes.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
1 CIS 2168 Data Structures and Algorithms in JAVA Brief Introduction to JAVA.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction to OOP OOP = Object-Oriented Programming OOP is very simple in python but also powerful What is an object? data structure, and functions (methods)
CSC 142 Computer Science II Zhen Jiang West Chester University
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Lecture 04 – Models of memory Mutable and immutable data.
JSON COMPSCI 105 SS 2015 Principles of Computer Science.
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.
Object Oriented Programing (OOP)
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
JSON COMPSCI 105 S Principles of Computer Science.
Lecture 09 – Classes.  At the end of this lecture, students should be able to:  Define a new class  Store state information about instances of the.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
PyGame - Unit 4 PyGame Unit – Object Oriented Programming OOP.
Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science.
Adapted from slides by Marty Stepp and Stuart Reges
CMSC201 Computer Science I for Majors Lecture 25 – Classes
COMPSCI 107 Computer Science Fundamentals
Objects as a programming concept
Object Oriented Programming
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Classes and Objects; Inheritance
Objects as a programming concept
Classes and OOP.
Objects as a programming concept
Yanal Alahmad Java Workshop Yanal Alahmad
Object Oriented Programming
OBJECT ORIENTED CONCEPTS
Object Oriented Programming
Object-Oriented Programming
Object-oriented programming
Adapted from slides by Marty Stepp and Stuart Reges
User Defined Classes CS F.
Teach A-Level Computer Science: Object-Oriented Programming in Python
CS 100: Roadmap to Computing
Week 8 Classes and Objects
Adapted from slides by Marty Stepp and Stuart Reges
Classes, Objects and lists
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Classes 1 COMPSCI 105 S Principles of Computer Science

Exercise  What is the output of the following code fragment? Lecture04COMPSCI 1052 x = ['a', 'b', 'c'] y = x z = ['a', 'b', 'c'] print (x is y) print (x == y) print (x is z) print (x == z) x = ['a', 'b', 'c'] y = x z = ['a', 'b', 'c'] print (x is y) print (x == y) print (x is z) print (x == z)

Lecture04COMPSCI 105 Object Oriented Programming  An object represents an entity in the real world that can be distinctly identified, e.g., students, dogs, cars, cats, books.  Object Oriented Programming (OOP) involves the use of objects to create programs. 3

Objects Lecture04 COMPSCI 105 Cars may have: information: colour, current speed, current gear, etc. function: accelerate, brake, change gear, reverse, etc. color: red speed: 50 gear: 4th color: white speed: 5 gear: 1st Car A Car B Car B – accelerate color: white speed: 10 gear: 1st Car B 4

Lecture04COMPSCI 105 State and Behaviour  Every real world object has:  State – information that the object stores.  Behavior – functionality of the object, i.e., what the object can do.  Example:  Consider a system managing university students.  A student object has:  State – id, name, age, contact number, address, stage, completed courses, current courses, faculty, …  Behavior – enroll in a new course, change contact number, change enrolment, choose degree, … 5

Lecture04COMPSCI 105 Object is state + behaviour  A software object’s state is represented by its variables, called data fields.  A software object implements its behavior with methods.  Every object is a bundle of variables and related methods.  We make an object perform actions by invoking the methods on that object.  Example: my_list = [ x * 3 for x in range(10) if x %2 != 0 ] my_list.reverse() my_list = [ x * 3 for x in range(10) if x %2 != 0 ] my_list.reverse() 6

Lecture04COMPSCI 105 In a Program  Our program consists of many different objects  Two objects of the same kind would have the same set of behaviors, but independent state information  Two string objects store different words, but can perform same methods, e.g., lower(), split(), index(), etc.  For an object in our program  State – is defined by variables (data fields).  Behaviors – is defined by methods (actions).  The definition of a particular kind of objects is called a class. Once created, an object is an instance of a class. 7

Lecture04COMPSCI 105 Python Class  A class is the structure we use to define a category of objects. It defines the state and behaviour of a category of objects.  A class is a template or blueprint defining the date fields and actions (methods) that any instance (object) of that class can have.  Analogies for class and object:  Cookie cutter and cookies.  Factory mold and products produced from that mold. 8

Classes  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes  creates a new type of object in Python  Classes consist of:  state variables (sometimes called instance variables)  methods (functions that are linked to a particular instance of the class) class name_of_the_class: # definition of the class goes here # initializer # methods class name_of_the_class: # definition of the class goes here # initializer # methods Lecture04COMPSCI 1059

Example  An example:  Instantiating Classes  A class is instantiated by calling the class object: class foo: a, b, c = 0, "bar", (1,2) class foo: a, b, c = 0, "bar", (1,2) i = foo() print (i.a) print (i.b) print (i.c) i = foo() print (i.a) print (i.b) print (i.c) 0 bar (1, 2) 0 bar (1, 2) Lecture04COMPSCI 10510

The Simplest Class  Note: “pass” is a statement that does nothing  It is often used as a placeholder when developing code class Point: pass class Point: pass >>> p = Point() >>> p >>> p.x = 2 >>> p.y = 4 >>> p.x 2 >>> p.y 4 >>> p = Point() >>> p >>> p.x = 2 >>> p.y = 4 >>> p.x 2 >>> p.y 4 Lecture04COMPSCI 10511

Saving the Class  Classes are designed to help build modular code  Can be defined within a module that also contains application code  Multiple classes can be defined in the same file  In this course, we will typically store each class in their own module  To use the class in another module, you will need to import the module class Point: # definition goes here class Point: # definition goes here Saved in a file called Geometry.py from Geometry import Point p = Point(5,7) from Geometry import Point p = Point(5,7) Use the class like this p x:5 y:7 The object in memory Lecture04COMPSCI Geometry.py

Setting the Initial State  We want to define the Point class so we can write code that sets the initial values of some variables  First, we need to define a special method of the Point class called a constructor  The constructor is called whenever you create an object of the Point class. from Geometry import Point p = Point(5, 7) from Geometry import Point p = Point(5, 7) Lecture04COMPSCI p x:5 y:7 The object in memory

Constructors  Each class should contain a constructor method  The initializer is always named __init__, which is preceded and followed by two underscore symbols.  The method always has at least one parameter, called self  Self is a reference to the object that we are creating  The constructor can have other parameters class Point: def __init__(self, loc_x, loc_y): self.x = loc_x self.y = loc_y class Point: def __init__(self, loc_x, loc_y): self.x = loc_x self.y = loc_y from Geometry import Point p = Point(5, 7) print(p.x) print(p.y) from Geometry import Point p = Point(5, 7) print(p.x) print(p.y) Lecture04COMPSCI Saved in a file called Geometry.py

Adding Functionality  Defining more methods  A method to shift a point by a given amount in horizontal and vertical directions  Note: the method is named normally, but has the additional parameter (self) as the first parameter  All methods that are called on an instance of an object need the self parameter class Point: def __init__(self, loc_x, loc_y): self.x = loc_x self.y = loc_y def translate(self, dx, dy): self.x += dx self.y += dy class Point: def __init__(self, loc_x, loc_y): self.x = loc_x self.y = loc_y def translate(self, dx, dy): self.x += dx self.y += dy Lecture04COMPSCI 10515

Using the Point Class  Methods are defined to accept self as the first parameter  We call the method using: object_name.method(params) class Point: def __init__(self, loc_x, loc_y): self.x = loc_x self.y = loc_y def translate(self, dx, dy): self.x += dx self.y += dy class Point: def __init__(self, loc_x, loc_y): self.x = loc_x self.y = loc_y def translate(self, dx, dy): self.x += dx self.y += dy from Geometry import Point p = Point(0,0) p.translate(10,7) from Geometry import Point p = Point(0,0) p.translate(10,7) Lecture04COMPSCI p x: 10 y:7 The object in memory