访问修饰符-目的是什么? [英] Access Modifiers - what's the purpose?

查看:119
本文介绍了访问修饰符-目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般对编程还比较陌生,我想知道是否有人可以帮助我理解访问修饰符的目的?我了解以下事实:它们为类和变量等设置了不同的访问级别,但是为什么要限制对它们的访问权限?不允许访问其他事物有什么意义?为什么不只允许访问所有内容?

很抱歉,这是一个愚蠢的问题!

I'm relatively new to programming in general and I was wondering if anyone could help me understand the purpose of access modifiers? I understand the fact that they set different levels of access for classes and variables etc. but why would you want to limit what has access to these? What is the point in not allowing access for different things? why not just allow access for everything?

Sorry if this is a stupid question!

推荐答案

有成千上万的原因,但我会引用此处,然后在其上进行扩展:

There are thousands of reasons, but I'll quote a few from here and then expand on those:

隐藏对象的内部结构可防止用户将组件的内部数据设置为无效或不一致的状态,从而保护其完整性.

Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state.

一种类型强制使用某些不变量(例如,一个人的ID号必须始终为8个字符)非常常见.如果客户对类的每个成员都具有完全访问权限,则无法强制实施这些约束.这是一个具体的例子:

It is very common for a type to enforce certain invariants (e.g., A person's ID number must always be 8 characters long). If a client has full access to every member of a class, then there's no way you can enforce those constraints. Here's a concrete example:

public class Person
{
    public string Id;

    public void SetId(string newId)
    {
        if(newId.Length != 8)
            throw new InvalidArgumentException("newId");

        Id = newId;
    }
}

没有什么阻止我仅访问Id字段并将其设置为我想要的任何内容!我可以这样做:

There's nothing preventing me from just accessing the Id field and setting it to whatever I want! I could just do this:

Person p = new Person();
p.Id = "Invalid Id";

这就是为什么您的私有国家必须为私有.

That's why your private state needs to be private.

封装的好处是可以通过允许开发人员限制软件组件之间的相互依赖关系来降低系统复杂性,从而提高鲁棒性.

A supposed benefit of encapsulation is that it can reduce system complexity, and thus increase robustness, by allowing the developer to limit the inter-dependencies between software component.

说我开发了一个包含40种方法的类-需要35种方法来连接类的内部结构并实现其功能,而其中5种对客户而言实际上很重要.现在,我为您提供该类供您使用-查看其公共接口,您会看到40种方法,其中大多数与您完全无关,并且您问我:这意大利面条到底是什么?代码?"

Say I develop a class which has 40 methods - 35 of which are needed to wire up the internals of the class and to implement its functionality, and 5 of which are actually important to the client. Now I give you this class for you to use - you look at its public interface and you see 40 methods, most of which are completely irrelevant to you, and you ask me "What the hell is this spaghetti of code??"

为了确保明确类型的意图,您限制了与客户端无关的所有成员的访问.

In order to make sure the intent of a type is clear, you restrain the access of any members that are not relevant to the client.

另外,更多的公共成员=更大的公共表面=更多的东西需要测试=难以维护.

Also, more public members = greater public surface = more stuff that needs to be tested = harder to maintain.

根据经验,尝试使您的成员尽可能私密,然后从那里继续前进.例如,以private开头,然后:

As a rule of thumb, try to make your members as private as possible, and then work your way up from there. For example, start with private, and then:

  1. 是否/将需要派生类访问此成员?如果是这样,请升级到protected
  2. 是否/是否其他类需要访问该成员?如果是这样,请升级到public
  1. Do/would derived classes need to access this member? If so, promote to protected
  2. Do/would other classes need to access this member? If so, promote to public

这篇关于访问修饰符-目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆