C#用抽象类定义几何图形
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();
}
}
}