2015-12-21 58 views
-1

我已經創建了兩個方法Radioactive_Sources,它們具有相同的名稱但參數不同,因爲它們用於不同的目的。一個只需要兩個參數,另一個需要六個參數。我如何使用這兩種方法,當我把它在不同的班級,我嘗試通過以下方式使用不同的參數創建相同的方法

namespace DABRAS_Software 
{ 
    ... 
    public DefaultConfigurations() 
    { 

ListOfSources = new List<Radioactive_Sources>(?) ; two arguments 
ListOfSources_2 = new List<Radioactive_Sources>(?) ; six arguments 
    ..... 

-------------------------------- 
namespace DABRAS_Software 
{ 
[Serializable] 
public class Radioactive_Source 
{ 
    .... 
#region Constructor 
public Radioactive_Source(string _Name, string _SerialNumber, string _Description, RadiationType _Type, EnergyBand _E, ulong _HalfLife, string _CertDate, int _CertActivity) 
{ 
    this.Name = _Name; 
    this.SerialNumber = _SerialNumber; 
    this.Description = _Description; 
    this.SourceType = _Type; 
    this.HalfLife = _HalfLife; 
    this.CertificationDate = _CertDate; 
    this.CertifiedActivity = _CertActivity; 
    this.Energy_Band = _E; 
} 

public Radioactive_Source(string _Name,RadiationType _Type) 
{ 
    this.Name = _Name; 
    this.SourceType = _Type; 
} 


#endregion 

.............

+1

@InseokBeak您需要了解[方法重載]之間的差值(http://csharpindepth.com/Articles/General/Overloading.aspx)和'你在哪裏創建屬性'名稱類Constructors', SerialNumber,...等'瞭解什麼是仿製藥關於列表其中'T'在你的情況是一個'類'請顯示所有相關的代碼。 – MethodMan

+2

這些不是單純的或常規的方法;他們是建設者。使用不同數量的參數不會使其成爲不同的類型。你有什麼只是兩種方式來創建一個同樣的事情的實例。您可能需要繼承 – Plutonix

+0

您沒有兩種名爲Radioactive_Source的方法。你有一個類有兩個構造函數,這是不同的。 –

回答

1

嗯,你的意思六項,(因爲ListOfSources實際上是一個集合,List<Radioactive_Sources>)而不是六個參數?如果是你的情況,那麼

ListOfSources = new List<Radioactive_Sources>() { 
    // Put as many items as you want here 
    new Radioactive_Sources(), //TODO: put the right constructors here 
    new Radioactive_Sources(), 
    }; 
+0

我認爲OP需要2種不同的類型,基於:'因爲它們用於不同的目的''它看起來像他們試圖將2個arg版本存儲在與6個arg版本不同的集合中。 – Plutonix

+0

注意到構造Radioactive_Source和列表之間的混淆。 –

0

列表對象將只包含放射性類的對象的數組或列表。您需要使用任何您喜歡的構造函數方法分別初始化每個放射性對象,並將其添加到列表中。 就是這樣。

Radioactive obj1=new Radioactive(2 or 6 or whatever args); 

mylist.add(obj1);