Python类和函数(私有变量、私有方法、变量作用域、继承和多态)
发布日期:2021-05-14 22:00:54 浏览次数:13 分类:精选文章

本文共 3145 字,大约阅读时间需要 10 分钟。

Python���

������Python������������������������������������

���Python������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���Pub���
```pythonclass Pub(): _name = 'protected���������������' __info = '���������������������'
def _func(self):    print("������������protected���������������")    print(self._name)    def __func2(self):    print('���������������������������������')    def get(self):    return self.__info

���������������:

```pythonp = Pub()p._name = 'changed' # ���������print(p._name) # protected���������������# print(p.__info) # ���������'Pub' object has no attribute '__info'p._func() # ������������protected���������������# p.__func2() # ���������'Pub' object has no attribute '__func2'print(p._Pub__info) # ���������������������print(p._Pub__func2()) # ���������������������������������

���������

- ������������������������������������������������������������������������������- ���������������������������������������������������

���������������������

1. ���������������������������������������������������

2. ���������������������������������������������������������������������������������

3. ������������������������������������������������������������������������������������������������������isinstance()���������������������������

���������������������

- ��������������������������������������� - ���������������������������������������������������������������
���������
```pythonclass Animal: pass

class Dog(Animal):pass

���������������������

```pythona = Animal() # Animal������b = Dog() # Dog������c = list() # list������print(isinstance(b, Animal)) # Trueprint(isinstance(a, Dog)) # Falseprint(isinstance(b, Dog)) # True

���������������������������������������������

������������������������������������

1. ������������������������������������������������������������������������

���������
```pythondef func(): a = 1 _b = 2 __c = 3 print(a, _b, __c)
def func2():    _b = 5    __c = 6    print(_b, __c)

func2()func() # ������: 1 2 3 5 6

���������������������������������������������������������

2. ������������������������������������������������

���������������������������������������������������������������������������������������������
���������
```pythonclass A: name = 'wang' _age = 22 __sex = 'men' a = A()print(a.name) # wang# a.name = 'liu' ���������������������������������������������������a.nameprint(a._age) # 22print(a.__sex) # ���������������������__sex������print(a._A__sex) # ������������������������������������������������������������������������������������

3. ������������������nonlocal���������������������

- ��������������������������������������������������������������������� - ������������������������������������������������������ - ������������������������������������������������������������
���������
```pythondef outer(): x = 10 def inner(): print(x) x += 1 print("inner:', x) inner()

outer()

上一篇:Python基础知识总结(常见问题与知识点)
下一篇:查看系统进程线程最大数

发表评论

最新留言

很好
[***.229.124.182]2025年04月08日 12时12分24秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章