面向对象进阶和模块

复习上节内容:

# 面向对象
# 类 实例化 对象/实例
# 什么是类? 拥有相同属性和方法的一类事物
# 什么是对象? 类的一个实际表现,给类中的属性填上具体的值,能够使用类中的方法
# 实例化的过程中做了那些事儿?
# 对象 = 类名()
# 首先要创造一个对象
# 被创造出来的这个对象会作为参数传递给__init__方法中的第一个参数
# 调用__init__方法,传递一些初始化参数 —— 初始化方法
# 将初始化之后的对象返回给调用者
# 查看属性和调用方法
# 类名 : 静态属性、类属性
# 对象 : 调用方法(动态属性)、查看对象的属性
# 组合 : 两类事物之间的所有关系,什么有什么的关系
# 继承 :两个类之间的包含关系,什么是什么的关系
# 钻石继承问题
# 经典类 : 在一个子类中寻找方法的时候,从子类到父类先找到的名字会被执行。
# 深度优先就是在经典类中找类的顺序的一种算法
# 新式类 : 广度优先
# py2中,主动继承object类的都是新式类,它的子类也是新式类
# py3中,所有的类都是新式类,都继承object
# super :
# 自己有父类也有的时候,在单继承中super就可以用父类的
# 在多继承中 super遵循mro广度优先顺序
# 派生 :
# 属性
# 方法
# 多态
# python中不需要程序员自己实现多态
# 在其他强数据类型舒颜的面向对象中,我要传递一个参数必须指定这个参数的数据类型
# 但是,往往在这个地方需要传递的不止一种类型
# 建立一个父类,让所有要传递的数据类型都继承这个父类,在参数指定数据类型的时候
# 指定父类名就可以了

# def eat(Animal alex):pass


# 思维导图
# python :基础数据类型 文件处理 函数 模块 面向对象
# 函数
# 函数的定义 :参数和返回值
# 生成器函数
# 递归函数
# 内置函数
# 匿名函数
# 面向对象
# 基础概念
# 定义
# 三大特性 : 封装
# 多态
# 继承

一、封装
# 广义上的封装 :把变量和函数都放在类中
# 狭义上的封装 :把一些变量 或者 方法 隐藏起来,不对外公开
# 公有的 :
# 私有的 : __名字(名字前面加两个双下划线,这个名字就私有了)

举例1:
class Person:
__country = '中国' # 私有的静态属性

print(Person.__country) # AttributeError: type object 'Person' has no attribute '__country'
#执行后报错
# 私有的名字 只能在类的内部使用 不能在类的外部使用

举例2:
class Person:
__country = '中国' # 私有的静态属性

# print(Person.__country) # AttributeError: type object 'Person' has no attribute '__country'
# 私有的名字 只能在类的内部使用 不能在类的外部使用
print(Person.__dict__)
执行结果:
{'__module__': '__main__', '_Person__country': '中国', '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
要想取到中国:
print(Person._Person__country)  # 不能使用上面这种方式去调用私有的变量(虽然也能有结果)
# 如果非要在类的外部调用一个私有的名字,name必须是在私有的名字前面加 _类名__私有的名字

执行结果:中国

举例3:
class Person:
__country = '中国'
Person.__name = 'XXX'
print(Person.__name) # 在类的外部不能第一一个私有变量
print(Person.__dict__)

执行结果:

XXX
{'__module__': '__main__', '_Person__country': '中国', '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None, '__name': 'XXX'}

结论:
# 私有的变量 :
# 在类的内部 如果使用__变量的形式会发生变形,python会自动的为你加上_类名

举例4:

class Person:
__country='中国'
def __init__(self,name,pwd):
self.name=name
self.__pwd=pwd
def login(self):
print(self.__dict__)
if self.name=='alex' and self.__pwd=='alex3714':
print('登录成功')


alex=Person('alex','alex3714')
alex.login()
print(alex.__dict__)
#print(alex.__pwd)  这个执行会报错 AttributeError: 'Person' object has no attribute '__pwd'


执行结果:

{'name': 'alex', '_Person__pwd': 'alex3714'}
登录成功

{'name': 'alex', '_Person__pwd': 'alex3714'}

举例5:

class Person:
def __init__(self):pass
def __制造密码转换(self,inp):
print('eating')
def 注册(self):
inp = input('pwd>>>')
加密之后的密码 = self.__制造密码转换(inp)

# alex3714 --->


总结:
# 静态属性 、 对象属性、 方法(动态属性) 前面加上双下划綫都会变成私有的
# 私有的特点就是只能在类的内部调用,不能在类的外部使用

举例6:(以下两个例子对比)

class Foo:
def __init__(self):
self.func()
def func(self):
print('in Foo')

class Son(Foo):
def func(self):
print('in son')

s = Son()
执行结果:
in son


class Foo:
def __init__(self):
self.__func() # self._Foo__func
def __func(self):
print('in Foo')

class Son(Foo):
def __func(self): # _Son__func
print('in son')

s = Son()
执行结果:
in Foo

print(Foo.__dict__)
print(Son.__dict__)
执行结果:

{'__module__': '__main__', '__init__': <function Foo.__init__ at 0x02193540>, '_Foo__func': <function Foo.__func at 0x021934F8>, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None}
{'__module__': '__main__', '_Son__func': <function Son.__func at 0x021934B0>, '__doc__': None}




猜你喜欢

转载自www.cnblogs.com/lucky-penguin/p/9070099.html
今日推荐