class Customer(object): # Creates the customer class
def __init__(self, customer_id): # Initializes the variables self.customer_id = customer_id # Sets the variable equal to self.variable def display_cart(self): # Defines a function for the class pass # Placeholder class ReturningCustomer(Customer): # Derived class created using a parent class # In this case, the parent class is "Customer" # And the derived class is "ReturningCustomer" def display_order_history(self): # Defines function for derived class pass # Placeholder
So in this case, the derived class, "ReturningCustomer" inherits both the variable (customer_id) and the function (display_cart(self):), while also having its own separate function "display_order_history(self):".
But what if we want the derived class to have the same function name, but a different function? This is called overriding. To override the function from the parent class, say,
display_cart(self):
, in the derived class, you would just define another function in the derived class like this:class ReturningCustomer(Customer): def display_cart(self): #Creates function to overwrite parent function (insert different code here)
And what if you want to use the original function that you already overwrote? Well, you could rewrite it, but what if it's a very complicated and long function? What you would use is define a new function,
def function(self, arguments):
and under that, you would write super(derivedclass, self).function(arguments)
.
No comments:
Post a Comment