Python的继承类型

Python的继承类型

继承是指某个特定类能够从另一个类中获取或继承属性,并在需要时使用这些属性。继承具有以下特点:

  • 这是对现实世界中关系的出色表示。
  • 它允许代码重用。它不需要我们重复创建相同的代码。它还允许我们在不修改现有代码的情况下向现有类添加选项。
  • 它具有传递性,意味着如果B从另一个类A继承,属于B的所有子类将直接继承自类A。

示例

# Python program for demonstrating inheritance
# Here, we will create the base class or the Parent class
class Child1:

    # here, we are apply the Constructor
    def __init__(self, name):
        self.name = name

    # now, we will creat a class To get name
    def getName1(self):
        return self.name

    # Now, we will create a class for checking 
    #if this person is student or not
    def isStudent1(self):
        return False

# here, we will create the derived class or the child class
class Student1(Child1):

    # if the child is student, it will return true
    def isStudent1(self):
        return True


# Driver code
# An Object of Child
std = Child1("Jackie")
print(std.getName1(), std.isStudent1())

# An Object of Student
std = Student1("johnny")
print(std.getName1(), std.isStudent1())

输出:

Jackie False
johnny True

Python中的继承类型

继承类型取决于所涉及的子类和父类的数量。Python中有四种可用的继承类型:

单继承

单继承允许派生类继承一个父类的属性,这样可以重用代码并在现有代码中引入额外的功能。

Python的继承类型

示例:

# Python program for demonstrating single inheritance

# Here, we will create the base class or the Parent class
class Parent1:
    def func_1(self):
        print ("This function is defined inside the parent class.")

# now, we will create the Derived class
class Child1(Parent1):
    def func_2(self):
        print ("This function is defined inside the child class.")

# Driver's code
object = Child1()
object.func_1()
object.func_2()

输出:

This function is defined inside the parent class.
This function is defined inside the child class.

多重继承 如果一个类可以由多个基类创建,这种继承被称为多重继承。当存在多重继承时,每个基类中存在的属性都会被传递给派生类。

Python的继承类型

示例:

# Python program for demonstrating multiple inheritance


# Here, we will create the Base class 1
class Mother1:
    mothername1 = ""
    def mother1(self):
        print(self.mothername1)

# Here, we will create the Base class 2
class Father1:
    fathername1 = ""
    def father1(self):
        print(self.fathername1)

# now, we will create the Derived class
class Son1(Mother1, Father1):
    def parents1(self):
        print ("Father name is :", self.fathername1)
        print ("Mother name is :", self.mothername1)

# Driver's code
s1 = Son1()
s1.fathername1 = "Rajesh"
s1.mothername1 = "Shreya"
s1.parents1()

输出:

Father name is : Rajesh
Mother name is : Shreya

多级继承 ,原始类的特征以及由其派生的类的特征都传递给新的类。这类似于一个涉及祖父母和子女之间关系的情况。

Python的继承类型

示例:

# Python program for demonstrating multilevel inheritance

# Here, we will create the Base class 
class Grandfather1:

    def __init__(self, grandfathername1):
        self.grandfathername1 = grandfathername1

# here, we will create the Intermediate class
class Father1(Grandfather1):
    def __init__(self, fathername1, grandfathername1):
        self.fathername1 = fathername1

        # here, we will invoke the constructor of Grandfather class
        Grandfather1.__init__(self, grandfathername1)

# here, we will create the Derived class
class Son1(Father1):
    def __init__(self,sonname1, fathername1, grandfathername1):
        self.sonname1 = sonname1

        # here, we will invoke the constructor of Father class
        Father1.__init__(self, fathername1, grandfathername1)

    def print_name(self):
        print('Grandfather name is :', self.grandfathername1)
        print("Father name is :", self.fathername1)
        print("Son name is :", self.sonname1)

# Driver code
s1 = Son1('John', 'John Jr', 'John Jr Jr')
print (s1.grandfathername1)
s1.print_name()

输出:

John Jr Jr
Grandfather name is : John Jr Jr
Father name is : John Jr
Son name is : John

Hierarchical Inheritance 如果从同一个基类创建了多个派生类,这种继承关系被称为层次继承。在这种情况下,我们有两个基类作为父类,以及两个子类作为派生类。

Python的继承类型

示例:

# Python program for demonstrating Hierarchical inheritance

# Here, we will create the Base class 
class Parent1:
    def func_1(self):
        print ("This function is defined inside the parent class.")

# Derived class1
class Child_1(Parent1):
    def func_2(self):
        print ("This function is defined inside the child 1.")

# Derivied class2
class Child_2(Parent1):
    def func_3(self):
        print ("This function is defined inside the child 2.")

# Driver's code
object1 = Child_1()
object2 = Child_2()
object1.func_1()
object1.func_2()
object2.func_1()
object2.func_3()

输出:

This function is defined inside the parent class.
This function is defined inside the child 1.
This function is defined inside the parent class.
This function is defined inside the child 2.

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程