欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自主访问控制 强制访问控制_访问控制:第1部分

程序员文章站 2024-03-19 14:40:16
...

自主访问控制 强制访问控制

什么是访问控制? (What is access control?)

As the name suggests Access control restricts access to parts of the code from other source files and modules.

顾名思义, 访问控制限制了对其他源文件和模块中部分代码的访问。

为什么要使用访问控制? (Why to use access control?)

This feature enables to hide the implementation details of the code and specifies a preferred interface through which that code can be accessed and used.

此功能可以隐藏代码的实现细节,并指定一个首选接口,通过该接口可以访问和使用该代码。

These specific access levels can be assigned to individual types like classes, structures, and enumerations, etc.

可以将这些特定的访问级别分配给各个类型,例如类,结构和枚举等。

Topics covered are,

涵盖的主题是

  1. Modules and Source Files

    模块和源文件
  2. Access Levels

    访问级别
  3. Access Control on Functions

    功能访问控制
  4. Access Control on Enumerations

    枚举访问控制
  5. Access Control on Subclassing

    子类访问控制
  6. Access Control on Constants, Variables, Properties, and Subscripts

    对常量,变量,属性和下标的访问控制

1.模块和源文件 (1. Modules and Source Files)

To have a clear understanding of access controls it is important to know about these two things because swift’s access control model is based on the concept of modules and source files.

要清楚地了解访问控制,重要的是要了解这两件事,因为swift的访问控制模型基于模块源文件的概念。

  • A module is a single unit of code distribution-a framework or application that is built and shipped as a single unit and that can be imported by another module with Swift’s import keyword.

    模块是代码分发的一个单元-框架或应用程序是作为一个单元构建和交付的,并且可以由另一个模块使用Swift的import关键字导入

  • A source file is a single Swift source code file within a module it contains definitions for multiple types, functions, and so on.

    源文件是模块中的单个Swift源代码文件,其中包含多种类型,函数等的定义。

2.访问级别 (2. Access Levels)

Swift provides five different access levels for entities within the code. These access levels are relative to the source file in which an entity is defined, and also relative to the module that source file belongs to.

Swift为代码中的实体提供了五种不同的访问级别 。 这些访问级别与定义实体的源文件有关,也与源文件所属的模块有关。

  • Open access and Public access enable entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module.

    开放访问公共访问使实体可以在其定义模块的任何源文件中使用,也可以在导入定义模块的另一个模块的源文件中使用。

    Open access applies only to classes and class members, and it differs from public access by allowing code outside the module to subclass and override.

    开放访问仅适用于类和类成员,它与公共访问不同,它允许模块外部的代码继承和覆盖。

  • Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module.

    内部访问使实体可以在其定义模块的任何源文件中使用,但不能在该模块外部的任何源文件中使用。

  • File-private access restricts the use of an entity to its own defining source file. File-private access is used to hide the implementation details of a specific piece of functionality when those details are used within an entire file.

    文件专用访问将实体的使用限制为自己定义的源文件。 在整个文件中使用文件专用访问权时,这些细节将用于隐藏特定功能的实现细节。

  • Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Private access is used to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.

    专用访问将实体的使用限制为封闭的声明以及该声明在同一文件中的扩展名。 当仅在单个声明中使用私有访问权限时,专用访问权限将隐藏这些功能的实现细节。

Open access is the highest access level and private access is the lowest access level.

开放访问是最高访问级别, 私有访问是最低访问级别。

Here,

这里,

  • highest meaning least restrictive

    最高含义最小限制性

  • lowest meaning most restrictive

    最低含义最严格

默认访问级别 (Default Access Levels)

All entities in the code (with a few specific exceptions) have a default access level of internal unless an explicit access level is specified.

除非指定了显式访问级别,否则代码中的所有实体(有一些特定的例外)都具有内部默认访问级别。

As a result, in many cases, an explicit access level is not necessary for the code.

结果,在许多情况下,代码不需要显式的访问级别。

访问控制语法 (Access Control Syntax)

The syntax for defining the access level for an entity is done by placing one of the open, public, internal, fileprivate, or private modifiers at the beginning of the entity’s declaration.

定义实体访问级别的语法是通过在实体声明的开头放置openpublicinternalfileprivateprivate修饰符之一来完成的。

Some examples(some of these examples are used throughout this blog) are,

一些示例(其中一些示例在整个博客中使用)是,

public class SomePublicClass {}internal class SomeInternalClass {}private class SomePrivateClass {}public var somePublicVariable = 0internal let someInternalConstant = 0private func somePrivateFunction() {}

The default access level is internal, as mentioned above. This means that SomeInternalClass and someInternalConstant can be written without an explicit access-level modifier, and will still have an access level of internal:

如上所述, 默认访问级别是internal 。 这意味着SomeInternalClasssomeInternalConstant可以在没有显式访问级别修饰符的情况下编写,并且仍然具有内部访问级别:

class SomeInternalClass {} // implicitly internallet someInternalConstant = 0 // implicitly internal

3.功能访问控制 (3. Access Control on Functions)

The access level must be specified explicitly as part of the function’s definition if the function’s calculated access level doesn’t match the contextual default.

如果函数的计算访问级别与上下文默认值不匹配,则必须将访问级别明确指定为函数定义的一部分。

Consider this scenario, the example below defines a global function called someFunction(), without providing a specific access-level modifier for the function itself.

考虑这种情况,下面的示例定义了一个名为someFunction()的全局函数,而没有为该函数本身提供特定的访问级别修饰符。

The default access level internal does not apply here. In fact, someFunction() won’t compile as written below:

默认的内部访问级别在这里不适用。 实际上, someFunction()不会像下面这样编写:

func someFunction() -> (SomeInternalClass, SomePrivateClass) {
// function implementation
}

The function’s return type is a tuple type composed of the custom classes declared above in the examples. One of these classes is defined as internal, and the other is defined as private. Therefore, the overall access level of the compound tuple type is private.

该函数的返回类型是一个元组类型,由上面的示例中声明的自定义类组成。 这些类中的一个被定义为内部类,另一个被定义为私有类 。 因此,复合元组类型的总体访问级别是私有的。

Because the function’s return type is private, the function’s overall access level is to be specified private in order to be valid.

因为该函数的返回类型为private ,所以该函数的总体访问级别必须指定为private才能有效。

private func someFunction() -> (SomeInternalClass, SomePrivateClass){
// function implementation
}

4.枚举的访问控制 (4. Access Control on Enumerations)

The individual cases of an enumeration automatically belong to the same access level as the enumeration. A different access level cannot be specified for individual enumeration cases.

枚举的个别情况将自动属于与枚举相同的访问级别。 不能为各个枚举情况指定不同的访问级别。

Consider this scenario, In the example below, the CompassPoint enumeration has an explicit access level of the public. The enumeration cases north, south, east, and west therefore also have an access level of the public.

考虑这种情况,在下面的示例中, CompassPoint枚举具有显式的public访问级别。 因此,枚举案例northsoutheastwest也具有公共访问级别。

public enum CompassPoint {
case north
case south
case east
case west
}

原始值和关联值 (Raw Values and Associated Values)

The types used for any raw values or associated values in an enumeration definition must have an access level at least as high as the enumeration’s access level. For example, you can’t use a private type as the raw-value type of an enumeration with an internal access level.

枚举定义中用于任何原始值或关联值的类型的访问级别必须至少与枚举的访问级别一样高。 例如,您不能将私有类型用作具有内部访问级别的枚举的原始值类型。

5.子类访问控制 (5. Access Control on Subclassing)

Insubclass and the class which contains properties, methods, and functions to inherit other classes is called a superclass.To know more about inheritance click here.

类中,包含继承其他类的属性,方法和函数的类称为超类 。要了解有关继承的更多信息,请单击此处

Any class can be inherited that's defined in the same module as the subclass and it applies the same for any class which has the access control open that's defined in a different module.

可以继承在与子类相同的模块中定义的任何类,并且对在不同模块中定义的具有打开的访问控制的任何类都可以应用相同的类。

A subclass can’t have a higher access level than its superclass-for example, you can’t write a public subclass of an internal superclass.

子类不能具有比其父类更高的访问级别-例如,您不能编写内部父类的公共子类。

When it comes to overriding, For classes that are defined in the same module, any class member can be overridden that’s visible in a certain access context. For classes that are defined in another module, any open class member can be overridden.

说到覆盖,对于同一模块中定义的类W¯¯母鸡,任何类成员可以覆盖这在一定访问上下文可见。 对于在另一个模块中定义的类,可以覆盖任何打开的类成员。

An override can make an inherited class member more accessible than its superclass version.

覆盖可以使继承的类成员比其超类版本更易于访问。

In the example below, classA is a public class with a file-private method called someMethod(). classB is a subclass of classA, with a reduced access level of internal.

在下面的示例中, classA是一个公共类,带有一个名为someMethod()文件私有方法。 classB是的一个子类classA ,具有的内部减小的访问级别。

Nonetheless, classB provides an override of someMethod() with an access level of internal, which is higher than the original implementation of someMethod()

尽管如此, classB提供了对someMethod()的重写,其访问级别为internal ,该访问级别高于someMethod()的原始实现。

public class classA {
fileprivate func someMethod() {}
}internal class classB: classA {
override internal func someMethod() {}
}

It’s even valid for a subclass member to call a superclass member that has lower access permissions than the subclass member, as long as the call to the superclass’s member takes place within an allowed access level context (that is, within the same source file as the superclass for a file-private member call, or within the same module as the superclass for an internal member call)

子类成员调用访问权限比子类成员低的超类成员甚至是有效的,只要对超类成员的调用发生在允许的访问级别上下文中(即,与文件私有成员调用的超类,或与内部成员调用的超类在同一模块中)

public class classA {
fileprivate func someMethod() {}
}internal class classB: classA {
override internal func someMethod() {
super.someMethod()
}
}

Because superclass classA and subclass classB are defined in the same source file, it’s valid for the classB implementation of someMethod() to call super.someMethod().

因为超类classA和子类classB是在同一源文件中定义的,所以someMethod()classB实现调用super.someMethod()

6.对常量,变量,属性和下标的访问控制 (6. Access Control on Constants, Variables, Properties, and Subscripts)

If a constant, variable, property, or subscript makes use of a private type, then the constant, variable, property, or subscript must also be marked as private.

如果常量,变量,属性或下标使用私有类型,则常量,变量,属性或下标也必须标记为私有。

private var privateInstance = SomePrivateClass()

So far concepts on Access control on functions, enumerations, subclasses, constants, and variables are covered.

到目前为止,已经涵盖了有关对函数,枚举,子类,常量和变量的访问控制的概念。

翻译自: https://medium.com/ivymobility-developers/access-control-5f88492747b

自主访问控制 强制访问控制

相关标签: python