CS-116 OOP - week 3 class syntax in python
CS-116 OOP - week 3 class syntax in python
Last Time . . .
Classes are blueprints/code snippets which can be used to instantiate objects
Each instantiated object will have its own set of attributes and behaviours
Encapsulation and data hiding, inheritance, polymorphism and association constitute the main
strengths of object oriented programming paradigm
Data and behaviour can be encapsulated in a single object, enabling it to hide whatever it
wants from other objects
2
CS 116 - Lecture 03
Classes and Object in Python
Today’s Session . . .
3
CS 116 - Lecture 03
Classes and Object in Python
4
CS 116 - Lecture 03
Classes and Object in Python
5
CS 116 - Lecture 03
Classes and Object in Python
# define a class
class Dog:
sound = "bark" # class
attribute
6
CS 116 - Lecture 03
Classes and Object in Python
# define a class
class Dog:
sound = "bark" # class attribute
8
CS 116 - Lecture 03
Classes and Object in Python
# define a class
class Dog:
sound = "bark" # class attribute
# define a class
class Dog:
sound = "bark" # class attribute
10
CS 116 - Lecture 03
Classes and Object in Python
# define a class
class Dog:
sound = "bark" # class attribute
# define a class
class Dog:
sound = "bark" # class attribute
#defining setter methods
def setName (self, n):
self.Name = n
def setAge (self, a):
self.Age = a
def info (self):
print(‘My name is’+ self.Name+’ and my age is’+ self.Age+’and I
can’+ self.sound)
12
CS 116 - Lecture 03
Classes and Object in Python
dog1 = Dog()
dog2 = Dog()
dog1. setName(‘Harry’)
# define a class
dog1.setAge(3)
class Dog:
dog1.info()
sound = "bark" # class attribute
dog2.info()
#defining setter methods
dog2.Name=‘Happy’
def setName (self, n):
dog2.Age=5
self.Name = n
dog2.info()
def setAge (self, a):
self.Age = a
def info (self):
print(‘My name is’+ self.Name+’ and my age is’+ self.Age+’and I can’+ self.sound)
14
CS 116 - Lecture 03
Classes and Object in Python
15
CS 116 - Lecture 03
Classes and Object in Python