我用VS建立了一个c#的控制台项目,然后以下是我的代码
using System;namespace MyClass
{
public class Round
{
     public double r; public Round(double r)
{
  this.r=r;
} public virtual double Area()
{
  return 3.14*r*4;
}
} class Sphere:Round
{
public Sphere(double r):base(r)
{
} public override double Area()
{
  return (4*(base.Area()));
}
} class MyClass
{
public static void main()
{
  double r=1;   Round rnd=new Round(r);
  Sphere s=new Sphere(r); Console.WriteLine("圆的面积是 {0}",rnd.Area());
Console.WriteLine("球的面积是 {0}",s.Area());
String str=Console.ReadLine();
}
}
}
1:我F5运行的时候,提示我程序“E:\project\Round\obj\Debug\Round.exe”未定义入口点
2:public virtual double Area()对于这个虚方法,我难道不可以public  double Area()
这样写么,也就是没太理解virtual的作用~请教~~~~

解决方案 »

  1.   

    1.加上/// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }
      

  2.   

    不是虚方法的问题,只是C#的主方法的写法和java不同,
    public static void main()  //把你的主方法的方法名改为Main(),M要大写
      

  3.   

    不好意思没有看清楚,帮你调了一下:
    using System;namespace ConsoleApplication1
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class Class1
    {
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    //
    // TODO: 在此处添加代码以启动应用程序
    //
    double r=1; Round rnd=new Round(r);
    Sphere s=new Sphere(r); Console.WriteLine("圆的面积是 {0}",rnd.Area());
    Console.WriteLine("球的面积是 {0}",s.Area());
    String str=Console.ReadLine();
    } }
    public class Round
    {
    public double r; public Round(double r)
    {
    this.r=r;
    } public virtual double Area()
    {
    return 3.14*r*4;
    }
    } class Sphere:Round
    {
    public Sphere(double r):base(r)
    {
    } public override double Area()
    {
    return (4*(base.Area()));
    }
    }}
      

  4.   

    谢谢,晕死,一个字母写错~~~
    还是不太明白为什么要加那个virtual啊?继续请教~
      

  5.   

    楼上的,楼主所写的是Console控制台的程序.
    楼主,你主函数写错了:"public static void main()"
    应该是"public static void Main()"
    另外使用虚函数是为了派生类可以继承并且覆写这个函数.
    建议你去看看书,了解一下面向对象的概念.
      

  6.   

    楼上的,我看了,我就是想知道是不是这个意思,因为书上写得不是很明白~
    如果说,我确定这个方法以后需要在子类中重写,就必须写成virtual,否则就不能override,是不是这个意思~
      

  7.   

    是的.是这个意思.virtual修饰的函数就是这样的.