我可以访问内部类的外部类对象 [英] Can i access outer class objects in inner class

查看:156
本文介绍了我可以访问内部类的外部类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个类是这样的。

class A
{
    public class innerB
       {
       //Do something
       }

    public class innerC
       {
        //trying to access objB here directly or indirectly over here. 
        //I dont have to create an object of innerB, but to access the object created by A
        //i.e.
             innerB objInnerB = objB;
        //not like this
             innerB objInnerB= new innerB();
       }

private innerB objB{get;set;}  **//Private**

public A()
   {
    objB= new innerB();
   }
}



我要访问B类的类对象由A级
创建的C是否有可能以某种方式作出对C类A级的对象,我可以通过创建事件或无论如何得到一个类的对象更改。

I want to access the object of class B in Class C that is created by class A. Is it possible somehow to make changes on object of Class A in Class C. Can i get Class A's object by creating event or anyhow.

编辑:我在问上面的问题的错误。在已创建的的B对象是私人不公开

是有可能做到这一点通过创建事件。

IS IT POSSIBLE TO DO THIS BY CREATING EVENT

如果好歹我成为能够提高,可以通过A类,那么我的问题是可以解决处理的事件。

If anyhow I become able to raise an event that can be handled by Class A, then my problem can be solved.

推荐答案

如果我正确地读你要访问A类中的innerC财产objB无一起传递给它。

If I'm reading you correctly you want to access the objB property of class A within innerC WITHOUT passing it along.

这是不是C#的内部类是如何工作的,如本文中描述: C#嵌套类像C ++嵌套类,而不是Java内部类

This isn't how C# inner classes work, as described in this article: C# nested classes are like C++ nested classes, not Java inner classes

如果您想从innerC访问A.objB那么你将不得不A类莫名其妙地传递到innerC 确保你以某种方式暴露objB财产。 (即此,如果你有objB设置为私人将无法工作。)

If you want to access A.objB from innerC then you are going to have to somehow pass class A to innerC AND make sure that you expose the objB property somehow. (i.e. this won't work if you have objB set to private.)

这篇关于我可以访问内部类的外部类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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