受保护的成员Protected Members

受保护的成员本身不提供任何可扩展性,但它们可以通过子类化使可扩展性更强大。Protected members by themselves do not provide any extensibility, but they can make extensibility through subclassing more powerful. 它们可用于公开高级自定义选项,而不会毫无必要地使主公共接口复杂化。They can be used to expose advanced customization options without unnecessarily complicating the main public interface.

框架设计者需要小心使用受保护的成员,因为“受保护”一词会给人一种虚假的安全感。Framework designers need to be careful with protected members because the name "protected" can give a false sense of security. 任何人都能够对未密封的类进行子类化并访问受保护的成员,因此所有用于公共成员的防御性编码实践都适用于受保护的成员。Anyone is able to subclass an unsealed class and access protected members, and so all the same defensive coding practices used for public members apply to protected members.

✔️考虑使用受保护的成员进行高级自定义。✔️ CONSIDER using protected members for advanced customization.

✔️将未密封类中的受保护成员视为公共的,目的是为了实现安全、文档和兼容性分析。✔️ DO treat protected members in unsealed classes as public for the purpose of security, documentation, and compatibility analysis.

任何人都可以从类继承并访问受保护的成员。Anyone can inherit from a class and access the protected members.

部分©2005,2009 Microsoft Corporation。保留所有权利。Portions © 2005, 2009 Microsoft Corporation. All rights reserved.

*在 Pearson Education, Inc. 授权下,由 Addison-Wesley Professional 作为 Microsoft Windows 开发系列的一部分再版自 Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, 2nd Edition(Framework 设计准则:可重用 .NET 库的约定、惯例和模式第 2 版),由 Krzysztof Cwalina 和 Brad Abrams 发布于 2008 年 10 月 22 日。Reprinted by permission of Pearson Education, Inc. from Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, 2nd Edition by Krzysztof Cwalina and Brad Abrams, published Oct 22, 2008 by Addison-Wesley Professional as part of the Microsoft Windows Development Series.

另请参阅See also