-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathten.py
More file actions
96 lines (76 loc) · 3.44 KB
/
ten.py
File metadata and controls
96 lines (76 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -----------------------------------------------OBJECT ORIENTED PROGRAMMING-------------------------------------------------------
'''
Solving a problem by creating object is one of the most popular approaches in programming. This is called object-oriented programming.
This concept focuses on using reusable code (DRY Principle).
'''
# -----------------------------------------------CLASS-------------------------------------------------------
'''
A class is a blueprint for creating object.
Syntax:
class Employee: # Class name is written in pascal case
#Methods & Variables
'''
# -----------------------------------------------OBJECT------------------------------------------------------
'''
An object is an instantiation of a class. When class is defined, a template (info) is defined. Memory is allocated only after object instantiation.
Objects of a given class can invoke the methods available to it without revealing the implementation details to the user. - Abstractions & Encapsulation!
'''
# -----------------------------------------------MODELLING A PROBLEM IN OOPS-------------------------------------------------------
'''
We identify the following in our problem.
1) Noun → Class → Employee
2) Adjective → Attributes name, age, salary
3) Verbs → Methods → getSalary(), increment()
'''
# -----------------------------------------------CLASS ATTRIBUTES-------------------------------------------------------
'''
An attribute that belongs to the class rather than a particular object.
'''
class Employee:
language = "Py" # This is a class attribute
salary = 1200000
john = Employee()
john.name = "John" # This is an instance attribute
print(john.name, john.language, john.salary)
rohan = Employee()
rohan.name = "Rohan Robinson"
print(rohan.name, rohan.salary, rohan.language)
# Here name is instance attribute and salary and language are class attributes as they directly belong to the class
# -----------------------------------------------INSTANCE ATTRIBUTES-------------------------------------------------------
class Employee:
language = "Python" # This is a class attribute
salary = 1200000
john = Employee()
john.language = "JavaScript" # This is an instance attribute
print(john.language, john.salary)
# -----------------------------------------------SELF PARAMETER and STATIC METHOD--------------------------------------------------
class Employee:
language = "Python" # This is a class attribute
salary = 1200000
def getInfo(self):
print(f"The language is {self.language}. The salary is {self.salary}")
@staticmethod
def greet():
print("Good morning")
john = Employee()
# john.language = "JavaScript" # This is an instance attribute
john.greet()
john.getInfo()
# Employee.getInfo(john)
# -----------------------------------------------_INIT_() CONSTRUCTOR-------------------------------------------------------
class Employee:
language = "Python" # This is a class attribute
salary = 1200000
def __init__(self, name, salary, language): # dunder method which is automatically called
self.name = name
self.salary = salary
self.language = language
print("I am creating an object")
def getInfo(self):
print(f"The language is {self.language}. The salary is {self.salary}")
@staticmethod
def greet():
print("Good morning")
john = Employee("John", 1300000, "JavaScript")
# john.name = "John"
print(john.name, john.salary, john.language)