确实不仅有 3 种访问类型,在不同的语言中还有更多。
例如:
-
public – 使用此访问修饰符定义的类或其成员可以从任何地方公开访问,甚至可以从类范围之外访问。
-
private – 使用此关键字的类成员将在类本身内访问。它通过类实例的引用保护成员免受外部类访问。
-
protected – 与 private 相同,但允许子类访问受保护的超类成员。
-
abstract – 此关键字只能用于 PHP 类及其函数。为了包含抽象函数,PHP 类应该是一个抽象类。
-
final - 防止子类覆盖用 final 关键字定义的超类成员。
-
internal - 可以在包含其声明的程序内访问,也可以在同一程序集级别内访问,但不能从另一个程序集访问。
-
受保护的内部 - 受保护和内部的访问级别相同。它可以访问同一程序集和同一类中的任何位置,也可以访问从同一类继承的类
其中一些适用于类,一些适用于函数,另一些适用于变量。
因此,在大多数语言中,所有类成员都以 public 访问类型声明(例如 Java 除外)。
回到主要问题。
所有这些访问修饰符的应用都是为了方便组件的封装。
在 C++ 中使用此类访问修饰符的简单示例 (taken from here):
#include <iostream>
#include<conio.h>
using std::cout;
using std::endl;
struct B { // default access modifier inside struct is public
void set_n(int v) { n = v; }
void f() { cout << "B::f" << endl; }
protected:
int m, n; // B::m, B::n are protected
private:
int x;
};
struct D : B {
using B::m; // D::m is public
int get_n() { return n; } // B::n is accessible here, but not outside
// int get_x() { return x; } // ERROR, B::x is inaccessible here
private:
using B::f; // D::f is private
};
int main() {
D d;
// d.x = 2; // ERROR, private
// d.n = 2; // ERROR, protected
d.m = 2; // protected B::m is accessible as D::m
d.set_n(2); // calls B::set_n(int)
cout << d.get_n() << endl; // output: 2
// d.f(); // ERROR, B::f is inaccessible as D::f
B& b = d; // b references d and "views" it as being type B
// b.x = 3; // ERROR, private
// b.n = 3; // ERROR, protected
// b.m = 3; // ERROR, B::m is protected
b.set_n(3); // calls B::set_n(int)
// cout << b.get_n(); // ERROR, 'struct B' has no member named 'get_n'
b.f(); // calls B::f()
return 0;
}
所以,要理解这个修饰符的用途,首先你必须理解面向对象编程的核心原理,尤其是封装范式。
这不是简单的示例代码就能轻松解释的事情。
修饰符只是庞大的 OOP 世界的一小部分。