主题:[讨论]帮我写个判断语句
er72681085
[专家分:0] 发布于 2006-05-08 15:57:00
/*先判断每个图形是二维的还是三维的。如果一个图形是二维的,显示它的Area(面积),如果一个图形是三维的,显示它的Area(面积)和Volume(体积)*/
回复列表 (共9个回复)
沙发
er72681085 [专家分:0] 发布于 2006-05-08 16:05:00
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();
}
}
}
板凳
er72681085 [专家分:0] 发布于 2006-05-08 16:08:00
就是为实现这个程序,应该怎么去写个判断语句呢?!
希望大家不吝赐教
3 楼
jzyray [专家分:20610] 发布于 2006-05-08 16:44:00
Shape中定义一个可重载公共变量,指示是否为三维。
二维和三维在继承Shape时修改该值。
4 楼
er72681085 [专家分:0] 发布于 2006-05-08 21:40:00
呵呵,帮我改过来嘛,我都想了好几天了,现在都晕了
先谢谢你了哈!
5 楼
jzyray [专家分:20610] 发布于 2006-05-09 09:47:00
using System;
namespace _6._2
{
public abstract class Shape
{
public abstract double GetArea();//计算图形面积的抽象方法
public abstract double GetVolume();//计算图形体积的抽象方法
public abstract bool IsThreeDimensional{
get;
}
}
public abstract class TwoDimensionalShape:Shape//写一个二维图象抽象类
{
public double a;
public TwoDimensionalShape(double a)//构造函数
{
this.a=a;
}
public abstract double Noodles//抽象属性成员
{get;set;}
public override bool IsThreeDimensional{
get{
return false;
}
}
}
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 override bool IsThreeDimensional{
get{
return false;
}
}
}
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();
}
}
}
这样,使用的时候用IsThreeDimensional判断即可。
6 楼
er72681085 [专家分:0] 发布于 2006-05-09 17:30:00
怎么三个二维图象(Circle,Square,Triangle)类还是不能实现继承的抽象成员GetVolume()呢?!
7 楼
jzyray [专家分:20610] 发布于 2006-05-09 19:34:00
二维对象干吗要继承体积方法?
8 楼
er72681085 [专家分:0] 发布于 2006-05-09 22:42:00
我就是想找种方法让二维对象不要继承体积方法
但程序调试还是有问题撒
9 楼
er72681085 [专家分:0] 发布于 2006-05-12 12:41:00
呵呵,还是要继承地,只不过在三个具体的二维图象类在继承体积的时候
返回0就ok啦!
我来回复