zjm_sunrise 发表于 2008-7-28 22:48

请大家帮忙看一下这段程序哪里不对了?

有这样一个问题:定义一个Shape抽象类,作用它作为基类派生出Rectanle 、Circle等具体形状类,已知具体形状类均具有两个方法,分别用来求形状的面积和周长。运行界面如附件中所示,程序代码编写如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace shape
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();            
        }
        public class Shape
        {
            public virtual double GetArea()
            {
                return (0);
            }
            public virtual double GetPerim()
            {
                return (0);
            }
        }
            class Rectangle : Shape
            {
                public double Width;
                public double Length;
                public Rectangle(double a, double b)
                {
                    Width = a;
                    Length = b;
                }
                public override double GetArea()
                {
                    return (Width * Length);
                }
                public override double GetPerim()
                {
                    return (2 * (Width + Length));
                }
            }
            class Circle : Shape
            {
                public double Radius;
                public Circle(double r)
                {
                    Radius = r;
                }
                public override double GetArea()
                {
                    return (Math.PI * Radius * Radius);
                }
                public override double GetPerim()
                {
                    return (2 * Math.PI * Radius);
                }
            }
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            double a,b;
            a = Convert.ToDouble(textBox1.Text);
            b = Convert.ToDouble(textBox2.Text);
            Rectangle Rect=new Rectangle (a,b);
            textBox3.Text = Rec.GetPerim().ToString();
            textBox4.Text = Rec.GetAera().ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            double r;
            r = Convert.ToDouble(textBox5.Text);
            Circle Cir = new Circle(r);
            textBox6.Text = Cir.GetPerim().ToString();
            textBox7.Text = Cir.GetArea().ToString();
        }
    }
}
但是程序在编译时提示了这样一堆错误:
错误    1    应输入 class、delegate、enum、interface 或 struct    G:\visualstudio\shape\shape\Form1.cs    64    17    shape
错误    2    应输入 class、delegate、enum、interface 或 struct    G:\visualstudio\shape\shape\Form1.cs    69    17    shape
错误    3    应输入 class、delegate、enum、interface 或 struct    G:\visualstudio\shape\shape\Form1.cs    74    32    shape
错误    4    应输入 class、delegate、enum、interface 或 struct    G:\visualstudio\shape\shape\Form1.cs    79    17    shape
错误    5    应输入 class、delegate、enum、interface 或 struct    G:\visualstudio\shape\shape\Form1.cs    83    30    shape
错误    6    应输入类型、命名空间定义或文件尾    G:\visualstudio\shape\shape\Form1.cs    86    9    shape
请大家帮忙看看是哪里出了问题呢?

KingTam 发表于 2008-7-29 09:34

在类里面可以定义类的吗?应该是不可以的吧!

师妃暄 发表于 2008-7-29 11:15

类里面不能定义类的

xiaoshu838 发表于 2008-7-29 11:59

您的两个click方法和form_load方法,根本没在类中,肯定要报错啊

xyq701830 发表于 2008-7-29 12:37

类里面可以定义类的吧````

漏网之雨 发表于 2008-7-30 00:09

类里面是可以定义一个类的  叫子类   不信你定义就知道了!

搂主那里是不是想用textBox.Text输入来实现运算啊!

其实你也不必写的 那复杂!

tpriwwq 发表于 2008-7-30 13:14

少了个}

页: [1]

编程论坛