python 面向对象 私有属性
Posted minger_lcm
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 面向对象 私有属性相关的知识,希望对你有一定的参考价值。
__init__构造函数
self.name = name # 属性, 实例变量,成员变量,字段
def sayhi()# 方法, 动态属性
私有属性不对外看到 前面加上__
class role(): # 传参数 def __init__(self,name,role,weapon,life_value=100,moneny=15000): # 构造函数 # 实例化过程中做一些类的初始化工作 self.name = name self.role = role self.weapon = weapon self.life_value = life_value self.moneny = moneny self.__heart = ‘normal‘ # 私有属性 def shot(self): # 类的方法,功能(动态属性) print("shotting") def got_shot(self): print("on,%s got shoot..." % self.name) def buy_gun(self,gun_name): print("%s just bought %s" %(self.name,gun_name)) self.weapon = gun_name b1 = role(‘mike‘,‘police‘,‘AK47‘) print(b1.name) print(b1.__heart) # 私有属性不对外公开 ‘‘‘ mike Traceback (most recent call last): File "C:/Users/Administrator/PycharmProjects/ss/SSS/day6/私有属性1.py", line 32, in <module> print(b1.__heart) AttributeError: ‘role‘ object has no attribute ‘__heart‘ ‘‘‘
在内部方法使用
class role(): # 传参数 def __init__(self,name,role,weapon,life_value=100,moneny=15000): # 构造函数 # 实例化过程中做一些类的初始化工作 self.name = name self.role = role self.weapon = weapon self.life_value = life_value self.moneny = moneny self.__heart = ‘normal‘ # 私有属性 def shot(self): # 类的方法,功能(动态属性) print("shotting") print("私有属性", self.__heart) self.__heart = ‘dddd‘ print("私有属性", self.__heart) def got_shot(self): print("on,%s got shoot..." % self.name) def buy_gun(self,gun_name): print("%s just bought %s" %(self.name,gun_name)) self.weapon = gun_name b1 = role(‘mike‘,‘police‘,‘AK47‘) print(b1.shot()) ‘‘‘ shotting 私有属性 normal 私有属性 dddd None ‘‘‘
如果想强制对外能访问
class role(): # 传参数 def __init__(self,name,role,weapon,life_value=100,moneny=15000): # 构造函数 # 实例化过程中做一些类的初始化工作 self.name = name self.role = role self.weapon = weapon self.life_value = life_value self.moneny = moneny self.__heart = ‘normal‘ # 私有属性 def shot(self): # 类的方法,功能(动态属性) print("shotting") print("私有属性", self.__heart) self.__heart = ‘dddd‘ print("私有属性", self.__heart) # 定义一个方法用来对外访问 def show_heart(self): return self.__heart def got_shot(self): print("on,%s got shoot..." % self.name) def buy_gun(self,gun_name): print("%s just bought %s" %(self.name,gun_name)) self.weapon = gun_name b1 = role(‘mike‘,‘police‘,‘AK47‘) print(b1.show_heart()) # normal
强制访问私有属性
class role(): # 传参数 def __init__(self,name,role,weapon,life_value=100,moneny=15000): # 构造函数 # 实例化过程中做一些类的初始化工作 self.name = name self.role = role self.weapon = weapon self.life_value = life_value self.moneny = moneny self.__heart = ‘normal‘ # 私有属性 def shot(self): # 类的方法,功能(动态属性) print("shotting") def got_shot(self): print("on,%s got shoot..." % self.name) def buy_gun(self,gun_name): print("%s just bought %s" %(self.name,gun_name)) self.weapon = gun_name b1 = role(‘mike‘,‘police‘,‘AK47‘) # 强制访问私有属性 print(b1._role__heart) b1._role__heart # normal
私有属性 继承重构方法不能访问 父类的私有属性
class Foo(object): def __init__(self, name): self.__name = name def f1(self): print(self.__name) class Bar(Foo): def f2(self): print(self.__name) obj = Bar("mike") obj.f2() ‘‘‘ Traceback (most recent call last): File "C:/Users/Administrator.QH-20170325TNQR/PycharmProjects/SSSSS/SS/day7/S13 面向对象/私有属性 继承1.py", line 21, in <module> obj.f2() File "C:/Users/Administrator.QH-20170325TNQR/PycharmProjects/SSSSS/SS/day7/S13 面向对象/私有属性 继承1.py", line 18, in f2 print(self.__name) AttributeErr
只有父类自己才能访问
class Foo(object): def __init__(self, name): self.__name = name def f1(self): print(self.__name) class Bar(Foo): def f2(self): print(self.__name) obj = Bar("mike") obj.f1() # mike
以上是关于python 面向对象 私有属性的主要内容,如果未能解决你的问题,请参考以下文章