嵌套类型问题

发布于 2024-10-25 10:29:40 字数 371 浏览 3 评论 0原文

我只是尝试创建这个简单的实现:

class Test
{
   private int abc = 0;

   public class TestClass
   {
      private void changeABC()
      {
         abc = 123;
      }
   }
}

如果我编译它,它会抱怨:

无法通过嵌套类型“B.Test.TestClass”访问外部类型“A.Test”的非静态成员

我不喜欢设置的解决方案: static int abc = 0;

还有其他吗解决这个问题?

I just tried to create this simple implementation:

class Test
{
   private int abc = 0;

   public class TestClass
   {
      private void changeABC()
      {
         abc = 123;
      }
   }
}

If I compile it, it will complain:

Cannot access a non-static member of outer type 'A.Test' via nested type 'B.Test.TestClass'

I dont like the solution of setting: static int abc = 0;

Is there any other solution for this?

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

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

发布评论

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

评论(4

老旧海报 2024-11-01 10:29:40

您可能有 Java 背景,这段代码可以按预期工作。

在 C# 中,嵌套类型是静态的(用 Java 的说法),即它们不绑定到父类的实例。这就是您的代码失败的原因。您需要以某种方式将父类的实例传递给子类并访问其成员abc

You are probably coming from a Java background where this code would work as expected.

In C#, nested types are static (in the parlance of Java), i.e. they are not bound to an instance of the parent class. This is why your code fails. You need to somehow pass an instance of the parent class to the child class and access its member abc.

南巷近海 2024-11-01 10:29:40

内部类需要引用外部类的实例:

class Test
{
   private int abc = 0;

   public class TestClass
   {
      private void changeABC(Test test)
      {
         test.abc = 123;
      }
   }
}

The inner class needs a reference to an instance of the outer class:

class Test
{
   private int abc = 0;

   public class TestClass
   {
      private void changeABC(Test test)
      {
         test.abc = 123;
      }
   }
}
寂寞笑我太脆弱 2024-11-01 10:29:40

我不明白为什么 TestClass 应该 在其实例类时更改父 Test

也许我的例子可以阐明这一点:

class Test
{
   public Test()
   {
     TestClass test = new TestClass();//create a new **instance** here
     test.changeABC(this);//give the instance of Test to TestClass
     Console.WriteLine(abc);//will print 123 
   }
   int abc = 0;

   public class TestClass
   {
      public void changeABC(Test t)
      {
         t.abc = 123;
      }
   }
}

像这样使用:

Test theTest = new Test();

I don't see why TestClass should change the parent Test when its an instance class.

Maybe me example would shed light on this:

class Test
{
   public Test()
   {
     TestClass test = new TestClass();//create a new **instance** here
     test.changeABC(this);//give the instance of Test to TestClass
     Console.WriteLine(abc);//will print 123 
   }
   int abc = 0;

   public class TestClass
   {
      public void changeABC(Test t)
      {
         t.abc = 123;
      }
   }
}

Use Like this:

Test theTest = new Test();
淤浪 2024-11-01 10:29:40

来自 C# 嵌套类就像 C++ 嵌套类,而不是 Java 内部类

当你在里面声明一个类时
另一个类,内部类仍然
就像普通班级一样。筑巢
控制访问和可见性,但是
不是行为。换句话说,所有的
你学到的常规规则
类也适用于嵌套类。

在Java中,内部类有一个秘密
这位记得的 $0 会员
外部类的实例
它已被绑定。

换句话说,Java内部类是
不可用的语法糖
到 C#。在C#中,你必须这样做
手动。

From C# nested classes are like C++ nested classes, not Java inner classes

When you declare a class inside
another class, the inner class still
acts like a regular class. The nesting
controls access and visibility, but
not behavior. In other words, all the
rules you learned about regular
classes also apply to nested classes.

In Java, the inner class has a secret
this$0 member which remembers the
instance of the outer class to which
it was bound.

In other words, Java inner classes are
syntactic sugar that is not available
to C#. In C#, you have to do it
manually.

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