-1

在java中,我可以编写这样的代码。

public class Map1 extends MapInfo {

    {
        // I can access fields of the super class
    }

}

目前在sharpdevelop中编写此代码,代码完成没有出现。

public class Map1 : MapInfo {

    {
        // It gives error.
        // Invalid token '{' in class, struct, or interface member declaration (CS1519) - C:\Users\sriharshach....st\Map.cs:10,3
    }

}

有没有办法在 c# 中编写相同的代码?(即,不在构造函数中)

谢谢

4

4 回答 4

3

在 C# 领域它会是

public class Map1 : MapInfo
{
   // You can access protected and public fields / methods from the SuperClass
   // and internal + protected internal 
}

编辑:有太多花括号并修正了评论

于 2013-01-13T12:31:06.717 回答
0

与 Java 一样,答案取决于字段的访问级别。假设这些字段不是私有的,您应该能够访问超类字段,就好像它们属于子类一样。

于 2013-01-13T12:28:17.750 回答
0

在 C# 中,您可以在派生类中随处访问超类的非私有字段。

于 2013-01-13T12:29:07.177 回答
0

在 C# 中,您可以使用 继承/实现基类:,但不能在类中立即使用 {}。您需要方法或构造函数名称后跟 {}

例如

public class ParentFoo {
  public Parentfoo() {}
}

public class ChildFoo:ParentFoo -- inheriting parentfoo
{
   public ChildFoo(){}
}
于 2013-01-13T12:32:27.257 回答