Python 访问修饰符

原文:https://www.studytonight.com/python/access-modifier-python

在大多数面向对象语言中,访问修饰符用于限制对类的变量和函数的访问。大多数语言使用三种类型的访问修饰符,它们是- 私有公共受保护

就像任何其他面向对象编程语言一样,在 python 中使用访问修饰符也可以限制对变量或函数的访问。Python 使用下划线来指定类中特定数据成员和成员函数的访问修饰符。

访问修饰符在保护数据免受未经授权的访问以及防止数据被操纵方面起着重要的作用。当实现继承时,由于不需要的数据从父类转移到子类,数据有被破坏(操纵)的巨大风险。因此,根据需求为不同的数据成员和成员函数提供正确的访问修饰符是非常重要的。


Python:访问修饰符的类型

Python 中一个类有 3 种类型的访问修饰符。这些访问修饰符定义了如何访问类的成员。当然,类的任何成员都可以在同一类的任何成员函数中访问。继续讨论访问修饰符的类型,它们是:

访问修饰符:公共

通过类的对象,可以从类外部访问声明为公共的成员。

访问修饰符:受保护

声明为受保护的成员可以从类外部访问,但只能在子类或子类中从其派生的类中访问。

访问修饰符:私有

这些成员只能从类中访问。不允许外部访问。


该举些例子了

在本节中,我们将为每种类型的访问修饰符提供一些基本的代码示例。

public存取修改子

默认情况下,一个类的所有变量和成员函数在 python 程序中都是public

# defining a class Employee
class Employee:
    # constructor
    def __init__(self, name, sal):
        self.name = name;
        self.sal = sal;

上述代码中类的所有成员变量默认为public,因此我们可以按如下方式访问它们:

>>> emp = Employee("Ironman", 999000);
>>> emp.sal;
999000

protected存取修改子

根据 Python 惯例,在变量名中添加前缀_(单下划线)使其成为protected。是的,不需要额外的关键字。

# defining a class Employee
class Employee:
    # constructor
    def __init__(self, name, sal):
        self._name = name;   # protected attribute 
        self._sal = sal;     # protected attribute

在上面的代码中,我们通过添加一个_(下划线)作为前缀,使类变量名为名为 protected,因此现在我们可以如下访问它们:

>>> emp = Employee("Captain", 10000);
>>> emp._sal;
10000

同样,如果有一个子类扩展了类Employee,那么它也可以访问类Employee的受保护成员变量。让我们举个例子:

# defining a child class
class HR(Employee):

    # member function task
    def task(self):
        print ("We manage Employees")

现在让我们尝试从类HR访问类Employee的受保护成员变量:

>>> hrEmp = HR("Captain", 10000);
>>> hrEmp._sal;
10000
>>> hrEmp.task();
We manage Employees

private存取修改子

而前缀__(双下划线)的添加导致成员变量或函数成为private

# defining class Employee
class Employee:
    def __init__(self, name, sal):
        self.__name = name;    # private attribute 
        self.__sal = sal;      # private attribute

如果我们想访问私有成员变量,我们会得到一个错误。

>>> emp = Employee("Bill", 10000);
>>> emp.__sal;

属性错误:“员工”对象没有属性“__sal”


在一个例子中

现在,我们已经在单独的示例中看到了每个访问修饰符,现在,让我们将迄今为止所学的全部内容合并到一个示例中:

# define parent class Company
class Company:
    # constructor
    def __init__(self, name, proj):
        self.name = name      # name(name of company) is public
        self._proj = proj     # proj(current project) is protected

    # public function to show the details
    def show(self):
        print("The code of the company is = ",self.ccode)

# define child class Emp
class Emp(Company):
    # constructor
    def __init__(self, eName, sal, cName, proj):
        # calling parent class constructor
        Company.__init__(self, cName, proj)
        self.name = eName   # public member variable
        self.__sal = sal    # private member variable

    # public function to show salary details
    def show_sal(self):
        print("The salary of ",self.name," is ",self.__sal,)

# creating instance of Company class
c = Company("Stark Industries", "Mark 4")
# creating instance of Employee class
e = Emp("Steve", 9999999, c.name, c._proj)

print("Welcome to ", c.name)
print("Here ", e.name," is working on ",e._proj)

# only the instance itself can change the __sal variable
# and to show the value we have created a public function show_sal()
e.show_sal()

现在上面的代码显示了publicprivateprotected成员变量和方法的正确用法。你可以试着在运行程序时改变一些东西,看看这些改变会导致什么样的错误。