- >抽象用于隐藏用户的内部功能。
- >用户仅与该函数的基本实现进行交互,但内部工作已隐藏。
->用户熟悉“函数的作用”,但他们不知道“它的作用”。
->抽象是使用摘要类和摘要方法实现的,abc(抽象基类)模块提供。
一个抽象类是无法实例化的类(即,您无法创建它的对象)。抽象方法不应给出车身。 >示例:1
from abc import *
class demo(abc):
@abstractmethod
def display(self):
print("abcd")
d = demo()
d.display()
>输出:
typeerror: can't instantiate abstract class demo without an implementation for abstract method 'display'
>示例:2
from abc import *
class parent(abc):
@abstractmethod
def study(self):
pass
class child(parent):
def study(self):
print("commerce")
child = child()
child.study()
>输出:
商业
>示例:3
from abc import *
class parent(abc):
@abstractmethod
def study(self):
pass
class child(parent):
def study(self):
print("commerce")
@abstractmethod
def test(self):
pass
class grandchild(child):
def test(self):
pass
child = grandchild()
child.study()
>输出:
商业
封装:
- >保护数据免受意外修改
>示例:
>输出:
100000 100 AttributeError: 'Infosys' object has no attribute '__project_bid'
以上就是Python Day-抽象,封装的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号