可从同一类的另一个实例访问私有字段

发布于 2024-12-22 03:05:18 字数 456 浏览 3 评论 0原文

我没有得到以下信息..我一直认为我只能从声明该字段的类访问私有字段。但是在这种情况下我可以访问它:

class Session
{
    List<client> ListOfClients = new List<client>();

    public void IterateClients(Action<client> action)
    {

    }
}

class client
{
    private int A;

    Session area;

    public void SendData()
    {
        area.IterateClients(delegate(client c)
        {
            c.A = 5; //how come this is accessible?
        });
    }
}

I do not get the following..I always thought I can access private fields only from class which the field was declared in. However in this case I am able to access it:

class Session
{
    List<client> ListOfClients = new List<client>();

    public void IterateClients(Action<client> action)
    {

    }
}

class client
{
    private int A;

    Session area;

    public void SendData()
    {
        area.IterateClients(delegate(client c)
        {
            c.A = 5; //how come this is accessible?
        });
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

感悟人生的甜 2024-12-29 03:05:18

您只能从它所属的类访问私有数据。同一类的两个对象可以访问彼此的私有部分。

合法:

class c1
{
        private int A;

        public void test(c1 c)
        {
        c.A = 5;

        }

}

非法:

class c2
{
  public void test(c1 c)
  {
     c.A = 5;
  }
}

You can only access private data from the CLASS it is a member of. Two objects of the same class can access each other's private parts.

Legal:

class c1
{
        private int A;

        public void test(c1 c)
        {
        c.A = 5;

        }

}

Illegal:

class c2
{
  public void test(c1 c)
  {
     c.A = 5;
  }
}
一个人练习一个人 2024-12-29 03:05:18

这就是它应该工作的方式。

你的理解不正确;私有成员不限于 this 限定符。

That's the way it's supposed to work.

Your understanding was incorrect; private members are not limited to the this qualifier.

纸短情长 2024-12-29 03:05:18

你是对的

我只能从声明该字段的类访问私有字段

因此您要从声明私有变量的类访问私有字段。这在 Java 中也是允许的。

you are right

I can access private fields only from class which the field was declared in

so u're accessing the private field from the class in which which the private variable is declared. this is allowed in Java too.

美煞众生 2024-12-29 03:05:18

client 类中有一个方法。在此方法中,您调用 Session 类的方法。在此调用中,您有一个委托(一种“回调”),该委托在类 client 的上下文中运行,因此可以访问私有成员 A

You have a method inside class client. In this method you give a call to a method of class Session. In this call you have a delegate (a kind of "callback"), this delegate runs in the context of class client and therefore can access private member A.

小霸王臭丫头 2024-12-29 03:05:18

从技术上讲,访问私有 A 变量的不是 Session 类,而是在 SendData() 中创建的委托函数来执行此操作。这没有问题。可以将其视为 IterateClients 只是从 client 类调用方法,该方法可以访问 A 变量,因为它位于同一个类中。

Technically it's not Session class that's accessing private A variable, it's delegate function created in SendData() that does this. There is no problem with that. Think of it as IterateClients is just calling method from client class, which method can access A variable since it is on the same class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文