public abstract class Shape<S,T>
{
T _Result=default(T);
S _ShapeName = default(S);
//Calculate Shape Area
public virtual T Area(T Par1, T Par2) { return _Result; }
//Calculate Shape Volume
public virtual T Volume(T Length, T Width, T Height) { return _Result; }
//Calculate Triangle Volume
public virtual T Volume(T Bottom, T Height) { return _Result; }
//Show Is ShapeName
public virtual S ShapeName() { return _ShapeName; }
}
public class Square : Shape<string,double>
{
public override double Area(double Length, double Width)
{
return Length * Width;
}
public override double Volume(double Length, double Width, double Height)
{
return Length * Width * Height;
}
public override string ShapeName()
{
return "Shape";
}
}
public class Triangle : Shape<string, int>
{
public override int Area(int Bottom, int Height)
{
return Bottom * Height / 2;
}
public override int Volume(int Bottom, int Height)
{
return Bottom * Height / 2*Height;
}
public override string ShapeName()
{
return "Triangle";
}
}
private void btnAction_Click(object sender, EventArgs e)
{
Shape<string,int> ishap = new Triangle();
MessageBox.Show(ishap.ShapeName() + "面積 : " + ishap.Area(8, 9) + "\n" + ishap.ShapeName() + "體積 : " + ishap.Volume(8, 9));
}
執行結果:
沒有留言:
張貼留言