py3中怎么用? py2中怎么用?
在单继承中执行父类的的同名方法的时候怎么用
super方法 和mro 方法的关系 是什么
# super().func() py3
# super(D,self).func() py3
# super(D,self).func() py2中必须这么写
# class A:
# def func(self):
# print('A')
# class B(A):
# def func(self):
# super().func()
# print('B')
# class C(A):
# def func(self):
# super().func()
# print('C')
# class D(B,C):
# def func(self):
# super().func() #super 是按照mro 顺序来寻找 当前类的下一个类的
# # super(D,self).func() 和上面作用一样
# print('D')
# D().func()
#
# A
# C
# B
# D
# 在py3中 不需要传参数, 自动就帮我们寻找当前类的mro 顺序下一个类中的同名方法
# py2中的新式类 需要我们传递参数super(子类的名字,子类的对象).函数名()
# 这样才能够帮我们调用这个子类的mro顺序的下一个类的方法
# 在py2经典类中 并不支持使用super来找下一个类
#
# 在D类 中找 super 的func ,那么还可以这样写 super().func()
# 也可以这样写, super(D,self).func() (并且在py2中必须这样写)
#
# 在单继承类的程序中,super 就是找父类
class User:
def __init__(self,name):
self.name=name
class VIPUser(User):
def __init__(self,name,level,strat_date,end_date):
# User.__init__(self,name)
super().__init__(name)
# super(VIPUser,self).__init__(name)
self.level=level
self.strat_date=strat_date
self.end_date=end_date
太白=VIPUser('太白',6,'2019-01-01','2020-01-01')
print(太白.__dict__)
就是把属性或者方法装起来
把属性或者方法装起来, 外面不能直接调用,用通过类名来调用
把属性或者方法藏起来, 外面不能直接调用,只能在内部偷偷调用
(所有的私有化 都是为了让用户不在外部调用类中的缪某个名字)
想找 也可以找到
外部使用
print(User._User__Country) 可以调用到
print(User.__Country) 调用不到
给一个名字加上了双下划线的时候 ,这个名字就变成了私有的了 __pwd 私有的实例变量. 私有的对象属性
# 不让看不让改
# 只让看 不让改
# # 可以看可以改,只能按照规则改
# class User:
# def __init__(self,name,passwd):
# self.usr=name
# self.__pwd=passwd #私有的实例变量,/私有对象属性
# alex=User('alex',"xiaowu")
#
#
# print(alex.__pwd) #报错 外部使用不了
# print(alex.pwd) #报错 外部使用不了
class User:
def __init__(self,name,passwd):
self.usr=name
self.__pwd=passwd #私有的实例变量,/私有对象属性
def get_pwd(self): #表示用户只能看 不能改 私有+某个get方法
return self.__pwd
def change__pwd(self): #表示用户必须调用我们的自定义方法进行变量修改 私有+change方法 实现
pass
alex=User('alex',"xiaowu")
# 不让看不让改
# 只让看 不让改
# # 可以看可以改,只能按照规则改
# class User:
# __Country ='china' #私有的静态变量
# def func(self):
# print(User.__Country)
#
# # print(User.Country) #报错
# # print(User.__Country) #报错
# User().func()
#
# 私有的静态变量
# 私有的实例变量
# 私有的绑定方法
import hashlib
class User:
def __init__(self,name,passwd):
self.usr=name
self.__pwd=passwd #私有的
def __get_md5(self): #私有的方法
md5=hashlib.md5(self.usr.encode('utf-8')) #usr 用户名做盐
md5.update(self.__pwd.encode('utf-8'))
return md5.hexdigest()
def getpwd(self):
return self.__get_md5()
alex=User('alex','xiaowu')
# print(alex.__get_md5()) #报错 调用不到
print(alex.getpwd())
能不能在类内部使用?
能不能在类的外部使用?
能不能在类的子类使用?
_User__Course
类内部的时候 self.__Course 他自己会拼接类名
类外部 print(User._User__Course)
类中变量的级别 那些是python 支持的
public private
python 私有 公用 支持 保护不支持
把一个方法伪装成属性了来使用
class Yuan:
def __init__(self,r):
self.r=r
@property
def area(self):
return 3.14*self.r**2
a=Yuan(3)
print(a.r)
print(a.area)
import time
class Person:
def __init__(self,name,birth):
self.name=name
self.birth=birth
@property
def age(self):
return time.localtime().tm_year-self.birth
小屋=Person('小屋',1999)
print(小屋.age)
class User:
def __init__(self,name,pwd):
self.user=name
self.__pwd=pwd
@property
def get_pwd(self):
return self.__pwd
小屋=User("小屋",'沙雕沙雕')
print(小屋.get_pwd)
# 沙雕沙雕
class Goods:
discount=0.8
def __init__(self,name,aprice):
self.name=name
self.__price=aprice
@property
def price(self):
return self.__price* self.discount
小屋=Goods("小屋酱",100)
print(小屋.price)
# 80.0
class Goods:
discount=0.8
def __init__(self,name,aprice):
self.name=name
self.__price=aprice
@property
def price(self):
return self.__price* self.discount
@price.setter
def price(self,new):
if isinstance(new,int):
self.__price=new
小屋=Goods("小屋酱",100)
print(小屋.price) #调用的是被@property装饰的 price
小屋.price=200 #调用的是被setter装饰的 price
print(小屋.price)
用字符串数据的名字,来操作这个名字对应的函数, 实例变量 绑定方法 各种方法
原文:https://www.cnblogs.com/xueba/p/12487633.html