Python - 继承

Python - 继承 首页 / Python3入门教程 / Python - 继承

继承是面向对象范例的重要方面。继承为程序提供了代码可重用性,因为无涯教程可以使用现有的类来创建新类,而不必从头开始创建它。

在继承中,子类获取属性,并可以访问父类中定义的所有数据成员和函数。子类也可以将其特定实现提供给父类的函数。

在python中,派生类可以通过仅在派生类名称后面的括号中提及基类来继承基类。请考虑以下语法,以将基类继承到派生类中。

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

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

链接:https://www.learnfk.comhttps://www.learnfk.com/python3/inheritance-in-python.html

来源:LearnFk无涯教程网

class derive-class(<base class 1>, <base class 2>, ..... <base class n>):
	<class - suite> 
class Animal:
    def speak(self):
        print("Animal Speaking")
#子类 Dog 继承基类 Animal
class Dog(Animal):
    def bark(self):
        print("dog barking")
d = Dog()
d.bark()
d.speak()

输出:

dog barking
Animal Speaking

多层继承

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

无涯教程网

Python Inheritance

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

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

示例

class Animal:
    def speak(self):
        print("Animal Speaking")
#子类 Dog 继承基类 Animal
class Dog(Animal):
    def bark(self):
        print("dog barking")
#子类 Dogchild 继承另一个子类 Dog
class DogChild(Dog):
    def eat(self):
        print("Eating bread...")
d = DogChild()
d.bark()
d.speak()
d.eat()

输出:

dog barking
Animal Speaking
Eating bread...

多重继承

Python为无涯教程提供了在子类中继承多个基类的灵活性。

Python Inheritance

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

class Base1:
	<class-suite>

class Base2:
	<class-suite>
.
.
.
class BaseN:
	<class-suite>

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

示例

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))

输出:

30
200
0.5

issubclass(s​​ub,sup)方法

issubclass(s​​ub,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))

输出:

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))

输出:

True

方法覆盖

可以在子类中提供父类方法的一些特定实现。当在子类中使用某些特定实现定义父类方法时,该概念称为方法重写。在子类中需要对父类方法进行不同定义的情况下,可能需要执行方法重写。

考虑以下示例,以在python中执行方法覆盖。

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

输出:

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());

输出:

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

数据抽象

抽象是面向对象编程的重要方面。在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()

输出:

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

邱岳的产品手记 -〔邱岳〕

赵成的运维体系管理课 -〔赵成〕

如何设计一个秒杀系统 -〔许令波〕

Java性能调优实战 -〔刘超〕

黄勇的OKR实战笔记 -〔黄勇〕

分布式数据库30讲 -〔王磊〕

手机摄影 -〔@随你们去〕

遗留系统现代化实战 -〔姚琪琳〕

给程序员的写作课 -〔高磊〕

好记忆不如烂笔头。留下您的足迹吧 :)