Download presentation
Presentation is loading. Please wait.
Published byAriel Fay Young Modified over 9 years ago
1
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information and Communication Technology Box Hill Institute of TAFE
2
September 7, 2015September 7, 2015September 7, 2015Slide2 Copyright Box Hill Institute Contents at a Glance Introduction to classes Introduction to classes Methods Methods Introduction to Objects Introduction to Objects UML UML Program Example Program Example
3
September 7, 2015September 7, 2015September 7, 2015Slide3 Copyright Box Hill Institute Class Class - A blueprint for an object. Contains attributes of each object that it defines Class - A blueprint for an object. Contains attributes of each object that it defines Attributes Attributes Data (aka fields) of a class - describe the state of an object Data (aka fields) of a class - describe the state of an object behaviours or methods of a class - describe what an object can do behaviours or methods of a class - describe what an object can do
4
September 7, 2015September 7, 2015September 7, 2015Slide4 Copyright Box Hill Institute Data type – describes the type of data held in a field Data type – describes the type of data held in a field Instance - an object that is used by an application Instance - an object that is used by an application Method - set of instruction describing the operations performed by an object - essentially a function that belongs to a class Method - set of instruction describing the operations performed by an object - essentially a function that belongs to a class
5
September 7, 2015September 7, 2015September 7, 2015Slide5 Copyright Box Hill Institute Naming of Classes Must begin with a letter of an alphabet Must begin with a letter of an alphabet Can contain only letters, digits, or underscores Can contain only letters, digits, or underscores Can not be a keyword or literal Can not be a keyword or literal Case sensitive Case sensitive Start with an upper case letter and then use lower case letters, with the first character of a new word capitalised e.g. Person, MotorVehicle Start with an upper case letter and then use lower case letters, with the first character of a new word capitalised e.g. Person, MotorVehicle
6
September 7, 2015September 7, 2015September 7, 2015Slide6 Copyright Box Hill Institute Example 2.1. Creating a Class #!/usr/bin/python # Filename: simplestclass.py class Person: pass # An empty block p = Person() # Create an instance Person print p
7
September 7, 2015September 7, 2015September 7, 2015Slide7 Copyright Box Hill Institute How It Works We create a new class using the class statement followed by the name of the class. An indented block of statements which form the body of the class follows. In this case, we have an empty block which is indicated using the pass statement. We create a new class using the class statement followed by the name of the class. An indented block of statements which form the body of the class follows. In this case, we have an empty block which is indicated using the pass statement. Next, we create an object or an instance of this class using an empty constructor, which is the name of the class followed by a pair of parentheses. Next, we create an object or an instance of this class using an empty constructor, which is the name of the class followed by a pair of parentheses. For our verification, we confirm the type of the variable by simply printing it. It tells us that we have an instance of the Person class in the __main__ module. For our verification, we confirm the type of the variable by simply printing it. It tells us that we have an instance of the Person class in the __main__ module. Notice that the address of the computer memory where your object is stored is also printed. The address will have a different value on your computer since Python can store the object wherever it finds space. Notice that the address of the computer memory where your object is stored is also printed. The address will have a different value on your computer since Python can store the object wherever it finds space.
8
September 7, 2015September 7, 2015September 7, 2015Slide8 Copyright Box Hill Institute Output >>> >>>
9
September 7, 2015September 7, 2015September 7, 2015Slide9 Copyright Box Hill Institute Constructor Methods All class definitions include a constructor method to create objects All class definitions include a constructor method to create objects methodName = ClassName() methodName = ClassName() ClassName() is a null constructor ClassName() is a null constructor NULL constructor – without arguments NULL constructor – without arguments
10
September 7, 2015September 7, 2015September 7, 2015Slide10 Copyright Box Hill Institute Example 2.2. Using Object Methods #!/usr/bin/python # Filename: method.py class Person: def sayHi(self): print 'Hello, how are you?‘ p = Person() # instantiate a person named p p.sayHi() # This short example can also be written as Person().sayHi()
11
September 7, 2015September 7, 2015September 7, 2015Slide11 Copyright Box Hill Institute Output and How It Works >>> Hello, how are you? >>> Here we see the self in action. Notice that the sayHi method takes no parameters but still has the self in the function definition. Here we see the self in action. Notice that the sayHi method takes no parameters but still has the self in the function definition.
12
September 7, 2015September 7, 2015September 7, 2015Slide12 Copyright Box Hill Institute The self Class methods have only one specific difference from ordinary functions - they must have an extra first name that has to be added to the beginning of the parameter list, but you do not give a value for this parameter when you call the method. Class methods have only one specific difference from ordinary functions - they must have an extra first name that has to be added to the beginning of the parameter list, but you do not give a value for this parameter when you call the method. Python will provide it. Python will provide it. This particular variable refers to the object itself, and by convention, it is given the name self. This particular variable refers to the object itself, and by convention, it is given the name self.
13
September 7, 2015September 7, 2015September 7, 2015Slide13 Copyright Box Hill Institute The __init__ method There are many method names which have special significance in Python classes. The __init__ method is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object. Notice the double underscore both in the beginning and at the end in the name. The __init__ method is a constructor. The __init__ method is a constructor.
14
September 7, 2015September 7, 2015September 7, 2015Slide14 Copyright Box Hill Institute Example 2.3. Using the __init__ method #!/usr/bin/python # Filename: class_init.py class Person: def __init__(self, aName): self.name = aName def sayHi(self): print 'Hello, my name is', self.name p = Person('Swaroop') p.sayHi()
15
September 7, 2015September 7, 2015September 7, 2015Slide15 Copyright Box Hill Institute Output >>> Hello, my name is Swaroop >>>
16
September 7, 2015September 7, 2015September 7, 2015Slide16 Copyright Box Hill Institute Class and Object Variables Class variables are shared in the sense that they are accessed by all objects (instances) of that class. Class variables are shared in the sense that they are accessed by all objects (instances) of that class. There is only one copy of the class variable and when any one object makes a change to a class variable, the change is reflected in all the other instances as well. There is only one copy of the class variable and when any one object makes a change to a class variable, the change is reflected in all the other instances as well. Object variables are owned by each individual object/instance of the class. In this case, each object has its own copy of the field i.e. they are not shared and are not related in any way to the field by the same name in a different instance of the same class. Object variables are owned by each individual object/instance of the class. In this case, each object has its own copy of the field i.e. they are not shared and are not related in any way to the field by the same name in a different instance of the same class. Remember this simple difference between class and object variables. Remember this simple difference between class and object variables. An example will make this easy to understand. An example will make this easy to understand.
17
September 7, 2015September 7, 2015September 7, 2015Slide17 Copyright Box Hill Institute Example 2.4. Using Class and Object Variables See the filename: objvar.py found in the Wk02-Examples folder and Observe its operation
18
September 7, 2015September 7, 2015September 7, 2015Slide18 Copyright Box Hill Institute Example 2.4 details In Example 2.4., we also see the use of docstrings for classes as well as methods. In Example 2.4., we also see the use of docstrings for classes as well as methods. We can access the class docstring at runtime using Person.__doc__ and We can access the class docstring at runtime using Person.__doc__ and the method docstring as Person.sayHi.__doc__ the method docstring as Person.sayHi.__doc__ Just like the __init__ method, there is another special method __del__ which is called when an object is going to die i.e. it is no longer being used Just like the __init__ method, there is another special method __del__ which is called when an object is going to die i.e. it is no longer being used The __del__ method returns the resources used by the object, such as its allocated memory, back to the system for reusing. The __del__ method returns the resources used by the object, such as its allocated memory, back to the system for reusing.
19
September 7, 2015September 7, 2015September 7, 2015Slide19 Copyright Box Hill Institute The __del__ method The __del__ method is run when the object is no longer in use and there is no guarantee when that method will be run. The __del__ method is run when the object is no longer in use and there is no guarantee when that method will be run. If you want to explicitly kill an object, you just have to call the __del__ method. Calling the __del__ method can create some bad side effects, so it is best to avoid it. If you want to explicitly kill an object, you just have to call the __del__ method. Calling the __del__ method can create some bad side effects, so it is best to avoid it. The __del__ method is analogous to the concept of a destructor. The __del__ method is analogous to the concept of a destructor.
20
September 7, 2015September 7, 2015September 7, 2015Slide20 Copyright Box Hill Institute Attribute characteristics All class members (including the data members) are public and all the methods are virtual in Python. All class members (including the data members) are public and all the methods are virtual in Python. One exception: If you use data members with names using the double underscore prefix such as __privateVar, Python uses name-mangling to effectively make it a private variable. One exception: If you use data members with names using the double underscore prefix such as __privateVar, Python uses name-mangling to effectively make it a private variable.
21
September 7, 2015September 7, 2015September 7, 2015Slide21 Copyright Box Hill Institute Attribute characteristics The convention followed is that any variable that is to be used only within the class or object should begin with an underscore, all other names are public and can be used by other classes/objects. Remember that this is only a convention and is not enforced by Python (except for the double underscore prefix). The convention followed is that any variable that is to be used only within the class or object should begin with an underscore, all other names are public and can be used by other classes/objects. Remember that this is only a convention and is not enforced by Python (except for the double underscore prefix).
22
September 7, 2015September 7, 2015September 7, 2015Slide22 Copyright Box Hill Institute Method Types Accessor or getter a method that gets data from an object Accessor or getter a method that gets data from an object Mutator or setter a method that sets data in an object Mutator or setter a method that sets data in an object Custom Method a method to perform a specific user defined task Custom Method a method to perform a specific user defined task
23
September 7, 2015September 7, 2015September 7, 2015Slide23 Copyright Box Hill Institute Message – request for a method Message – request for a method Message sending objects send (and receive) messages to interact with each other [ This is really just a method call ] Message sending objects send (and receive) messages to interact with each other [ This is really just a method call ] Re-use once written and tested, objects and classes can be used in other applications Re-use once written and tested, objects and classes can be used in other applications
24
September 7, 2015September 7, 2015September 7, 2015Slide24 Copyright Box Hill Institute Encapsulation containing or packaging data and methods in a class Encapsulation containing or packaging data and methods in a class Information (data) hiding black box approach i.e. no need to publish details Information (data) hiding black box approach i.e. no need to publish details
25
September 7, 2015September 7, 2015September 7, 2015Slide25 Copyright Box Hill Institute Overloaded Methods methods with same name but different formats (data types) are called overloaded methods methods with same name but different formats (data types) are called overloaded methods method signature identifies the specific method used method signature identifies the specific method used
26
September 7, 2015September 7, 2015September 7, 2015Slide26 Copyright Box Hill Institute UML UML – Unified Modelling Language UML – Unified Modelling Language Diagrams Diagrams Use Case Diagramfunctionality Use Case Diagramfunctionality Class diagramstatic structure Class diagramstatic structure Object diagram Object diagram State chart Diagramdynamic behaviour State chart Diagramdynamic behaviour Collaboration diagramsobject interaction Collaboration diagramsobject interaction Activity diagramsdynamic nature Activity diagramsdynamic nature Component Component Deployment Deployment
27
September 7, 2015September 7, 2015September 7, 2015Slide27 Copyright Box Hill Institute UML Diagrams Rectangle describes classes and objects Rectangle describes classes and objects Lines describe relationships Lines describe relationships Symbols describe accessibility and strength of relationships Symbols describe accessibility and strength of relationships + public - private # protected default (no symbol) Links inheritance association aggregation Links inheritance association aggregation
28
September 7, 2015September 7, 2015September 7, 2015Slide28 Copyright Box Hill Institute UML Class Diagram Person Class Name Attribute s (fields) methods +name : String -dateOfBirth : String -id : integer +Person (String aName, String aDate, int aID)
29
September 7, 2015September 7, 2015September 7, 2015Slide29 Copyright Box Hill Institute References http://www.bhtafe.edu.au/bhive http://www.byteofpython.info http://www.ibiblio.org/g2swap/byteofpython/read/index.html Farrell Joyce, 2006, An Object-Oriented Approach to Programming Logic and Design, [Thomson] Sinan Si Alhir, Learning UML, [O’Reilly Press] Paul Harmon & Mark Watson, Understanding UML
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.