2016-05-24 35 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更好)。

保持您的模型儘可能接近您想表示的現實。正確設計和維護你的課程會更容易。