三角形面积、圆的面积
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _2
{
public abstract class shape
{
private string mc;
public shape(string s)
{
Mc = s;
}
public string Mc
{
get
{
return mc;
}
set
{
mc = value;
}
}
public double Area()
{
return 1;
}
public abstract double area
{
get;
}
public override string ToString()
{
return Mc + "面积为:" + string.Format("{0:F2}", area);
}
}
public class triangle : shape
{
private int a,b,c;
public triangle(int a,int b,int c, string id)
: base(id)
{
this.a = a;
this.b = b;
this.c = c;
}
public double Area()
{
double p,s;
p = (a + b + c) / 2;
s = Math.Sqrt(p*(p-a)*(p-b)*(p-c));
return s;
}
public override double area
{
get
{
return Math.Sqrt(((a + b + c) / 2) * (((a + b + c) / 2) - a) * (((a + b + c) / 2) - b) * (((a + b + c) / 2) - c));
}
}
}
public class circles : shape
{
public double r;
public circles(double r, string id)
: base(id)
{
this.r = r;
}
public double Area()
{
double s;
s = 2 * 3.14 * r;
return s;
}
public override double area
{
get
{
return 2 * 3.14 * r;
}
}
}
class Program
{
static void Main(string[] args)
{
triangle s1 = new triangle(3,4,5,"三角形");
circles c1 = new circles(4,"圆");
string result1, result2;
result1 = s1.ToString();
result2 = c1.ToString();
Console.WriteLine("{0}\n{1}",result1,result2);
Console.WriteLine("第二种实现方法:");
Console.WriteLine("三角形面积为:{0}\n圆的面积为:{1}", s1.Area(), c1.Area());
}
}
}