各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题是《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-抽象,封装》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
本文内容来源于互联网,如有侵权请联系删除。
