0

我正在尝试以下但给我编译时错误

   class Program
{
    static void Main(string[] args)
    {
        shape.innershape s = new rectangle(); // Error Here

    }
}
class shape
{
    public int nshape = 0;
    public shape()
    {
        nshape = 1;
        innershape n = new innershape();
    }
    public void MakeOuterShape()
    {

    }
    public class innershape
    {
        public int nInnerShape = 0;
        public innershape()
        {
            nInnerShape = 1;
        }
        public void makeInnerShape()
        {

        }
    }
}
class rectangle :shape
{
     // Code goes here.
}

我正在继承Shape包含类定义的innershape类。但是当我尝试制作Rectangle类的实例时,会innershape显示编译时错误。为什么 ??以及如何使它成为可能?

4

3 回答 3

3

C# 中的内部类与 Java 内部类不同,它们不属于外部类,只是可见性问题。

您必须从中派生 Rectangle shape.innershape

于 2013-08-01T11:18:14.110 回答
1

因为rectangle是从shape继承的,但不是从innershape 继承的

  class rectangle: shape {
  ...

  public class innershape
  {
   ...

你不能写

shape.innershape s = new rectangle(); // <- can't convert rectangle to shape

但你可以把

  shape s = new rectangle(); // shape is super class for rectangle

Perharps您应该将代码更改为

  class rectangle :shape.innershape 
  {
  ...
于 2013-08-01T11:29:10.690 回答
0

还可以尝试公开您的课程,这样可见性就不会受到限制。

于 2013-08-01T11:33:53.293 回答