c# 封装类 圆形(Circle)和三角形(Trangle)两个类,分别计算圆和三角形
c#封装类圆形(Circle)和三角形(Trangle)两个类,分别计算圆和三角形的周长和面,并通过主体(Main)实现打印...
c# 封装类 圆形(Circle)和三角形(Trangle)两个类,分别计算圆和三角形的周长和面,并通过主体(Main)实现打印
展开
展开全部
/// <summary>
/// 基类
/// </summary>
public abstract class CShape
{
public abstract Double Perimeter();
public abstract Double Acreage();
}
/// <summary>
/// 三角型
/// </summary>
public class Triangle : CShape
{
public Double A { get; set; }
public Double B { get; set; }
public Double C { get; set; }
public Double H { get; set; }
/// <summary>
/// 求面积
/// </summary>
/// <returns></returns>
public override Double Acreage()
{
return 0.5 * A * H;
}
/// <summary>
/// 求周长
/// </summary>
/// <returns></returns>
public override Double Perimeter()
{
return A + B + C;
}
}
/// <summary>
/// 圆
/// </summary>
public class CirCle : CShape
{
public Double R { get; set; }
public override Double Acreage()
{
return 3.14 * R *
R;
}
public override Double Perimeter()
{
return 3.14 * R * 2;
}
}
上面是类的定义,基础基类只是抽象了方法,你在Main中调用就行。
CirCle circle = new CirCle() { R = 10 };
Triangle triangle = new Triangle() { A = 10, B = 5, C = 12, H = 7 };
Console.WriteLine("Circle R = " + circle.R + "\tAcreage=" + circle.Acreage() + "\tPerimeter=" + circle.Perimeter());
Console.WriteLine("Triangle A = " + triangle.A + ", B = " + triangle.B + ", C = " + triangle.C + ", H = " + triangle.H
+ "\tAcreage=" + triangle.Acreage() + "\tPerimeter=" + triangle.Perimeter());
控制台程序。
/// 基类
/// </summary>
public abstract class CShape
{
public abstract Double Perimeter();
public abstract Double Acreage();
}
/// <summary>
/// 三角型
/// </summary>
public class Triangle : CShape
{
public Double A { get; set; }
public Double B { get; set; }
public Double C { get; set; }
public Double H { get; set; }
/// <summary>
/// 求面积
/// </summary>
/// <returns></returns>
public override Double Acreage()
{
return 0.5 * A * H;
}
/// <summary>
/// 求周长
/// </summary>
/// <returns></returns>
public override Double Perimeter()
{
return A + B + C;
}
}
/// <summary>
/// 圆
/// </summary>
public class CirCle : CShape
{
public Double R { get; set; }
public override Double Acreage()
{
return 3.14 * R *
R;
}
public override Double Perimeter()
{
return 3.14 * R * 2;
}
}
上面是类的定义,基础基类只是抽象了方法,你在Main中调用就行。
CirCle circle = new CirCle() { R = 10 };
Triangle triangle = new Triangle() { A = 10, B = 5, C = 12, H = 7 };
Console.WriteLine("Circle R = " + circle.R + "\tAcreage=" + circle.Acreage() + "\tPerimeter=" + circle.Perimeter());
Console.WriteLine("Triangle A = " + triangle.A + ", B = " + triangle.B + ", C = " + triangle.C + ", H = " + triangle.H
+ "\tAcreage=" + triangle.Acreage() + "\tPerimeter=" + triangle.Perimeter());
控制台程序。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询