python 面向对象-访问权限修饰符
1、访问权限修饰符可以用来修饰变量和方法
2、内部调用和外部调用:
比如在类的内部调用实例方法,叫内部调用;在类的外部调用实例化方法,叫外部调用
当然,类变量以及实例变脸也是有内部调用和外部调用
#coding=utf-8 class Student(): name = "" age = 0 sum = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self):#实例方法 self.do_english_homework()#内部调用 print(self.name +" do homework") def do_english_homework(self): print(self.name+ " do english homework") student1 = Student("anson",19) #student1.do_english_homework() student1.do_homework()#外部调用 # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" # anson do english homework # anson do homework
3、安全隐患:直接通过外部调用一些变量,可能造成一些不安全的因素。
比如,给学生打分,如果直接外部访问,修改学生分数,是非常危险的。所以,推荐调用方法去修改对象属性
#coding=utf-8 class Student(): sum = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self):#实例方法 self.do_english_homework() print(self.name +" do homework") def do_english_homework(self): print(self.name+ " do english homework") def marketing(self,score): if score < 0: return else: self.score = score print (self.name+" score :" + str(score)) student1 = Student("anson",19) student1.score = -1 #可以修改,但是没有逻辑限制,很危险 print(student1.__dict__) student1.marketing(60)#可以修改,但是有逻辑限制,python 、Java、都是推荐使用的 # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" # {'age': 19, 'score': -1, 'name': 'anson'} # anson score :60
4、使用访问权限修饰符,限制外部调用直接修改变量:对方法
ps.python 中不用修饰符修饰时,默认都是public 的(python 没有public private 这种权限修饰符),得着么做来限制__funcname()即可表示这个方法不能被外部访问
#coding=utf-8 class Student(): sum = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self):#实例方法 self.do_english_homework() print(self.name +" do homework") def do_english_homework(self): print(self.name+ " do english homework") def __marketing(self,score): if score < 0: return else: self.score = score print (self.name+" score :" + str(score)) student1 = Student("anson",19) student1.__marketing(60)#左边双下划线,表示这个方法不允许外部访问 # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" # Traceback (most recent call last): # File "/Users/anson/Documents/Project/python_ToolCodes/test6.py", line 22, in <module> # student1.__marketing(60) # AttributeError: Student instance has no attribute '__marketing'
5、使用访问权限修饰符,限制外部调用直接修改变量:对变量
student1.__score = -1:还是能去访问,没啥用
#coding=utf-8 class Student(): sum = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self):#实例方法 self.do_english_homework() print(self.name +" do homework") def do_english_homework(self): print(self.name+ " do english homework") def __marketing(self,score): if score < 0: return else: self.score = score print (self.name+" score :" + str(score)) student1 = Student("anson",19) student1.__score = -1 print(student1.__dict__) # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" # {'age': 19, '__score': -1, 'name': 'anson'}
再来:
student1有__score 属性
student2 没有这个属性,如果去读这个属性student2.__score,必然报错
实际上呢,student1利用了python的动态添加特性,给新增了个__score 属性,student2没去新增这个属性,当然就读不出来了。
所以__score 这招,在方法的访问限制上是生效的,但是对变量没啥用。
#coding=utf-8 class Student(): sum = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self):#实例方法 self.do_english_homework() print(self.name +" do homework") def do_english_homework(self): print(self.name+ " do english homework") def __marketing(self,score): if score < 0: return else: self.score = score print (self.name+" score :" + str(score)) student1 = Student("anson",19) student1.__score = -1 print(student1.__dict__) student2 = Student("bob",19) print(student2.__dict__) # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" # {'age': 19, '__score': -1, 'name': 'anson'} # {'age': 19, 'name': 'bob'}
总结,python 其中总的来说好似没发强制限制开发者不能去读私有变量的,开发者用魔术方法还是可以读到,但是,总的来说,靠人治,靠自觉吧