/*要求判断每个图形是二维的还是三维的。如果一个图形是二维的,显示它的Area,如果一个图形是三维的,显示它的Area和Volume。*/
using System;
namespace _6._2
{
public abstract class Shape
{
public abstract double GetArea();//计算图形面积的抽象方法
public abstract double GetVolume();//计算图形体积的抽象方法
}
public abstract class TwoDimensionalShape:Shape//写一个二维图象抽象类
{
public double a;
public TwoDimensionalShape(double a)//构造函数
{
this.a=a;
}
public abstract double Noodles//抽象属性成员
{get;set;}
}
public class Circle:TwoDimensionalShape//定义二维派生类-圆类
{
public Circle(double a):base(a)//调用抽象类构造函数
{}
public override double Noodles//重写抽象类属性成员
{
get{return a;}
set{a=value;}
}
public override double GetArea()//调用抽象方法
{
Console.Write("圆的面积为:");
return Math.PI*a*a;
}
}
public class Square:TwoDimensionalShape//定义二维派生类-正方形类
{
public Square(double a):base(a)
{}
public override double Noodles
{
get{return a;}
set{a=value;}
}
public override double GetArea()
{
Console.Write("正方形的面积为:");
return a*a;
}
}
public class Triangle:TwoDimensionalShape//定义二维派生类-三角形类
{
public Triangle(double a):base(a)
{}
public override double Noodles
{
get{return a;}
set{a=value;}
}
public override double GetArea()
{
Console.Write("三角形的面积为:");
return Math.Sin(Math.PI/3)*a*a*1/2;
}
}
public abstract class ThreeDimensionalShape:Shape//定义三维图形类
{
public double b;
public ThreeDimensionalShape(double b)//构造函数
{
this.b=b;
}
public abstract double Body//抽象属性成员
{get;set;}
}
public class Sphere:ThreeDimensionalShape//定义三维派生类-球体类
{
public Sphere(double b):base(b)//调用抽象类构造函数
{}
public override double Body//重写抽象类属性成员
{
get{return b;}
set{b=value;}
}
public override double GetArea()//调用抽象方法
{
Console.Write("球体的表面积为:");
return Math.PI*4*b*b;
}
public override double GetVolume()
{
Console.Write("球体的体积为:");
return Math.PI*b*b*b*4/3;
}
}
public class Cube:ThreeDimensionalShape//定义三维派生类-正方体类
{
public Cube(double b):base(b)//调用抽象类构造函数
{}
public override double Body//重写抽象类属性成员
{
get{return b;}
set{b=value;}
}
public override double GetArea()//调用抽象方法
{
Console.Write("正方体的表面积为:");
return 6*b*b;
}
public override double GetVolume()
{
Console.Write("正方体的体积为:");
return b*b*b;
}
}
public class Cylinder:ThreeDimensionalShape//定义三维派生类-圆柱类
{
public Cylinder(double b):base(b)//调用抽象类构造函数
{}
public override double Body//重写抽象类属性成员
{
get{return b;}
set{b=value;}
}
public override double GetArea()//调用抽象方法
{
Console.Write("圆柱体的表面积为:");
return Math.PI*2*b*b+Math.PI*2*b*b;
}
public override double GetVolume()
{
Console.Write("圆柱体的体积为:");
return Math.PI*b*b*b;
}
}
class runApp
{
static void Main(string[] args)
{
for(int i=1;i<=10;i++)
{
Console.WriteLine("\n当边长为{0}个单位长度时",i);
Circle s = new Circle(i);
Console.WriteLine(s.GetArea());
Square s1=new Square(i);
Console.WriteLine(s1.GetArea());
Triangle s3=new Triangle(i);
Console.WriteLine(s3.GetArea());
Sphere s4=new Sphere(i);
Console.WriteLine(s4.GetArea());
Cube s5=new Cube(i);
Console.WriteLine(s5.GetArea());
Cylinder s6=new Cylinder(i);
Console.WriteLine(s6.GetArea());
}
Console.ReadLine();
}
}
}