Professional Documents
Culture Documents
Classes and Objects
Classes and Objects
Classes and Objects
All-in-One Course
1
Year 2024
Outline
Ø Motivation
Ø Classes and Objects
Ø Inheritance
2
AI VIETNAM
All-in-One Course
Motivation
v Review procedural programming
ML Robot
CV Robot
NLP Robot
3
AI VIETNAM
All-in-One Course
Motivation
v Object-based Organization
ML Robot
CV Robot
NLP Robot
4
AI VIETNAM
All-in-One Course
Motivation
v Group the common features of the objects into a prototype
5
AI VIETNAM
All-in-One Course
Motivation
v Use the prototype instead of objects
ML Robot
CV Robot
NLP Robot
6
Outline
Ø Motivation
Ø Classes and Objects
Ø Inheritance
7
AI VIETNAM
All-in-One Course
Classes and Objects
v Abstract view
Methods/ head
Attributes
Actions body
arm
leg
move
process
head body arm leg move process emit
emit
8
AI VIETNAM
All-in-One Course
Classes and Objects
v Class Diagram
+ width
Attributes + height 3 parts
+ calculate_area ( )
Methods
+ calculate_perimeter ( )
9
AI VIETNAM
All-in-One Course
Classes and Objects
v Syntax for creating a class
+ width
Attributes + height
+ calculate_area ( )
Methods + calculate_perimeter ( )
10
AI VIETNAM
All-in-One Course
Classes and Objects
v Syntax for creating a class
Class
Objects
11
AI VIETNAM
All-in-One Course
Classes and Objects
v Constructor
12
AI VIETNAM
All-in-One Course
Classes and Objects
v Constructor
13
AI VIETNAM
All-in-One Course
Classes and Objects
v Another approach to declaring a class
14
AI VIETNAM
All-in-One Course
Classes and Objects
v Self keyword
15
AI VIETNAM
All-in-One Course
Classes and Objects
v Self keyword
16
AI VIETNAM
All-in-One Course
Classes and Objects
v Some rules when using self keyword
17
AI VIETNAM
All-in-One Course
Classes and Objects
v Replacement for self keyword
18
AI VIETNAM
All-in-One Course
Classes and Objects
v The special function: __call__( ) method
19
AI VIETNAM
All-in-One Course
Naming Conventions
v Survey the naming styles of a few popular repos
https://github.com/ultralytics/yolov5/blob/master/models/yolo.py
20
AI VIETNAM
All-in-One Course
Naming Conventions
v Survey the naming styles of a few popular repos
https://github.com/lucidrains/denoising-diffusion-
pytorch/blob/main/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py 21
AI VIETNAM
All-in-One Course
Naming Conventions
v Survey the naming styles of a few popular repos
https://github.com/lucidrains/denoising-diffusion-
pytorch/blob/main/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py 22
AI VIETNAM
All-in-One Course
Naming Conventions
Fun fact:
In Python, everything is an object.
24
AI VIETNAM
All-in-One Course
Classes and Objects
25
AI VIETNAM
All-in-One Course
Lists and Classes
Approach 2:
27
Outline
Ø Motivation
Ø Classes and Objects
Ø Inheritance
28
AI VIETNAM
All-in-One Course
Inheritance
v Motivation
VacuumCleaner
EggBeater
29
AI VIETNAM
All-in-One Course
Inheritance
v Motivation
Chip
Ø Code Reusability
Inheritance allows you to reuse previously written
code segments.
Ø Scalability
You can easily extend the functionality of classes by
modifying the SuperClass.
31
AI VIETNAM
All-in-One Course
Inheritance
v Definition and simple syntax
SuperClass
Inheritance is a mechanism in object-
oriented programming (OOP) that allows a
new class to inherit the attributes and
methods of an existing class.
is
SubClass
32
AI VIETNAM
All-in-One Course
Inheritance
v Example
Animal
+ name
+ make_sound( )
is
Cat
+ breed
+ info( )
33
AI VIETNAM
All-in-One Course
Inheritance
v Access Modifiers
Name Class
Ø Public data: Accessible anywhere from otside oclass.
+ public_attribute
Ø Private data: Accessible within the class # protected_attribute
Ø Protected data: Accessible within the class and its - private_attribute
sub-classes. + public_method( )
# protected_method( )
- private_method( )
34
AI VIETNAM
All-in-One Course
Inheritance
v Access Modifiers: Public
Cat
+ name
+ color
+ age
//..
35
AI VIETNAM
All-in-One Course
Inheritance
v Access Modifiers: Private
Cat
+ name
+ color
- age
//..
36
AI VIETNAM
All-in-One Course
Inheritance
v Access Modifiers: Private
Cat
+ name
+ color
- age
//..
37
AI VIETNAM
All-in-One Course
Inheritance
v Access Modifiers: Protected
Animal
+ name
# color
# make_sound( )
is
Cat
+ breed
+ info( )
+ sound( )
38
AI VIETNAM
All-in-One Course
Inheritance
v Access Modifiers: Protected
Animal
+ name
# color
# make_sound( )
is
Cat
+ breed
+ info( )
+ sound( )
39
AI VIETNAM
All-in-One Course
Inheritance
v Override
Animal
+ name
+ make_sound( )
is
Cat
+ breed
+ info( )
+ make_sound( )
40
AI VIETNAM
All-in-One Course
Example
v Implement the two classes below
Math1 Math2
41
AI VIETNAM
All-in-One Course
Example
v Implement the two classes below
Math1
+ isEven(int): bool
+ factorial(int): int
42
AI VIETNAM
All-in-One Course
Example
v Implement the two classes below
𝑒 = 2.71828
Math2
+ isEven(int): bool
+ factorial(int): int
+ estimateEuler(int): double
43
AI VIETNAM
All-in-One Course
Example
Implement the two classes below
𝑒 = 2.71828
Math2
+ isEven(int): bool
+ factorial(int): int
+ estimateEuler(int): double
44
AI VIETNAM
All-in-One Course
Example
How to reuse an existing class?
Math1
+ isEven(int): bool
+ factorial(int): int
Math2
+ isEven(int): bool
+ factorial(int): int
+ estimateEuler(int): double
45
AI VIETNAM
All-in-One Course
Example
v Inheritance
Math1
46
47
AI VIETNAM
All-in-One Course
48