为什么私有字段是类型私有的,而不是实例私有的? [英] Why are private fields private to the type, not the instance?

查看:36
本文介绍了为什么私有字段是类型私有的,而不是实例私有的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C#(和许多其他语言)中,访问相同类型的其他实例的私有字段是完全合法的.例如:

公共类Foo{私人布尔 aBool;public void DoBar(Foo anotherFoo){如果 (anotherFoo.aBool) ...}}

作为 C# 规范(第 3.5.1、3.5.2 节)声明对私有字段的访问是基于类型,而不是实例.我一直在与一位同事讨论这个问题,我们正试图找出它为什么会这样工作的原因(而不是限制对同一实例的访问).

我们能想到的最好的论点是进行相等性检查,其中类可能想要访问私有字段以确定与另一个实例的相等性.还有其他原因吗?或者一些绝对意味着它必须像这样工作或完全不可能的事情的黄金理由?

解决方案

我认为它以这种方式工作的一个原因是因为访问修饰符在编译时起作用.因此,确定给定对象是否也是当前 对象并不容易.例如,考虑以下代码:

公共类Foo{私人 int 栏;public void Baz(Foo 其他){其他.bar = 2;}公共无效嘘(){巴兹(这个);}}

编译器能否确定other实际上是this?并非在所有情况下.有人可能会争辩说这不应该编译,但这意味着我们有一个代码路径,其中一个私有实例成员正确实例是不可访问的,我认为这更糟糕.>

只需要类型级别而不是对象级别的可见性可确保问题易于处理,并让情况看起来应该实际上起作用.

编辑:Daniel Hilgarth 认为这种推理是倒退的观点确实有道理.语言设计者可以创建他们想要的语言,编译器编写者必须遵守它.话虽如此,语言设计者确实有一些动机让编译器编写者更容易完成他们的工作.(尽管在这种情况下,很容易争辩说私有成员可以通过this(隐式或显式)访问).

但是,我认为这会使问题变得比实际情况更令人困惑.如果上述代码不起作用,大多数用户(包括我自己)会发现它不必要地受到限制:毕竟,这是我要访问的我的数据!为什么我必须通过this?

简而言之,我认为我可能夸大了编译器困难"的情况.我真正想表达的是,上述情况似乎是设计师想要的.

In C# (and many other languages) it's perfectly legitimate to access private fields of other instances of the same type. For example:

public class Foo
{
    private bool aBool;

    public void DoBar(Foo anotherFoo)
    {
        if (anotherFoo.aBool) ...
    }
}

As the C# specification (sections 3.5.1, 3.5.2) states access to private fields is on a type, not an instance. I've been discussing this with a colleague and we're trying to come up with a reason why it works like this (rather than restricting access to the same instance).

The best argument we could come up with is for equality checks where the class may want to access private fields to determine equality with another instance. Are there any other reasons? Or some golden reason that absolutely means it must work like this or something would be completely impossible?

解决方案

I think one reason it works this way is because access modifiers work at compile time. As such, determining whether or not a given object is also the current object isn't easy to do. For example, consider this code:

public class Foo
{
    private int bar;

    public void Baz(Foo other)
    {
        other.bar = 2;
    }

    public void Boo()
    {
        Baz(this);
    }
}

Can the compiler necessarily figure out that other is actually this? Not in all cases. One could argue that this just shouldn't compile then, but that means we have a code path where a private instance member of the correct instance isn't accessible, which I think is even worse.

Only requiring type-level rather than object-level visibility ensures that the problem is tractable, as well as making a situation that seems like it should work actually work.

EDIT: Danilel Hilgarth's point that this reasoning is backwards does have merit. Language designers can create the language they want, and compiler writers must conform to it. That being said, language designers do have some incentive to make it easier for compiler writers to do their job. (Though in this case, it's easy enough to argue that private members could then only be accessed via this (either implicitly or explicitly)).

However, I believe that makes the issue more confusing than it needs to be. Most users (myself included) would find it unneccessarily limiting if the above code didn't work: after all, that's my data I'm trying to access! Why should I have to go through this?

In short, I think I may have overstated the case for it being "difficult" for the compiler. What I really meant to get across is that above situation seems like one that the designers would like to have work.

这篇关于为什么私有字段是类型私有的,而不是实例私有的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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