公有变量和方法
在Python中默认上对象的属性和方法都是公开的,可以通过点操作符( . )来进行访问
例1:
class Student:
def __init__(self,name,score):
self.name = name
self.score = score
def get_information(self):
print("学生:%s,分数为:%s" % (self.name ,self.score)) #类中访问实例变量通过 self.属性名
person = Student("小明","95")
print("修改前的属性名:",person.score) #类外访问实例变量通过 实例名.属性名
person.get_information()
person.score = 0 #通过实例名.属性名修改实例变量的值
print("修改后的属性名:",person.score)
person.get_information()
#上面代码的输出结果为:
#修改前的属性名: 95
#学生:小明,分数为:95
#修改后的属性名: 0
#学生:小明,分数为:0
由上面的代码和输出结果可以看出,在类中定义的非构造方法可以调用类中构造方法中的实例变量的属性,调用方式为self.实例变量属性名,如代码中的self.name和self.score。且可以在类的外部修改类的内部属性
私有变量
要让内部属性不被外部访问可以在属性名称前加 两个下划线( __ )。在python中,实例变量名前如果以__开头,就会变成私有变量,只有内部可以访问,外部不能访问例2:
class Student:
def __init__(self,name,score):
self.__name = name
self.__score = score
def get_information(self):
print("学生:%s,分数为:%s" % (self.__name ,self.__score))
person = Student("小明","95")
print("修改前的属性名:",person.__score) #在类的外部访问私有属性
#上面代码的输出结果为:AttributeError: 'Student' object has no attribute '__score'
1、将变量定义为私有变量,可以确保外部代码不能随意修改对象内部的状态,通过访问限制的保护,代码更加安全
2、若在类外部直接修改私有变量的值,是不会影响到最终实例(私有)变量的值
例3:
class Student:
def __init__(self,name,score):
self.__name = name
self.__score = score
def get_information(self):
print("学生:%s,分数为:%s" % (self.__name ,self.__score))
person = Student("小明","95")
person.get_information()
person.__score = 0 #修改私有变量的值
print("修改后的属性名:",person.__score) #此处访问的__score是上一行修改的__score值,相当于一个类变量
person.get_information() #修改后并不会影响到私有变量的值(伪修改)
#上面代码的输出结果为:
#学生:小明,分数为:95
#修改后的属性名: 0
#学生:小明,分数为:95
在类的外部获取私有变量
在python中,可以通过为类增加的 get_attrs方法来获取类中的私有变量(定义一个get_属性名的方法)例4:
class Student:
def __init__(self,name,score):
self.__name = name
self.__score = score
def get_information(self):
print("学生:%s,分数为:%s" % (self.__name ,self.__score))
def get_score(self): #定义get_attrs方法
return self.__score
person = Student("小明","95")
print("修改前的属性名:",person.get_score()) #通过get_实例变量名的方法来获取私有变量的值
person.get_information()
person.__score = 0
print("修改后的属性名:",person.__score) #此处访问的__score是上一行修改的__score值,相当于一个类变量
person.get_information()
#上面代码的输出结果为:
#修改前的属性名: 95
#学生:小明,分数为:95
#修改后的属性名: 0
#学生:小明,分数为:95
在类的外部可以使用
"实例名._类名__变量名"的方法获取私有变量
例5:
class Student:
def __init__(self,name,score):
self.__name = name
self.__score = score
def get_information(self):
print("学生:%s,分数为:%s" % (self.__name ,self.__score))
person = Student("小明","95")
print("修改前的属性名:",person._Student__score) #通过实例名._类名__私有变量名来访问私有变量
person.get_information()
person.__score = 0 #修改私有变量的值
print("修改后的属性名:",person.__score)
person.get_information()
#上面代码的输出结果为:
#修改前的属性名: 95
#学生:小明,分数为:95
#修改后的属性名: 0
#学生:小明,分数为:95
通过外部修改私有变量的值
在python中,可以通过set__attrs的方法来修改私有变量的值
例6:class Student:
def __init__(self,name,score):
self.__name = name
self.__score = score
def get_information(self):
print("学生:%s,分数为:%s" % (self.__name ,self.__score))
def get_score(self):
return self.__score
def set_score(self,new_score): #通过set_变量名的方法修改实例变量的值
self.__score = new_score
person = Student("小明","95")
print("修改前的属性名:",person.get_score()) #通过get_实例变量名的方法来获取私有变量的值(实例变量)
person.get_information()
person.set_score(0) #通过set_实例变量名的方法来修改私有变量的值(实例变量)
print("修改后的属性名:",person.get_score())
person.get_information()
#上面代码的输出结果为:
#修改前的属性名: 95
#学生:小明,分数为:95
#修改后的属性名: 0
#学生:小明,分数为:0
在python中,通过定义私有变量和对应的set方法可以帮助我们做参数检查,避免传入无效的参数
例7:
class Student:
def __init__(self,name,score):
self.__name = name
self.__score = score
def get_information(self):
print("学生:%s,分数为:%s" % (self.__name ,self.__score))
def get_score(self):
return self.__score
def set_score(self,new_score): #通过set_变量名的方法修改实例变量的值
if 0 <= new_score <= 100:
self.__score = new_score
else:
print("输入的份值错误,")
person = Student("小明","95")
print("修改前的属性名:",person.get_score()) #通过get_实例变量名的方法来获取私有变量的值(实例变量)
person.get_information()
person.set_score(-10) #通过set_实例变量名的方法来修改私有变量的值(实例变量)
person.set_score(10)
print("修改后的属性名:",person.get_score())
person.get_information()
#上面代码的输出结果为:
#修改前的属性名: 95
#学生:小明,分数为:95
#输入的份值错误,
#修改后的属性名: 10
#学生:小明,分数为:10
类的私有方法
类也有私有方法。类的私有方法也是以两个下划线开头,声明该方法为私有方法,不能再类外使用。 私有方法的调用方法为self.方法名例8:
class Student:
def __init__(self):
pass
def __func(self): #定义一个私有方法
print("这是私有方法")
def func(self): #定义一个公有方法
print("现在为公有方法,接下来调用私有方法")
self.__func()
student = Student()
print("通过调用公有方法来间接调用私有方法")
student.func()
#student.__func() #直接调用私有方法报错:AttributeError: 'Student' object has no attribute '__func'