面向对象的继承特性


继承是面向对象范式的一个重要方面。继承为程序提供代码可重用性,因为我们可以使用现有的类来创建新类,而不是从头创建它。


在继承中,子类获取属性并可以访问父类中定义的所有数据成员和函数。子类还可以为父类的功能提供其特定的实现。在本教程的这一部分中,我们将详细讨论继承。


在python中,派生类只需在派生类名后面的括号中提及基类即可继承基类。请考虑以下语法将基类继承到派生类中。


python-inheritance.png

句法

class derived-class(base class):  
    <class-suite>

类可以通过在括号内提及所有类来继承多个类。请考虑以下语法。


句法

class derive-class(<base class 1>, <base class 2>, ..... <base class n>):  
    <class - suite>

例1

class Animal:  
    def speak(self):  
        print("Animal Speaking")  
#child class Dog inherits the base class Animal  
class Dog(Animal):  
    def bark(self):  
        print("dog barking")  
d = Dog()  
d.bark()  
d.speak()

Output:


dog barking
Animal Speaking

Python多级继承

像其他面向对象语言一样,python中可以实现多级继承。当派生类继承另一个派生类时,将对多级继承进行归档。对于多级别继承在python中存档的级别数没有限制。

python-inheritance2.png

下面给出了多级继承的语法。


句法

class class1:  
    <class-suite>   
class class2(class1):  
    <class suite>  
class class3(class2):  
    <class suite>  
.  
.

class Animal:  
    def speak(self):  
        print("Animal Speaking")  
#The child class Dog inherits the base class Animal  
class Dog(Animal):  
    def bark(self):  
        print("dog barking")  
#The child class Dogchild inherits another child class Dog  
class DogChild(Dog):  
    def eat(self):  
        print("Eating bread...")  
d = DogChild()  
d.bark()  
d.speak()  
d.eat()

Output:


dog barking
Animal Speaking
Eating bread...

Python多重继承

Python为我们提供了在子类中继承多个基类的灵活性。


python-inheritance3.png

下面给出了执行多重继承的语法。


句法

class Base1:  
    <class-suite>  
  
class Base2:  
    <class-suite>  
.  
.  
.  
class BaseN:  
    <class-suite>  
  
class Derived(Base1, Base2, ...... BaseN):  
    <class-suite>

Example

class Calculation1:  
    def Summation(self,a,b):  
        return a+b;  
class Calculation2:  
    def Multiplication(self,a,b):  
        return a*b;  
class Derived(Calculation1,Calculation2):  
    def Divide(self,a,b):  
        return a/b;  
d = Derived()  
print(d.Summation(10,20))  
print(d.Multiplication(10,20))  
print(d.Divide(10,20))

Output:


30
200
0.5

issubclass(sub,sup)方法

issubclass(sub,sup)方法用于检查指定类之间的关系。如果第一个类是第二个类的子类,则返回true,否则返回false。


请考虑以下示例。


class Calculation1:  
    def Summation(self,a,b):  
        return a+b;  
class Calculation2:  
    def Multiplication(self,a,b):  
        return a*b;  
class Derived(Calculation1,Calculation2):  
    def Divide(self,a,b):  
        return a/b;  
d = Derived()  
print(issubclass(Derived,Calculation2))  
print(issubclass(Calculation1,Calculation2))

Output:


True
False

isinstance(obj,class)方法

isinstance()方法用于检查对象和类之间的关系。如果第一个参数,即obj是第二个参数的实例,即class,则返回true。


请考虑以下示例。


class Calculation1:  
    def Summation(self,a,b):  
        return a+b;  
class Calculation2:  
    def Multiplication(self,a,b):  
        return a*b;  
class Derived(Calculation1,Calculation2):  
    def Divide(self,a,b):  
        return a/b;  
d = Derived()  
print(isinstance(d,Derived))

Output:

True

方法覆盖

我们可以在子类中提供父类方法的一些特定实现。当在具有某些特定实现的子类中定义父类方法时,该概念称为方法重写。我们可能需要在子类中需要父类方法的不同定义的场景中执行方法重写。


请考虑以下示例在python中执行方法重写。


class Animal:  
    def speak(self):  
        print("speaking")  
class Dog(Animal):  
    def speak(self):  
        print("Barking")  
d = Dog()  
d.speak()

Output:

Barking

真实生活方法覆盖的示例

class Bank:  
    def getroi(self):  
        return 10;  
class SBI(Bank):  
    def getroi(self):  
        return 7;  
  
class ICICI(Bank):  
    def getroi(self):  
        return 8;  
b1 = Bank()  
b2 = SBI()  
b3 = ICICI()  
print("Bank Rate of interest:",b1.getroi());  
print("SBI Rate of interest:",b2.getroi());  
print("ICICI Rate of interest:",b3.getroi());

Output:


Bank Rate of interest: 10
SBI Rate of interest: 7
ICICI Rate of interest: 8

python中的数据抽象

抽象是面向对象编程的一个重要方面。在python中,我们还可以通过添加双下划线(___)作为要隐藏的属性的前缀来执行数据隐藏。在此之后,该属性将不会通过对象在类外部可见。


请考虑以下示例。


class Employee:  
    __count = 0;  
    def __init__(self):  
        Employee.__count = Employee.__count+1  
    def display(self):  
        print("The number of employees",Employee.__count)  
emp = Employee()  
emp2 = Employee()  
try:  
    print(emp.__count)  
finally:  
    emp.display()

Output:


The number of employees 2
AttributeError: 'Employee' object has no attribute '__count'