欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#用抽象类定义几何图形

程序员文章站 2022-06-07 12:55:09
using System;/*using System.Data;*/namespace tx{ abstract class tx { public double chang; public double kuan; public double r; public double pai; publ ......

using system;
/*using system.data;*/
namespace tx
{
    abstract class tx
    {
        public double chang;
        public double kuan;
        public double r;
        public double pai;
        public tx(double chang, double kuan)
        {
            this.chang = chang;
            this.kuan = kuan;
           /* this.r = r;
            this.pai = pai*/;
        }
        public abstract void mj();
    }
    class cfx:tx
    {
        public cfx(double chang, double kuan) : base(chang,kuan) { }
        public override void mj()
        {
            console.writeline("这个图形是长方形面积为"+chang*kuan+"m^2");
        }
    }
    class zfx : tx
    {
        public zfx(double chang, double kuan) : base(chang,kuan) { }
        public override void mj()
        {
            console.writeline("这个图形是正方形"+chang*kuan+"m^2");
        }
    }
    class yx : tx
    {
        /*public int k;*/
        
        public yx(double r) : base(r,0) { }
        public override void mj()
        {
            console.writeline("这图形是圆形面积是"+math.pi*chang*chang + "m^2");
        }
    }
    class program
    {
        static void main(string[] args)
        {
            cfx c = new cfx(10, 12);
            zfx z = new zfx(5, 5);
            yx y = new yx(4);
            c.mj();
            z.mj();
            y.mj();
        }
    }
}