2016-05-24 36 views
1

我想基于不同的尺寸和几何标志创建一个几何图形,以确定它是多维数据集还是圆形。为此,我必须使用重载函数,但我不知道如何在类函数中使用这些函数来存储我的输入。下面是我做的,到目前为止:如何在c中使用构造函数中的重载函数#

public void Object(double x, double y, double z) 
     { 
      name = "Cube"; 
      a = x; 
      b = y; 
      c = z; 
     } 
     public void Object(double r, double y) 
     { 
      name = "Cylinder"; 
      r1 = r; 
      b = y; 

     } 

    protected double a{ get; private set; } 
    protected double b{ get; private set; } 
    protected double c{ get; private set; } 
    protected double r1{ get; private set; } 

第一个问题我已经是,我无法使用声明的变量多次,我要声明一个变量为每一个可能的对象,在这种情况下,我不能在b保存两个变量,这是不太有效的。

我的第二个问题是,如果我想与其他值一起调用该对象在我的数据类这样它不工作:

public MeasureObject(double hash, string name, new Object obj(int n, different variables), double coordinates, ...) 

{ 
this.Hash = hash; 
this.Object=obj; 
} 

是否有实现目标的通用几何更好的方法,其可以采取一个整数和n个不同的维度,长度是什么?

+2

如果对象不同,你应该**在不同的类中处理它们。泛型应该用于以相同方式处理不同类型的代码。 –

回答

2

几何对象之间有足够的差异,最好是有单独的类。

例如,计算每个的体积是不同的。

一个解决方案可能是从中继承一个基类,并有一个工厂类,根据该标志确定需要哪个几何对象实例。

+0

我承认这是真的。但我怎么能使用这样一个类,并保持输入通用?例如,我只使用一个函数,它为相应的几何体提供了一个整数标志,以及x值? – DerBenutzer

+1

@DerBenutzer究竟是什么问题?在一种情况下,您可以使用Object first_object = Cube(1,2,3),而在另一种情况下,您可以使用Object second_object = Cylinder(4,5)。比对象更可读性third_object(1,2,3)// <<产生一个立方体。 – Aziuth

+0

这是不可能的,因为几何图形将从数据库中读取。它就像会有10000个输入,他们应该怎么知道他们是否应该调用cube()或者cylinder()。他们需要一个标志来调用某种方法。 – DerBenutzer

1

我会做一个Cube和Cylinder类和一个抽象类几何。然后,您可以将两个类的方法放在抽象类中,并覆盖其他类。

public abstract class Shape 
    { 
     public int Height; 
     public int Width; 
     public int Depth; 
     public double Area() 
     { 
      return Height * Width; 
     } 
     public abstract double Volume(); 
    } 
    class Square : Shape 
    { 
     public Square(int height, int width) 
     { 
      Height = height; 
      Width = width;    
     } 
     public override double Volume() 
     { 
      throw new NotImplementedException(); 
     }    
    } 
    class Cube : Shape 
    { 
     public Cube(int height, int width, int depth) 
     { 
      Height = height; 
      Width = width;   
      Depth = depth;  
     } 
     public override double Volume() 
     { 
      return Height * Width * Depth; 
     }    
    } 
1

我设计我的课是这样的:

namespace Geometry 
{ 
    public interface IMeasurableSolid 
    { 
     double CalcSurface(); 
     double CalcVolume(); 
    } 

    public class Sphere : IMeasurableSolid 
    { 
     public double Radius { get; set; } 

     public Sphere(double radius) 
     { 
      if (radius <= 0) 
      { 
       throw new ArgumentOutOfRangeException("radius", "value must be positive"); 
      } 
      this.Radius = radius; 
     } 

     public double CalcSurface() 
     { 
      return (Math.PI * 4 * Math.Pow(this.Radius, 2)); 
     } 

     public double CalcVolume() 
     { 
      return (Math.PI * 4 * Math.Pow(this.Radius, 3)/3); 
     } 
    } 

    public class Cylinder : IMeasurableSolid 
    { 
     public double Height { get; set; } 
     public double Radius { get; set; } 

     public Cylinder(double height, double radius) 
     { 
      if (height <= 0) 
      { 
       throw new ArgumentOutOfRangeException("height", "value must be positive"); 
      } 
      if (radius <= 0) 
      { 
       throw new ArgumentOutOfRangeException("radius", "value must be positive"); 
      } 
      this.Height = height; 
      this.Radius = radius; 
     } 

     public double CalcSurface() 
     { 
      return 2 * Math.PI * this.Radius * (this.Radius + this.Height); 
     } 

     public double CalcVolume() 
     { 
      return this.Height * Math.PI * Math.Pow(this.Radius, 2); 
     } 
    } 
} 

你甚至可以从一个抽象基类派生SphereCylinder为马腾塞曼建议;我更喜欢使用接口,因为我认为它是一种更通用的解决方案。 无论如何你决定这么做,请让我建议你遵守一些最佳实践(你会在这里找到几个例子:https://www.roberthalf.com/technology/blog/4-best-practices-for-microsoft-net-framework-and-applications)。 即:

  • 避免调用类Object。这是一个危险的名字。可以通过简单的模糊处理将其与object混淆。叫它GeometrySolid
  • 选择明确的属性名称和大写(高度,长度和宽度比a,b,c更好)。

保持您的模型尽可能接近您想表示的现实。正确设计和维护你的课程会更容易。