DAY 13 Impacts Blue Bridge Cup - Inheritance of Python Foundation 13python

Posted by paulsiew2 on Fri, 04 Feb 2022 18:51:57 +0100

Inheritance allows us to define a class that inherits all methods and properties from another class. A parent class is an inherited class, also known as a base class. Subclasses are classes that inherit from another class, also known as derived classes.

14.1 Create parent class

Any class can be a parent, so the syntax is the same as creating any other class:
Here you create a class named Person, firstname, and lastname attributes, along with a printname method:

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)


x = Person("Chuan Chuan", "Green hand")
x.printname()

14.2 Create subclasses

To create a class that inherits functionality from another class, send the parent class as a parameter when you create a subclass.
For example, create a class named Student that inherits the properties and methods of the class Person

class Student(Person):
  pass

Note: Use keywords when you don't want to add any other properties or methods to your class.
Now the Student class has the same properties and methods as the Person class. Use the Student class to create the object, then execute the printname method:

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

class Student(Person):
  pass

x = Student("Chuan Chuan", "Green hand")
x.printname()

14.3 Add init() function

So far, we have created a subclass that inherits the properties and methods of the parent class. We want to u init_u () The function is added to the subclass (not the pass keyword).
Note: init() automatically calls this function every time it creates a new object using this class.
For example: init_u () Function added to Student class

class Student(Person):
  def __init__(self, fname, lname):

Add_u Init_u After the () function, the subclass will no longer inherit the u of the parent class Init_u () function. Note: Children's u Init_u The () function overrides the inherited init() function of the parent. In order to keep the parent_u Init_u () Inheritance of functions, adding calls to parent functions_u Init_u ():

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

class Student(Person):
  def __init__(self, fname, lname):
    Person.__init__(self, fname, lname)

x = Student("Chuan Chuan", "Green hand")
x.printname()

14.4 Use the super() function

Python also has a super() function that allows subclasses to inherit all the methods and properties of their parent class:

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)

x = Student("hg", "Green hand")
x.printname()

By using the super() function, you do not have to use the name of the parent element; it automatically inherits methods and attributes from its parent element.

14.5 Add Attributes

Add a property that calls graduationyear to the Student class:

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)
    self.graduationyear = 2021

x = Student("Chuan Chuan", "Green hand")
print(x.graduationyear)

Year 2019 should be a variable and Student passes it to the class when it creates the student object. To do this, add another parameter to the init() function.
Add the year parameter and pass the correct year when the object is created:

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

class Student(Person):
  def __init__(self, fname, lname, year):
    super().__init__(fname, lname)
    self.graduationyear = year

x = Student("Chuan Chuan", "Green hand", 2021)
print(x.graduationyear)

Return as same as 2021

14.6 Add Method

Add a method that calls welcome to the Student class:

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

class Student(Person):
  def __init__(self, fname, lname, year):
    super().__init__(fname, lname)
    self.graduationyear = year

  def welcome(self):
    print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)

x = Student("Chuan Chuan", "Green hand", 2021)
x.welcome()

Topics: Python