在.NET中,为什么我可以访问一个类的实例的私有成员在类中? [英] In .NET, Why Can I Access Private Members of a Class Instance within the Class?

查看:230
本文介绍了在.NET中,为什么我可以访问一个类的实例的私有成员在类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在清洗一些code今天别人写的,我改变了访问修饰符从公开私人一类变量/成员/场。我希望,我用它来重构/返工/检讨的code,它使用这个变量编译器错误一长串。想象一下,我的惊讶,当我没有得到任何错误。检查之后,事实证明这个类的另一个实例可以访问类中声明的另一个实例的私有成员。完全相应稳固。

这是正常的吗?我一直在编码.NET自年初以来,从来没有碰到这个问题,也没有读到它。也许是我无意中发现了它之前,而只是隐隐约约地注意到,继续前进。 谁能解释这behavoir给我?我想知道为什么我能做到这一点。请解释一下,不要只告诉我的规则。我是不是做错了什么?我发现在C#和VB.NET这种行为。在code似乎采取的访问私有变量的能力优势。缺点是程序员创建意大利面的一大板块。

真诚的,

  • 完全糊涂了

 类杰克
    私人_INT作为整数
末级
类Foo
    公共属性值()作为整数
        得到
            返回_INT
        最终获取
        设置(BYVAL值作为整数)
            _INT =值* 2
        结束设定
    高端物业
    私人_INT作为整数
    私人_foo作为富
    私人_jack正如杰克
    私人_fred弗雷德
    公用Sub SetPrivate()
        _foo =新富
        _foo.Value = 4'你希望做什么,因为_INT是私人
        _foo._int = 3'完全出乎意料
        _jack =新杰克
        _jack._int = 3'预计编译错误
        _fred =新弗雷德
        _fred._int = 3'预计编译错误
    结束小组
    私人Fred类
        私人_INT作为整数
    末级
末级
 

解决方案

这是正常。 私人成员都是私有的类,而不是特定的实例。

While cleaning some code today written by someone else, I changed the access modifier from Public to Private on a class variable/member/field. I expected a long list of compiler errors that I use to "refactor/rework/review" the code that used this variable. Imagine my surprise when I didn't get any errors. After reviewing, it turns out that another instance of the Class can access the private members of another instance declared within the Class. Totally unexcepted.

Is this normal? I been coding in .NET since the beginning and never ran into this issue, nor read about it. I may have stumbled onto it before, but only "vaguely noticed" and move on. Can anyone explain this behavoir to me? I would like to know the "why" I can do this. Please explain, don't just tell me the rule. Am I doing something wrong? I found this behavior in both C# and VB.NET. The code seems to take advantage of the ability to access private variables. The disadvantage is the programmer created a big plate of Spaghetti.

Sincerely,

  • Totally Confused

Class Jack
    Private _int As Integer
End Class
Class Foo
    Public Property Value() As Integer
        Get
            Return _int
        End Get
        Set(ByVal value As Integer)
            _int = value * 2
        End Set
    End Property
    Private _int As Integer
    Private _foo As Foo
    Private _jack As Jack
    Private _fred As Fred
    Public Sub SetPrivate()
        _foo = New Foo
        _foo.Value = 4  'what you would expect to do because _int is private
        _foo._int = 3   'TOTALLY UNEXPECTED
        _jack = New Jack
        '_jack._int = 3 'expected compile error 
        _fred = New Fred
        '_fred._int = 3 'expected compile error 
    End Sub
    Private Class Fred
        Private _int As Integer
    End Class
End Class

解决方案

This is "normal". Private members are private to the class, not to the particular instance.

这篇关于在.NET中,为什么我可以访问一个类的实例的私有成员在类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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