Payroll.add(HourlyEmployee( 'David', 'Wilson', 150, 100)) Payroll.add(HourlyEmployee( 'Jenifer', 'Smith', 200, 50)) Payroll.add(FulltimeEmployee( 'Jane', 'Doe', 6500)) Payroll.add(FulltimeEmployee( 'John', 'Doe', 6000)) from fulltimeemployee import FulltimeEmployeeįrom hourlyemployee import HourlyEmployee The following app.py uses the FulltimeEmployee, HourlyEmployee, and Payroll classes to print out the payroll of five employees. Self.last_name = def full_name (self): return f" ")Ĭode language: Python ( python ) The main program The following defines the Employee abstract class: from abc import ABC, abstractmethodĬlass Employee (ABC): def _init_ (self, first_name, last_name): The method for calculating salary should be an abstract method. In addition, it should have a method that calculates salary. The Employee class should have a property that returns the full name of an employee. The Employee class should be an abstract class because there’re only full-time employees and hourly employees, no general employees exist. The Employee class represents an employee, either full-time or hourly. To structure the program, we’ll use modules, where each class is placed in a separate module (or file). To model the payroll program in an object-oriented way, you may come up with the following classes: Employee, FulltimeEmployee, HourlyEmployee, and Payroll. The payroll program needs to print out a payroll that includes employee names and their monthly salaries. The full-time employees get a fixed salary while the hourly employees get paid by hourly wages for their services. The company has two groups of employees: full-time employees and hourly employees. Suppose that you need to develop a payroll program for a company. To define an abstract method, you use the decorator: from abc import ABC, abstractmethodĬlass AbstractClassName (ABC): def abstract_method_name (self): pass Code language: Python ( python ) Python abstract class example The abc module provides you with the infrastructure for defining abstract base classes.Ĭlass AbstractClassName (ABC): pass Code language: Python ( python ) To define an abstract class, you use the abc (abstract base class) module. But it does offer a module that allows you to define abstract classes. Python doesn’t directly support abstract classes. An abstract class may or may not include abstract methods. Similarly, an abstract method is an method without an implementation. Typically, you use an abstract class to create a blueprint for other classes. However, you can create classes that inherit from an abstract class. In object-oriented programming, an abstract class is a class that cannot be instantiated. Summary: in this tutorial, you’ll learn about Python Abstract classes and how to use it to create a blueprint for other classes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |