Classes & Objects
AIMAR F. 1906400186
HERNOWO A. 1906400274
HAIDAR L. 1906400135
SAMUEL S. 1906400204
KELAS: DDP1 - C
DOSEN:
FARIZ DARARI
What is a Class
A class is an entity that determines how an object will behave
and what the object will contain. In other words, it is a
blueprint or a set of instruction to build a specific type of
object.
For example in the real world, you often have many objects of
the same kind. Your bicycle is just one of many bicycles in the
world. Using object-oriented terminology, we say that your
bicycle is an instance of the class bicycles.
A class can also contain methods, which are similar to
functions
What is an OBJECT
Objects in OOP are the product of a Class and is an instance of that of said Class.
In the example given before, if you have a bike than your bike is an instance of
the class Bicycle. You can also set attributes for your bicycle. For example, you
can set the condition of your bicycle, set the characteristics of the bike like the
color, the weight, the material, and you can also set an attribute which signifies
that the bike is owned by you.
Making a Class
Class Bicycle (object):
Wheels = 2
def __init__(self, color, weight, materials):
self.color = color
self.weight = weight
self.materials = materials
Making an Object
We can create an object simply by calling the class with the object name
bike1 = Bicycle()
There are two ways to set an attribute the first one is using the method shown in the previous slide. You
can simply set the attribute as you are creating the object
bike1 = Bicycle(“blue”, 5, [“carbon fiber”, “titanium”]
The second way is if you want to add an attribute if the Class doesn’t have an __init__ method you can set
the attribute by writing
bike1.color = “blue”
Methods
Methods are simillar to function. They are both “small programs” that have parameters, perform some
operation and return a value. The difference is that methods are functions tied to a particular object.
Function:
def something(parameter):
pass
Methods:
class Myclass(object):
def something(self,parameter)
...
Methods
Class-Class Relation
Class-Class Relation
Output:
ROG is the best laptop on 2019
ROG has intel i7 processor
Read the comments aloud as you work
through this program to help you
understand what’s happening, then
before you run the program, see if you
can predict the expected output.
THANK YOU
Refrensi:
https://realpython.com/python3-object-oriented-programming/
https://www.journaldev.com/22460/python-str-repr-functions
The Practice Of Computing Using Python (3RD EDITION) by William Punch and Richard Enbody
https://github.com/ialfina/191-ddp1/tree/master/tp4-template
https://www.youtube.com/watch?v=wfcWRAxRVBA

Classes and object

  • 1.
    Classes & Objects AIMARF. 1906400186 HERNOWO A. 1906400274 HAIDAR L. 1906400135 SAMUEL S. 1906400204 KELAS: DDP1 - C DOSEN: FARIZ DARARI
  • 2.
    What is aClass A class is an entity that determines how an object will behave and what the object will contain. In other words, it is a blueprint or a set of instruction to build a specific type of object. For example in the real world, you often have many objects of the same kind. Your bicycle is just one of many bicycles in the world. Using object-oriented terminology, we say that your bicycle is an instance of the class bicycles. A class can also contain methods, which are similar to functions
  • 3.
    What is anOBJECT Objects in OOP are the product of a Class and is an instance of that of said Class. In the example given before, if you have a bike than your bike is an instance of the class Bicycle. You can also set attributes for your bicycle. For example, you can set the condition of your bicycle, set the characteristics of the bike like the color, the weight, the material, and you can also set an attribute which signifies that the bike is owned by you.
  • 4.
    Making a Class ClassBicycle (object): Wheels = 2 def __init__(self, color, weight, materials): self.color = color self.weight = weight self.materials = materials
  • 5.
    Making an Object Wecan create an object simply by calling the class with the object name bike1 = Bicycle() There are two ways to set an attribute the first one is using the method shown in the previous slide. You can simply set the attribute as you are creating the object bike1 = Bicycle(“blue”, 5, [“carbon fiber”, “titanium”] The second way is if you want to add an attribute if the Class doesn’t have an __init__ method you can set the attribute by writing bike1.color = “blue”
  • 6.
    Methods Methods are simillarto function. They are both “small programs” that have parameters, perform some operation and return a value. The difference is that methods are functions tied to a particular object. Function: def something(parameter): pass Methods: class Myclass(object): def something(self,parameter) ...
  • 7.
  • 8.
  • 9.
    Class-Class Relation Output: ROG isthe best laptop on 2019 ROG has intel i7 processor Read the comments aloud as you work through this program to help you understand what’s happening, then before you run the program, see if you can predict the expected output.
  • 10.
    THANK YOU Refrensi: https://realpython.com/python3-object-oriented-programming/ https://www.journaldev.com/22460/python-str-repr-functions The PracticeOf Computing Using Python (3RD EDITION) by William Punch and Richard Enbody https://github.com/ialfina/191-ddp1/tree/master/tp4-template https://www.youtube.com/watch?v=wfcWRAxRVBA