1 Object Oriented Programming Revisited Classes – Ideal for modeling real-world phenomena – Encapsulate data: Attributes – Encapsulate function: Behaviors.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Object Oriented Programming in PHP 5
Games and Simulations O-O Programming in Java The Walker School
1 Objects and classes Classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena – Act as blueprints.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Road Map Introduction to object oriented programming. Classes
1 Objects and classes Classes – Act as blueprints Objects are instantiated from classes – Encapsulate data: Attributes – Encapsulate function: Methods.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
 2002 Prentice Hall. All rights reserved. 1 Object Oriented Programming Revisited Object Orientation –Classes Encapsulate data –Attributes Encapsulate.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
ASP.NET Programming with C# and SQL Server First Edition
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fundamentals of Python: From First Programs Through Data Structures
Review of C++ Programming Part II Sheng-Fang Huang.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
CSE 114 Computer Science I Objects Lake Superior, Michigan.
CSCI/CMPE 4341 Topic: Programming in Python Review: Final Exam Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Working With Objects Tonga Institute of Higher Education.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 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.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
1 CSC241: Object Oriented Programming Lecture No 02.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CSCI 171 Presentation 15 Introduction to Object–Oriented Programming (OOP) in C++
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
PyGame - Unit 4 PyGame Unit – Object Oriented Programming OOP.
C++ Objects and Scope. Important Definitions  Class  Access and Visibility  Encapsulation  Unified Modeling Language (UML)  Constructor  Destructor.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
University of Central Florida COP 3330 Object Oriented Programming
Introduction to Classes
Road Map Introduction to object oriented programming. Classes
Class: Special Topics Copy Constructors Static members Friends this
Creating and Deleting Instances Access to Attributes and Methods
Procedural Abstraction Object-Oriented Code
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Teach A-Level Computer Science: Object-Oriented Programming in Python
An Introduction to Object Orientated Programming
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
C++ Programming ㅎㅎ String OOP Class Constructor & Destructor.
G. Pullaiah College of Engineering and Technology
Presentation transcript:

1 Object Oriented Programming Revisited Classes – Ideal for modeling real-world phenomena – Encapsulate data: Attributes – Encapsulate function: Behaviors – Act as blueprints Objects are instantiated from classes

2 An abstract game with two players Player: – Name – Overall skill (5.0 – 10.0) – Motivation flux mf (0.0 – 1.0) – Current strength: Current motivation cm : random number in [ -mf, mf ] Current strength = skill + skill * cm With motivation 1.0, current strength is 2 * skill. With motivation 0.0, current strength is skill. Motivation flux 0.0: very stable player. Motivation flux 1.0: very unstable player.

3 Object reference self first argument in all methods self refers to the object on which the method is called Object attribute self.skill vs. local variable skill player.py

4 >>> from player import Player >>> john = Player('John', 1.0, 8.0) >>> paul = Player('Paul') >>> print paul.skill 7.5 john name = ‘John’ skill = 8.0 motivation_flux = 1.0 name = ‘Paul’ skill = 7.5 motivation_flux = 0.0 paul No self argument in method calls! Python puts object reference in place of self class Player

5 Summary – Class header Keyword class begins definition, followed by name of class and colon ( : ) – Body of class Indented block of code – Optional documentation string Describes the class Appears immediately after class header – Constructor method __init__ Executes each time an object is created Initialize attributes of class Returns None – Object reference All methods must at least specify this one parameter Represents object of class on which a method is called Called self by convention

6 Remember the self. prefix when referencing object attributes >>> from player_wrong import Player >>> ringo = Player("Ringo") >>> print ringo.my_skill Traceback (most recent call last): File " ", line 1, in ? AttributeError: Player instance has no attribute ‘my_skill'

7 Let’s do something with our objects: method play player2.py >>> from player2 import Player >>> john = Player("John", 1.0, 8.0) >>> paul = Player("Paul") >>> john.play(paul) John (11.97) - Paul (7.50): John wins >>> john.play(paul) John (1.88) - Paul (7.50): Paul wins >>> paul.play(john) Paul (7.50) - John (3.32): Paul wins

8 Destructors Constructor named __init__ Destructor – Method is named __del__ – Executed automatically when object is destroyed or when no more references to object exist – Can be used for housekeeping before Python reclaims object memory

9 Class Attributes – One copy of attribute shared by all objects of a class – Represents “class-wide” information Property of the class, not of an object of the class – Initialized once in the class definition (not in constructor) Otherwise it would be re-initialized every time an object is created – Accessed through the class name or any object of the class May exist even if no objects of class exist

10 Adding class attribute count + destructor player3.py >>> from player3 import Player >>> paul = Player('Paul') >>> ringo = Player('Ringo') >>> del paul Player Paul deleted

11 Preparing to hold tournaments player4.py New object attribute wins to keep track of match results New play method which updates attribute wins

12 Testing.. >>> from player4 import Player >>> john = Player('John', 0.2, 8.0) >>> paul = Player('Paul') >>> john.play(paul) John defeats Paul >>> john.play(paul) John defeats Paul >>> john.play(paul) John defeats Paul >>> john.play(paul) Paul defeats John >>> print john.wins 3 >>> print paul.wins 1 >>> ^D Player Paul deleted Player John deleted threonine:~/python% NB: destructor called automatically when session ends

13 Now we’re ready to hold a tournament! player_game.py

14 Holding tournament with four players threonine:~/python% python player_game.py Player name: John John's skill ( ): 8 John's motivation flux ( ): 0.2 Player name: Paul Paul's skill ( ): 7.5 Paul's motivation flux ( ): 0 Player name: George George's skill ( ): 7.5 George's motivation flux ( ): 0.5 Player name: Ringo Ringo's skill ( ): 7.5 Ringo's motivation flux ( ): 1.0 Paul defeats John John defeats George Ringo defeats John George defeats Paul Ringo defeats Paul Ringo defeats George Ringo is the winner with 3 wins! Tournament matches Player initialization