0% found this document useful (0 votes)
11 views

Python 2 - P3 OOP PDF

Here is an example of encapsulation in Python by declaring private variables: ```python class Person: def __init__(self, name, age): self.__name = name self.__age = age def get_name(self): return self.__name def get_age(self): return self.__age def set_age(self, age): self.__age = age person = Person("John", 36) print(person.get_name()) print(person.get_age()) person.set_age(37) print(person.get_age()) ``` In this example, the `__name` and `

Uploaded by

Lê Hà
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Python 2 - P3 OOP PDF

Here is an example of encapsulation in Python by declaring private variables: ```python class Person: def __init__(self, name, age): self.__name = name self.__age = age def get_name(self): return self.__name def get_age(self): return self.__age def set_age(self, age): self.__age = age person = Person("John", 36) print(person.get_name()) print(person.get_age()) person.set_age(37) print(person.get_age()) ``` In this example, the `__name` and `

Uploaded by

Lê Hà
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

PYTHON 2

OOP
TS. NGUYỄN HOÀNG LONG
0916666384
[email protected]
OBJECT ORIENTED PROGRAMMING

An object contains data and behavior The concept of OOP in Python focuses
on developing reusable code. This
concept is also known as DRY (Don't
Repeat Yourself).

Conceptually, objects are like the Object-oriented programming (OOP) is


components of a system. a method of structuring a program by
bundling related properties and
behaviors into individual objects

2
OBJECT ORIENTED PROGRAMMING

Computationally, OOP software is Suitable for big and complicated


slower, and uses more memory since projects
more lines of code have to be written

OOPs take time to get used to it. The Development is faster and cheaper,
concept may be too complex for with better software maintainability
beginners

3
4 CLASS VS
OBJECT
• Class: can be defined as a template
which describes the data and
methods of a group of object

• Object: object is an instance of a


predefined class or subclass with the
class's own methods or procedures
and data variables
5 CLASS VS
OBJECT
• Class: can be defined as a template
which describes the data and
methods of a group of object

• Object: object is an instance of a


predefined class or subclass with the
class's own methods or procedures
and data variables
CLASS = DATA + METHODS

• DATA or Attributes: characteristics of the objects

• METHODS: actions that the objects can perform

6
DECLARE A CLASS

• Syntax: class class-name

• __init__() function: the initializer that


is used to instantiate objects
class class-name: • Self is the default parameter, refers to the
def __init__(self, data1, data2, …):
self.data1 = data1 object itself
self.data2 = data2
…. • data1, data2: attributes of the object

7
INSTANTIATE AN OBJECT
• Instantiate an object
object = class-name (data1, data2, …)

8
INSTANTIATE AN OBJECT
• Instantiate an object
object = class-name (data1, data2, …)

• Access an object's attributes


object.data-name

9
INSTANTIATE AN OBJECT
• Instantiate an object
object = class-name (data1, data2, …)

• Access an object's attributes


object.data-name

10
RUN

11
PRACTISE 2-1

Write a Python program to create a class


of card game

The Card class should have a suit


(Hearts, Diamonds, Clubs, Spades)
and a value (A,2,3,4,5,6,7,8,9,10,J,Q,K)

Instantiate a card object, then print


the card values
INSTANCE METHODS

• Instance methods are functions defined in


classes and accessed only from instances
(objects) of those classes.
• Similar to define a function, the def
keyword is used to defined an instance
method with the self argument.

13
INSTANCE METHODS

• Instance methods are functions defined in


classes and accessed only from instances
(objects) of those classes.
• Similar to define a function, the def
keyword is used to defined an instance
method with the self argument.
• To call an instance method:
object.method-name(arguments)

14
RUN

15
PRACTISE 2-2

Write an instance method of the Card


class to print the card values
INSTANCE METHOD WITH
PARAMETERS
Add delay() method to class Flight

17
RUN

18
PRACTISE 2-3
Write a Python program to create a class
of a desk of cards

There should be a shuffle method


which makes sure the deck of cards
has all 52 cards and then rearranges
them randomly

Instantiate a deck object, then print all the


cards in the deck object

Run shuffle() then print all the cards in


the deck object
SHUFFLE

20
DUNDER METHOD

21
DUNDER METHOD
Dunder method is also called magic method. Dunder method is used to customized the built-in
method of classes

22
DEFAULT GREATER THAN FUNCTION

23
CUSTOMIZE THE
GREATER THAN FUNCTION

24
PRACTISE 2-4
The Deck class should have a deal
method to deal a single card from the
deck

After a card is dealt, it is removed


from the deck

Deal a card, then print the dealed


card and the remaining cards in the
desk

Customise the greater than function


to compare two deal cards (e.g. the
Ace card is stronger than the K card)
DEL OBJECT
Objects should be deleted after used to release the memory

26
CUSTOMISE DEL() FUNCTION

27
PRACTISE 2-5

Customise the print function of the Deck


class
ADD MORE ATTRIBUTES TO CLASS
• Add variable counter to class Flight
• counter is the class attribute and can be
access by using:
Class-name.counter
• Add id attribute to class Flight
• counter is used to count the number of
flight and be the value for id attribute
• Add passengers attribute to store the list of
passenger of each flight

29
UPGRADE AND ADD MORE METHOD
• Add more code to print_info(self) method

• Add add_passenger(self, p) method

30
ADD CLASS PASSENGER

31
UPGRADE MAIN FUNCTION

32
RUN

33
ACCESS CLASS ATTRIBUTE

To access class attribute


object.__class__.class-attribute

34
RUN

35
PRACTISE 2-6

Write a Python program to create Player


class

Instantiate four players

Write a method that deals nine cards


in the desk to each player

Print the cards of each player


INHERITANCE

The sub-class inherits all the data and A class (often called a super class) can
methods of the parent class. The be inherited by another class (often
inherited components can be extended. called a sub-class or child-class)
New data and methods can be also
added into the sub-class

Inheritance is one of the typical One of the main advantages of Object


mechanisms of reusability Oriented Programming is reusability

37
AIRLINE PROJECT
• An airline company offers two types of flight:
• Cheap flight
• Normal flight

• Normal flight:
✓ passengers can get customer points for the
purchased tickets
✓ Provide meal for passengers

• Cheap flight:
✓ Passengers do not get customer points for the
purchased tickets
✓ Passenger can get one bottle of water

38
SOLUTION

class Cheap_Flight class Normal_Flight

39
INHERITANCE
CHEAP FLIGHT – NORMAL FLIGHT

40
TEST INHERITANCE

41
CHALLENGE

Think about a case when you can apply


inheritance to solve your problems

Write python program to


demonstrate your idea
ENCAPSULATION

The private property is denoted by a "-" Those types of variables are called
or "__" as a prefix private variables

An object's variable can not be assigned Encapsulation is used to prevent


a new value. It only can be done by accidental changes on the value of a
using the object's method. specific object’s attribute by places
restrictions on direct access to that
object’s attribute

43
NO ENCAPSULATION

44
ENCAPSULATION
DECLARE PRIVATE VARIABLE

45
ASSIGN NEW VALUE
FOR PRIVATE VARIABLE

46
ENCAPSULATION
ADD CHANGE_DRINK METHOD

47
POLYMORPHISM

For example: the input for the len The coding process is easier and more
function can be either a string or a a list efficient when using Polymorphism in
some cases.

Polymorphism = the same function Polymorphism = Poly (many) and


name can be used for different data morphism (forms).
types (objects)

48
POLYMORPHISM - 1

49
POLYMORPHISM - 2

50
POLYMORPHISM – 3

51
POLYMORPHISM - 4

52
POLYMORPHISM - 5

53
POLYMORPHISM - 6

54

You might also like