Skip to content

Commit 3f6a1b8

Browse files
some example
1 parent 78e2125 commit 3f6a1b8

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# ------------------ CLASS STRUCTURE (Multiple Inheritance) ------------------
2+
#
3+
# Person Job
4+
# | |
5+
# | |
6+
# -----> Employee <-----
7+
#
8+
# Employee inherits properties from both Person and Job
9+
# ---------------------------------------------------------------------------
10+
11+
12+
# Parent Class 1
13+
class Person:
14+
# Constructor to initialize name
15+
def __init__(self, name):
16+
self.name = name # store the name in the object
17+
18+
19+
# Parent Class 2
20+
class Job:
21+
# Constructor to initialize salary
22+
def __init__(self, salary):
23+
self.salary = salary # store the salary in the object
24+
25+
26+
# Child Class inheriting from both Person and Job
27+
class Employee(Person, Job):
28+
29+
# Constructor of Employee
30+
def __init__(self, name, salary):
31+
# Call constructor of Person to set name
32+
Person.__init__(self, name)
33+
34+
# Call constructor of Job to set salary
35+
Job.__init__(self, salary)
36+
37+
# Method to display employee details
38+
def details(self):
39+
print(self.name, "earns", self.salary)
40+
41+
42+
# ------------------ PROGRAM FLOW ------------------
43+
#
44+
# emp = Employee("Jennifer", 50000)
45+
# |
46+
# |-- Person.__init__() → sets name
47+
# |
48+
# |-- Job.__init__() → sets salary
49+
# |
50+
# v
51+
# emp.details() → prints name and salary
52+
#
53+
# Object Memory:
54+
# emp
55+
# ├── name = "Jennifer"
56+
# └── salary = 50000
57+
# --------------------------------------------------
58+
59+
60+
# Create an object of Employee
61+
emp = Employee("Jennifer", 50000)
62+
63+
# Call the method to display details
64+
emp.details()

0 commit comments

Comments
 (0)