wohhie
5/29/2017 - 5:09 AM

Python Object Oriented Programing 1. Class & Instance 2. Class Variable

Python Object Oriented Programing

  1. Class & Instance
  2. Class Variable

class Employee:

    raise_amount = 1.04
    num_of_emps = 0

    def __init__(self, firstname, lastname, payment):
        self.firstname = firstname
        self.lastname = lastname
        self.payment = payment
        self.email = firstname + '.' + lastname + '@company.com'

        # Increase number of employee
        Employee.num_of_emps += 1

    def fullname(self):
        return '{} {}'. format(self.firstname, self.lastname)

    def apply_raise(self):
        self.payment = int(self.payment * self.raise_amount)


class Developer(Employee):

    raise_amount = 1.10

    def __init__(self, firstname, lastname, payment, prog_lang):
        super().__init__(firstname, lastname, payment)
        self.prog_lang = prog_lang




class Manager(Employee):

    def __init__(self, firstname, lastname, payment, employees = None):
        super().__init__(firstname, lastname, payment)
        if  employees is None:
            self.employees = []
        else:
            self.employees = employees


    def add_employee(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('Corey', 'Schafer', 50000, 'Python')
dev_2 = Developer('Wohhie', 'Mahmud', 40000, 'Java')



# print(dev_1.payment)
# dev_1.apply_raise()
# print(dev_1.payment)

mgr_1 = Manager('Sue', 'Smith', 90000, [dev_1])
print(mgr_1.email)
print(mgr_1.print_emps())

mgr_1.add_employee(dev_2)
mgr_1.remove_emp(dev_1)

print(mgr_1.print_emps())


print('--------------------------------')




class Employee:

    raise_amount = 1.04
    num_of_emps = 0

    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    @property
    def email(self):
        return '{}.{}@email.com'. format(self.firstname, self.lastname)

    @property
    def fullname(self):
        return '{} {}'. format(self.firstname, self.lastname)

    @fullname.setter
    def fullname(self, name):
        firstname, lastname = name.split(' ')
        self.firstname = firstname
        self.lastname = lastname


emp_1 = Employee('Jewel', 'Mahmud')

emp_1.firstname = 'Wohhie'
emp_1.fullname = 'Corey Schafer'

print(emp_1.firstname)
print(emp_1.lastname)
print(emp_1.fullname)
print(emp_1.email)


class Employee:

    raise_amount = 1.04
    num_of_emps = 0

    def __init__(self, firstname, lastname, payment):
        self.firstname = firstname
        self.lastname = lastname
        self.payment = payment
        self.email = firstname + '.' + lastname + '@company.com'

        # Increase number of employee
        Employee.num_of_emps += 1

    def fullname(self):
        return '{} {}'. format(self.firstname, self.lastname)

    def apply_raise(self):
        self.payment = int(self.payment * self.raise_amount)




emp_1 = Employee('Corey', 'Schafer', 50000)
emp_2 = Employee('Wohhie', 'Mahmud', 40000)

emp_1.apply_raise()
print(emp_1.payment)
print('Number of Employee: ', Employee.num_of_emps)




class Employee:

    def __init__(self, firstname, lastname, payment):
        self.firstname = firstname
        self.lastname = lastname
        self.payment = payment
        self.email = firstname + '.' + lastname + '@company.com'

    def fullname(self):
        return '{} {}'. format(self.firstname, self.lastname)




emp_1 = Employee('Corey', 'Schafer', 50000)
emp_2 = Employee('Wohhie', 'Mahmud', 40000)

## Print(emp_1)
## Print(emp_2)

# print(emp_1.email)
# print(emp_2.email)

print(Employee.fullname(emp_1))