从同一类的其他实例访问私人领域 [英] Private field accessible from another instance of the same class
本文介绍了从同一类的其他实例访问私人领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不明白的following..I一直以为我只能从该领域在声明的类访问私有字段但是,在这种情况下,我能够访问它。
堂课
{
名单,LT;客户端> ListOfClients =新的List<客户端>();
公共无效IterateClients(动作<客户端>动作)
{
}
}
类客户端
{
私人诠释A;
会议区;
公共无效的SendData()
{
area.IterateClients(委托(客户端C)
{
CA = 5; //这是怎么来的?访问
});
}
}
解决方案
从技术上这并不是说的访问私有会话
类 A
变量,在创建它的委托功能送出数据()
做这个。有与没有问题。把它看成是 IterateClients
距离调用方法的客户端
类,方法可访问 A
变量,因为它是在同一类。
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?
});
}
}
解决方案
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.
这篇关于从同一类的其他实例访问私人领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文