Python - OOP(Object-Oriented Programming) Inheritance - Creating Subclasses
** OOP(Object-Oriented Programming) Inheritance - Creating Subclasses
==========================================
# Python Object-Oriented Programming
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
Employee.num_of_emps += 1
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
class Developer(Employee):
pass
dev_1 = Employee('HAN', 'SeokDu', 50000)
dev_2 = Employee('KIM', 'Young', 30000)
print(dev_1.email)
print(dev_2.email)
---------------------------------
HAN.SeokDu@company.com
KIM.Young@company.com
---------------------------------
==========================================
# Python Object-Oriented Programming
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
Employee.num_of_emps += 1
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
class Developer(Employee):
pass
dev_1 = Developer('HAN', 'SeokDu', 50000)
dev_2 = Developer('KIM', 'Young', 30000)
print(dev_1.email)
print(dev_2.email)
---------------------------------
HAN.SeokDu@company.com
KIM.Young@company.com
---------------------------------
==========================================
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
Employee.num_of_emps += 1
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
class Developer(Employee):
pass
dev_1 = Developer('HAN', 'SeokDu', 50000)
dev_2 = Developer('KIM', 'Young', 30000)
print(help(Developer))
---------------------------------
Help on class Developer in module __main__:
class Developer(Employee)
| Developer(first, last, pay)
|
| Method resolution order:
| Developer
| Employee
| builtins.object
|
| Methods inherited from Employee:
|
| __init__(self, first, last, pay)
| Initialize self. See help(type(self)) for accurate signature.
|
| apply_raise(self)
|
| fullname(self)
|
| ----------------------------------------------------------------------
| Data descriptors inherited from Employee:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from Employee:
|
| num_of_emps = 2
|
| raise_amt = 1.04
None
---------------------------------
==========================================
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
Employee.num_of_emps += 1
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
class Developer(Employee):
pass
dev_1 = Developer('HAN', 'SeokDu', 50000)
dev_2 = Developer('KIM', 'Young', 30000)
# print(dev_1.email)
# print(dev_2.email)
print(dev_1.pay)
dev_1.apply_raise()
print(dev_1.pay)
---------------------------------
50000
52000
---------------------------------
==========================================
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
Employee.num_of_emps += 1
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
class Developer(Employee):
raise_amt = 1.10
dev_1 = Developer('HAN', 'SeokDu', 50000)
dev_2 = Developer('KIM', 'Young', 30000)
print(dev_1.pay)
dev_1.apply_raise()
print(dev_1.pay)
---------------------------------
50000
55000
---------------------------------
==========================================
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
Employee.num_of_emps += 1
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
class Developer(Employee):
raise_amt = 1.10
dev_1 = Employee('HAN', 'SeokDu', 50000)
dev_2 = Developer('KIM', 'Young', 30000)
print(dev_1.pay)
dev_1.apply_raise()
print(dev_1.pay)
---------------------------------
50000
52000
---------------------------------
==========================================
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
Employee.num_of_emps += 1
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
class Developer(Employee):
raise_amt = 1.10
def __init__(self, first, last, pay, prog_lang):
super().__init__(first, last, pay)
self.prog_lang = prog_lang
dev_1 = Developer('HAN', 'SeokDu', 50000, 'Python')
dev_2 = Developer('KIM', 'Young', 30000, 'Java')
print(dev_1.email)
print(dev_1.prog_lang)
---------------------------------
HAN.SeokDu@company.com
Python
---------------------------------
==========================================
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
Employee.num_of_emps += 1
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
class Developer(Employee):
raise_amt = 1.10
def __init__(self, first, last, pay, prog_lang):
super().__init__(first, last, pay)
self.prog_lang = prog_lang
class Manager(Employee):
def __init__(self, first, last, pay, employees = None):
super().__init__(first, last, pay)
if employees is None:
self.employees = []
else:
self.employees = employees
def add_emp(self, emp):
if emp not in self.employees:
self.employees.append(emp)
def remove_emp(self, emp):
if emp in self.employees:
self.employees.remove(emp)
def print_emps(self):
for emp in self.employees:
print('-->', emp.fullname())
dev_1 = Developer('HAN', 'SeokDu', 50000, 'Python')
dev_2 = Developer('KIM', 'Young', 30000, 'Java')
mgr_1 = Manager('Sue', 'Smith', 90000, [dev_1])
print(mgr_1.email)
mgr_1.print_emps()
---------------------------------
Sue.Smith@company.com
--> HAN SeokDu
---------------------------------
==========================================
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
Employee.num_of_emps += 1
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
class Developer(Employee):
raise_amt = 1.10
def __init__(self, first, last, pay, prog_lang):
super().__init__(first, last, pay)
self.prog_lang = prog_lang
class Manager(Employee):
def __init__(self, first, last, pay, employees = None):
super().__init__(first, last, pay)
if employees is None:
self.employees = []
else:
self.employees = employees
def add_emp(self, emp):
if emp not in self.employees:
self.employees.append(emp)
def remove_emp(self, emp):
if emp in self.employees:
self.employees.remove(emp)
def print_emps(self):
for emp in self.employees:
print('-->', emp.fullname())
dev_1 = Developer('HAN', 'SeokDu', 50000, 'Python')
dev_2 = Developer('KIM', 'Young', 30000, 'Java')
mgr_1 = Manager('Sue', 'Smith', 90000, [dev_1])
print(mgr_1.email)
mgr_1.add_emp(dev_2)
mgr_1.print_emps()
---------------------------------
Sue.Smith@company.com
--> HAN SeokDu
--> KIM Young
---------------------------------
==========================================
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
Employee.num_of_emps += 1
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
class Developer(Employee):
raise_amt = 1.10
def __init__(self, first, last, pay, prog_lang):
super().__init__(first, last, pay)
self.prog_lang = prog_lang
class Manager(Employee):
def __init__(self, first, last, pay, employees = None):
super().__init__(first, last, pay)
if employees is None:
self.employees = []
else:
self.employees = employees
def add_emp(self, emp):
if emp not in self.employees:
self.employees.append(emp)
def remove_emp(self, emp):
if emp in self.employees:
self.employees.remove(emp)
def print_emps(self):
for emp in self.employees:
print('-->', emp.fullname())
dev_1 = Developer('HAN', 'SeokDu', 50000, 'Python')
dev_2 = Developer('KIM', 'Young', 30000, 'Java')
mgr_1 = Manager('Sue', 'Smith', 90000, [dev_1])
print(mgr_1.email)
mgr_1.add_emp(dev_2)
mgr_1.remove_emp(dev_1)
mgr_1.print_emps()
---------------------------------
Sue.Smith@company.com
--> KIM Young
---------------------------------
==========================================
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
Employee.num_of_emps += 1
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
class Developer(Employee):
raise_amt = 1.10
def __init__(self, first, last, pay, prog_lang):
super().__init__(first, last, pay)
self.prog_lang = prog_lang
class Manager(Employee):
def __init__(self, first, last, pay, employees = None):
super().__init__(first, last, pay)
if employees is None:
self.employees = []
else:
self.employees = employees
def add_emp(self, emp):
if emp not in self.employees:
self.employees.append(emp)
def remove_emp(self, emp):
if emp in self.employees:
self.employees.remove(emp)
def print_emps(self):
for emp in self.employees:
print('-->', emp.fullname())
dev_1 = Developer('HAN', 'SeokDu', 50000, 'Python')
dev_2 = Developer('KIM', 'Young', 30000, 'Java')
mgr_1 = Manager('Sue', 'Smith', 90000, [dev_1])
print(isinstance(mgr_1, Manager))
print(isinstance(mgr_1, Employee))
print(isinstance(mgr_1, Developer))
print("-"*80)
print(issubclass(Developer, Employee))
print(issubclass(Manager, Employee))
print(issubclass(Manager, Developer))
---------------------------------
True
True
False
--------------------------------------------------------------------------------
True
True
False
---------------------------------