以前学过C和VB,现在正在着手学C#。有个问题搞不明白,我按一本书上讲的内容自己做了一个例子,代码在下面。为什么我把基类和派生类的定义放在main函数里,C#就会报错?using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{ public class animal
{
public animal ()
{
Console.WriteLine("Constructing Animal!");
}
} public class elephant : animal
{
public elephant ()
{
Console.WriteLine("Constructing Elephant!");
}
}
static void Main(string[] args)
{
elephant e = new elephant();
}
}
}
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{ public class animal
{
public animal ()
{
Console.WriteLine("Constructing Animal!");
}
} public class elephant : animal
{
public elephant ()
{
Console.WriteLine("Constructing Elephant!");
}
}
static void Main(string[] args)
{
elephant e = new elephant();
}
}
}
using System.Text;namespace DataGrid
{
/// <summary>
/// Program 的摘要说明。
/// </summary>
public class Program
{
class a
{ public class animal
{
public animal ()
{
Console.WriteLine("Constructing Animal!");
}
} public class elephant : animal
{
public elephant ()
{
Console.WriteLine("Constructing Elephant!");
}
}
static void Main(string[] args)
{
elephant e = new elephant();
Console.ReadLine();
}
}
}
}
Constructing Animal!
Constructing Elephant!tianlong30()的解答之所以能显示出上面的结果
是因为他在MAIN函数里多了个Console.ReadLine();