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

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

问题描述

我有三个这样的类。

  class A 
{
public class innerB
{
//做某事
}

public class innerC
{
//试图在这里直接或间接访问objB。
//我不必创建一个innerB的对象,而是访问由A
//i.e创建的对象。
innerB objInnerB = objB;
//不是这样的
innerB objInnerB = new innerB();
}

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

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

我想访问类B的对象C创建的类A.
可能以某种方式对类C中的类A的对象进行更改。可以通过创建事件或任何方式获取类A的对象。



编辑:我在提出上述问题时的错误。在A中创建的B的对象不是公开的



这是创建活动的可能


解决方案

如果我能够提出一个可以由A类处理的事件,那么我的问题可以解决。 p>如果我正确地读你的话,你想访问innerC中的objB属性,而不是传递它。



这不是C# ,如本文所述: C#嵌套类是C ++嵌套类,而不是Java内部类



如果你想从innerC访问A.objB,那么你必须以某种方式将A类传递给innerC AND 确保以某种方式公开objB属性。 (如果你有objB设置为私有,这将不工作。)


I have three classes like this.

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();
   }
}

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.

Edit: My mistake in asking the question above. Object of B created in A is private not public

IS IT POSSIBLE TO DO THIS BY CREATING EVENT

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

解决方案

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

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

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天全站免登陆