Python Day-抽象,封装(封装.抽象.Python.Day...)
抽象:
- >抽象用于隐藏用户的内部功能。
- >用户仅与该函数的基本实现进行交互,但内部工作已隐藏。
->用户熟悉“函数的作用”,但他们不知道“它的作用”。
->抽象是使用摘要类和摘要方法实现的,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-抽象,封装的详细内容,更多请关注知识资源分享宝库其它相关文章!