Python中的访问修饰符:公共、私有和受保护
先决条件: Python中的下划线 (_)、 Python中的私有变量
各种面向对象的语言,如 C++、 Java、 Python控制访问修改,用于限制对类的变量和方法的访问。大多数编程语言具有三种形式的访问修饰符,即类中的Public 、 Protected和Private 。
Python使用“_”符号来确定特定数据成员或类的成员函数的访问控制。 Python中的访问说明符在保护数据免受未经授权的访问和防止数据被利用方面发挥着重要作用。
Python中的类具有三种类型的访问修饰符:
- 公共访问修饰符
- 受保护的访问修饰符
- 私有访问修饰符
公共访问修饰符:
声明为公共的类的成员可以从程序的任何部分轻松访问。默认情况下,类的所有数据成员和成员函数都是公共的。
Python3
# program to illustrate public access modifier in a class
class Geek:
# constructor
def __init__(self, name, age):
# public data members
self.geekName = name
self.geekAge = age
# public member function
def displayAge(self):
# accessing public data member
print("Age: ", self.geekAge)
# creating object of the class
obj = Geek("R2J", 20)
# accessing public data member
print("Name: ", obj.geekName)
# calling public member function of the class
obj.displayAge()
Python3
# program to illustrate protected access modifier in a class
# super class
class Student:
# protected data members
_name = None
_roll = None
_branch = None
# constructor
def __init__(self, name, roll, branch):
self._name = name
self._roll = roll
self._branch = branch
# protected member function
def _displayRollAndBranch(self):
# accessing protected data members
print("Roll: ", self._roll)
print("Branch: ", self._branch)
# derived class
class Geek(Student):
# constructor
def __init__(self, name, roll, branch):
Student.__init__(self, name, roll, branch)
# public member function
def displayDetails(self):
# accessing protected data members of super class
print("Name: ", self._name)
# accessing protected member functions of super class
self._displayRollAndBranch()
# creating objects of the derived class
obj = Geek("R2J", 1706256, "Information Technology")
# calling public member functions of the class
obj.displayDetails()
Python3
# program to illustrate private access modifier in a class
class Geek:
# private members
__name = None
__roll = None
__branch = None
# constructor
def __init__(self, name, roll, branch):
self.__name = name
self.__roll = roll
self.__branch = branch
# private member function
def __displayDetails(self):
# accessing private data members
print("Name: ", self.__name)
print("Roll: ", self.__roll)
print("Branch: ", self.__branch)
# public member function
def accessPrivateFunction(self):
# accessing private member function
self.__displayDetails()
# creating object
obj = Geek("R2J", 1706256, "Information Technology")
# calling public member function of the class
obj.accessPrivateFunction()
Python3
# program to illustrate access modifiers of a class
# super class
class Super:
# public data member
var1 = None
# protected data member
_var2 = None
# private data member
__var3 = None
# constructor
def __init__(self, var1, var2, var3):
self.var1 = var1
self._var2 = var2
self.__var3 = var3
# public member function
def displayPublicMembers(self):
# accessing public data members
print("Public Data Member: ", self.var1)
# protected member function
def _displayProtectedMembers(self):
# accessing protected data members
print("Protected Data Member: ", self._var2)
# private member function
def __displayPrivateMembers(self):
# accessing private data members
print("Private Data Member: ", self.__var3)
# public member function
def accessPrivateMembers(self):
# accessing private member function
self.__displayPrivateMembers()
# derived class
class Sub(Super):
# constructor
def __init__(self, var1, var2, var3):
Super.__init__(self, var1, var2, var3)
# public member function
def accessProtectedMembers(self):
# accessing protected member functions of super class
self._displayProtectedMembers()
# creating objects of the derived class
obj = Sub("Geeks", 4, "Geeks !")
# calling public member functions of the class
obj.displayPublicMembers()
obj.accessProtectedMembers()
obj.accessPrivateMembers()
# Object can access protected member
print("Object is accessing protected member:", obj._var2)
# object can not access private member, so it will generate Attribute error
#print(obj.__var3)
Name: R2J
Age: 20
在上述程序中,geekName 和 geekAge 是公共数据成员,displayAge() 方法是 Geek 类的公共成员函数。 Geek 类的这些数据成员可以从程序中的任何位置访问。
受保护的访问修饰符:
声明为受保护的类的成员只能由从它派生的类访问。类的数据成员通过在该类的数据成员前添加一个下划线“_”符号来声明为受保护。
Python3
# program to illustrate protected access modifier in a class
# super class
class Student:
# protected data members
_name = None
_roll = None
_branch = None
# constructor
def __init__(self, name, roll, branch):
self._name = name
self._roll = roll
self._branch = branch
# protected member function
def _displayRollAndBranch(self):
# accessing protected data members
print("Roll: ", self._roll)
print("Branch: ", self._branch)
# derived class
class Geek(Student):
# constructor
def __init__(self, name, roll, branch):
Student.__init__(self, name, roll, branch)
# public member function
def displayDetails(self):
# accessing protected data members of super class
print("Name: ", self._name)
# accessing protected member functions of super class
self._displayRollAndBranch()
# creating objects of the derived class
obj = Geek("R2J", 1706256, "Information Technology")
# calling public member functions of the class
obj.displayDetails()
Name: R2J
Roll: 1706256
Branch: Information Technology
在上述程序中,_name、_roll 和 _branch 是受保护的数据成员,_displayRollAndBranch() 方法是超类 Student 的受保护方法。 displayDetails()方法是从Student类派生的Geek类的公共成员函数,Geek类中的displayDetails()方法访问Student类的受保护数据成员。
私人访问修饰符:
声明为私有的类的成员只能在类中访问,私有访问修饰符是最安全的访问修饰符。通过在该类的数据成员之前添加双下划线“__”符号将类的数据成员声明为私有。
Python3
# program to illustrate private access modifier in a class
class Geek:
# private members
__name = None
__roll = None
__branch = None
# constructor
def __init__(self, name, roll, branch):
self.__name = name
self.__roll = roll
self.__branch = branch
# private member function
def __displayDetails(self):
# accessing private data members
print("Name: ", self.__name)
print("Roll: ", self.__roll)
print("Branch: ", self.__branch)
# public member function
def accessPrivateFunction(self):
# accessing private member function
self.__displayDetails()
# creating object
obj = Geek("R2J", 1706256, "Information Technology")
# calling public member function of the class
obj.accessPrivateFunction()
Name: R2J
Roll: 1706256
Branch: Information Technology
上述程序中,__name、__roll 和 __branch 是私有成员,__displayDetails() 方法是私有成员函数(只能在类内部访问),accessPrivateFunction() 方法是 Geek 类的公共成员函数,可以从程序中的任何地方访问。 accessPrivateFunction() 方法访问 Geek 类的私有成员。
下面是一个程序来说明Python中类的所有上述三个访问修饰符(public、protected和 private)的使用:
Python3
# program to illustrate access modifiers of a class
# super class
class Super:
# public data member
var1 = None
# protected data member
_var2 = None
# private data member
__var3 = None
# constructor
def __init__(self, var1, var2, var3):
self.var1 = var1
self._var2 = var2
self.__var3 = var3
# public member function
def displayPublicMembers(self):
# accessing public data members
print("Public Data Member: ", self.var1)
# protected member function
def _displayProtectedMembers(self):
# accessing protected data members
print("Protected Data Member: ", self._var2)
# private member function
def __displayPrivateMembers(self):
# accessing private data members
print("Private Data Member: ", self.__var3)
# public member function
def accessPrivateMembers(self):
# accessing private member function
self.__displayPrivateMembers()
# derived class
class Sub(Super):
# constructor
def __init__(self, var1, var2, var3):
Super.__init__(self, var1, var2, var3)
# public member function
def accessProtectedMembers(self):
# accessing protected member functions of super class
self._displayProtectedMembers()
# creating objects of the derived class
obj = Sub("Geeks", 4, "Geeks !")
# calling public member functions of the class
obj.displayPublicMembers()
obj.accessProtectedMembers()
obj.accessPrivateMembers()
# Object can access protected member
print("Object is accessing protected member:", obj._var2)
# object can not access private member, so it will generate Attribute error
#print(obj.__var3)
Public Data Member: Geeks
Protected Data Member: 4
Private Data Member: Geeks !
在上面的程序中,accessProtectedMembers() 方法是Sub类的公共成员函数,访问的是 Super 类的受保护成员函数_displayProtectedMembers() 方法,而 accessPrivateMembers() 方法是 Super 类的公共成员函数,访问 __displayPrivateMembers() 方法,该方法是 Super 类的私有成员函数。