Computer Science
Objects & Algorithms
Hiru_93
2022. 10. 21. 20:08
Let's say we have a class such as:
class student:
def __init__(self):
self.firstname = 'David'
self.lastname = 'Joyner'
DJ = student()
in this code, we can say DJ is an instance, student is object
object is a general class that cannot be specified, and DJ is an instance that can be specified.
for the other example, we can say 'person' is object, 'David Joyner' is instance that exists only one in a world.
- Encapsulating Methods in Classes
Getter vs Setter difference
Getters and setters has features following by:
- Let us log whenever a variable's value is accessed or changed
- let us prevent unauthorized access to instance variable's values
- let us build customized ways for modifying or retrieving instance variable's values
Now let's study about constructors in Python
class Person;
def __init__(self, firstname="[no first name]",
lastname="[no last name]"):
self.firstname = firstname
self.lastname = lastname
self.eyecolor = "[no eye color]"
self.age = -1
myPerson = Person()
print(myPerson1.firstname)
myPerson2 = Person(firstname = "David")
myPerson3 = Person("Vrushali")
print(myPerson3.firstname)
in this code, the print outputs will be:
[no first name]
David
Vrushali
we can see if we don't put the arguments when we create new instance, it defaults to those default arguments
and when we put the argument such as firstname = "David" or "Vrushali", it overrides the default value