我正在尝试以下但是给我编译时错误
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
类的实例时,显示编译时错误。为什么?怎么能让它成为可能?
答案 0 :(得分:3)
C#中的内部类不像Java内部类,它们不属于外部类,只是可见性问题。
您必须从shape.innershape
派生出矩形。
答案 1 :(得分:1)
因为矩形是从形状继承的,而不是来自 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
{
...
答案 2 :(得分:0)
同时尝试将您的课程公开,这样可见性不会受到限制。