How to implement Python inheritance and abstract classes _python

Source: Internet
Author: User
Tags inheritance

The examples in this article describe the implementation of Python inheritance and abstract classes. Share to everyone for your reference.

The implementation methods are as follows:

Copy Code code as follows:
#!/usr/local/bin/python
# Fig 9.9:fig09_09.py
# Creating a class hierarchy with an abstract base class.

Class Employee:
"" "Abstract base class Employee" "

def __init__ (self, I, last):
"" "Employee constructor, takes name.
Note:cannot Create object of Class Employee. "" "

if self.__class__ = = Employee:
Raise Notimplementederror, \
"Cannot create object of class Employee"

Self.firstname = A
Self.lastname = Last

def __str__ (self):
"" String Representation of Employee "" "

Return '%s%s '% (Self.firstname, self.lastname)

def _checkpositive (self, value):
"" "Utility to ensure a value is positive" "

If value < 0:
Raise ValueError, \
"Attribute value (%s) must be positive"% value
Else
return value

Def earnings (self):
"" "Abstract method; Derived classes must override "" "

Raise Notimplementederror, "cannot called abstract method"

Class Boss (Employee):
"" "Boss class, inherits from Employee" ""

def __init__ (self, A, last, salary):
"" "Boss constructor, takes and last names and salary" "

Employee.__init__ (self, A, last)
Self.weeklysalary = self._checkpositive (float (salary))

Def earnings (self):
"" "Compute the Boss ' s Pay" "

Return self.weeklysalary

def __str__ (self):
"" "String representation of the Boss" ""

Return "%17s:%s"% ("Boss", employee.__str__ (self))

Class Commissionworker (Employee):
"" "Commissionworker class, inherits from Employee" "

def __init__ (self, A, last, salary, commission, quantity):
"" "Commissionworker constructor, takes and last names,
Salary, commission and quantity "" "

Employee.__init__ (self, A, last)
Self.salary = self._checkpositive (float (salary))
Self.commission = self._checkpositive (float (Commission))
self.quantity = self._checkpositive (quantity)

Def earnings (self):
"" "Compute the Commissionworker ' s Pay" "

return self.salary + self.commission * self.quantity

def __str__ (self):
"" "String Representation of Commissionworker" ""

Return "%17s:%s"% ("Commission Worker",
Employee.__str__ (self))

Class Pieceworker (Employee):
"" "Pieceworker class, inherits from Employee" "

def __init__ (self, A, last, wage, quantity):
"" "Pieceworker constructor, takes and last names, wage
Per Piece and quantity "" "

Employee.__init__ (self, A, last)
Self.wageperpiece = self._checkpositive (float (wage))
self.quantity = self._checkpositive (quantity)

Def earnings (self):
"" "Compute Pieceworker ' s Pay" ""

Return self.quantity * self.wageperpiece

def __str__ (self):
"" "String Representation of Pieceworker" ""

Return "%17s:%s"% ("Piece Worker",
Employee.__str__ (self))

Class Hourlyworker (Employee):
"" "Hourlyworker class, inherits from Employee" "

def __init__ (self, A, last, wage, hours):
"" "Hourlyworker constructor, takes and last names,
Wage per hour and hours worked "" "

Employee.__init__ (self, A, last)
Self.wage = self._checkpositive (float (wage))
Self.hours = self._checkpositive (float (hours))

Def earnings (self):
"" "Compute Hourlyworker ' s Pay" ""

If Self.hours <= 40:
Return self.wage * self.hours
Else
return * self.wage + (self.hours-40) * \
Self.wage * 1.5

def __str__ (self):
"" "String Representation of Hourlyworker" ""

Return "%17s:%s"% ("hourly Worker",
Employee.__str__ (self))

# Main Program

# Create List of Employees
Employees = [Boss ("John", "Smith", 800.00),
Commissionworker ("Sue", "Jones", 200.0, 3.0, 150),
Pieceworker ("Bob", "Lewis", 2.5, 200),
Hourlyworker ("Karen", "Price", 13.75, 40)]

# Print Employee and compute earnings
For employee in Employees:
print '%s earned $%.2f '% (employee, employee.earnings ())

The output results are as follows:

Boss:john Smith earned $800.00

Commission worker:sue Jones earned $650.00

Piece Worker:bob Lewis earned $500.00

Hourly Worker:karen Price earned $550.00

I hope this article will help you with your Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.