Skip to content

Commit c46d1c4

Browse files
Added Class
1 parent ed29119 commit c46d1c4

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

class.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Diving Into Class and Objects
2+
'''
3+
Class:
4+
A user-defined prototype for an object that defines a set of attributes that
5+
characterize any object of the class.
6+
7+
Object:
8+
A unique instance of a data structure that's defined by its class.
9+
'''
10+
11+
#The following shows how to define a class. Here Employee is the class name.
12+
class Employee:
13+
'The Documentation of the class that describes the functionality of the class.'
14+
15+
16+
# it is the constructor of the class. Whwnever an object is created the constructor will execute aotumatically.
17+
def __init__(self, x):
18+
self.salary = 50000
19+
self.age = x
20+
21+
# These are some basic functions. These will not execute unless you call it.
22+
def details(self,x,n):
23+
self.name = x
24+
self.num = n
25+
26+
def increase_salary(self):
27+
if self.age > 25:
28+
self.salary = self.salary + 50000
29+
30+
def get_details(self):
31+
print(self.name,"\n",self.age,"\n",self.num,"\n",self.salary)
32+
33+
34+
# Creating an object
35+
obj1 = Employee(30) # This will create an object obj1
36+
obj2 = Employee(25) # This will create an object obj2
37+
38+
obj1.details("Sam",88644) # this will call method details with passing name as "sam" and number as "88644"
39+
obj2.increase_salary() # this will call the function increase_salary
40+
obj1.increase_salary()
41+
obj2.details("Joe",99854) # this will call method details with passing name as "Joe" and number as "99854"
42+
43+
obj1.get_details() # this will print the details of the objects
44+
obj2.get_details()
45+
46+
print(Employee.__doc__) # this will print the class documentation.

0 commit comments

Comments
 (0)