python 抽象类
介绍
抽象是面向对象编程的四大支柱之一,其他三个是封装、继承和多态。抽象允许人们专注于对象的本质方面,而忽略不必要的细节。面向对象编程最重要的特征之一是它对现实生活场景进行建模。面向对象程序的功能与现实世界中发生的事情完全相同,这可以使用抽象的概念非常有效地演示。
强烈推荐
海量程序代码,编程资源,无论你是小白还是大神研究借鉴别人优秀的源码产品学习成熟的专业技术强势助力帮你提高技巧与技能。在此处获取,给你一个全面升级的机会。只有你更值钱,才能更赚钱
如果你是初级程序员可以研究别人的代码提高技术,如果你喜欢搞网盟或者外包,可以让你快速建站,还等什么赶快关注吧,我们会持续输出相关资源
代码示例
假设有一个名为Shape的类。Shape 是一种抽象实体。我们不能独立地想象一个形状。形状可以采用不同的形式,如圆形、矩形、三角形、多边形等。因此,在不知道形状所代表的形式的情况下,不可能对形状执行任何确定的操作,如求面积或周长。
我们可以使用ABC元类和ABC模块中的@AbstractMethod修饰器来创建一个名为Shape的抽象基类,如下所示:
#!/usr/bin/python
# -*- coding: utf-8 -*-
class Shape(ABC):
def __init__(self):
print 'I am a Shape'
@abstractmethod
def area(self):
pass
@abstractmethod
def circumference(self):
pass
请注意,area()和circumference()方法是抽象方法。因此,它们在Shape类中没有实现。
现在,如果我们尝试创建Shape类的实例,Python将抛出TypeError异常,如下所示:
s = Shape()
TypeError: Can’t instantiate abstract class Shape with abstract methods area, circumference
这清楚地表明我们不能创建抽象类的实例。但是我们需要从抽象基类派生类,并为抽象方法提供实现。
例如,在我们的示例中,我们可以从Shape类派生类Circle和Rectangle,并实现area()和circumference()方法,如下所示:
class Circle(Shape):
def __init__(self):
super().__init__()
print("I am a Circle")
def area(self):
radius = int(input("Enter the radius: "))
print("Area of the Circle is %.2f." % ((22.0 / 7.0) * radius * radius))
def circumference(self):
radius = int(input("Enter the radius: "))
print("Circumference of the Circle is %.2f." % (2 * (22.0 / 7.0) * radius))
class Rectangle(Shape):
def __init__(self):
super().__init__()
print("I am a Rectangle")
def area(self):
length = int(input("Enter the length: "))
breadth = int(input("Enter the breadth: "))
print("Area of the Rectangle is %d." % (length * breadth))
def circumference(self):
length = int(input("Enter the length: "))
breadth = int(input("Enter the breadth: "))
print("Circumference of the Rectangle is %d." % (2 * (length + breadth)))
现在,我们可以实例化Circle和Rectangle类,并调用area()和circumference()方法。
下面是完整的Python代码,
# Demonstration of Abstraction
from abc import ABC, abstractmethod
class Shape(ABC):
def __init__(self):
print("I am a Shape")
@abstractmethod
def area(self):
pass
@abstractmethod
def circumference(self):
pass
class Circle(Shape):
def __init__(self):
super().__init__()
print("I am a Circle")
def area(self):
radius = int(input("Enter the radius: "))
print("Area of the Circle is %.2f." % ((22.0 / 7.0) * radius * radius))
def circumference(self):
radius = int(input("Enter the radius: "))
print("Circumference of the Circle is %.2f." % (2 * (22.0 / 7.0) * radius))
class Rectangle(Shape):
def __init__(self):
super().__init__()
print("I am a Rectangle")
def area(self):
length = int(input("Enter the length: "))
breadth = int(input("Enter the breadth: "))
print("Area of the Rectangle is %d." % (length * breadth))
def circumference(self):
length = int(input("Enter the length: "))
breadth = int(input("Enter the breadth: "))
print("Circumference of the Rectangle is %d." % (2 * (length + breadth)))
if __name__ == "__main__":
s = Shape()
#s.area()
#s.circumference()
c = Circle()
c.area()
c.circumference()
r = Rectangle()
r.area()
r.circumference()
另请注意,如果派生类没有实现其抽象基类的所有方法,则该派生类也将成为抽象的,并且无法实例化。在这种情况下,我们需要从派生类派生一个类,并在该类中实现抽象方法。
下面的程序演示了这一点,其中类B是抽象的,因为它没有实现其抽象基类A的test2()方法:
from abc import ABC,abstractmethod
class A(ABC):
@abstractmethod
def test1(self):
pass
@abstractmethod
def test2(self):
pass
class B(A):
def test1(self):
print("Hello")
class C(B):
def test2(self):
print("Welcome")
obj = C()
obj.test1()
obj.test2()
结论
我希望上面的讨论有助于读者理解Python中实现的抽象概念。
常见问题FAQ
- 程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
- 请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!