抽象:
– >抽象用于隐藏用户的内部功能。
– >用户仅与该函数的基本实现进行交互,但内部工作已隐藏。
->用户熟悉“函数的作用”,但他们不知道“它的作用”。 ->抽象是使用摘要类和摘要方法实现的,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()
登录后复制
>输出:
商业
封装:
封装是指数据(变量)和方法(函数)的捆绑到一个单元(类),同时限制对某些对象的某些详细信息的直接访问。
封装的功能:
– >保护数据免受意外修改
>示例:
332222685688
>输出:
100000 100 AttributeError: 'Infosys' object has no attribute '__project_bid'
登录后复制
以上就是Python Day-抽象,封装的详细内容,更多请关注php中文网其它相关文章!