2015-11-08 52 views
-4

我需要創建一個類的未知數量的實例,並且能夠在運行時跟蹤它們。示例用戶會說他們想創建_n數量的汽車。應用程序然後實例化car01 - car_n,並且在應用程序運行時,用戶可以使用汽車「X」,並且實例將跟蹤其自己的里程。如何實例化汽車實例,以及如何引用特定實例? 我知道我可能有班級和數量可觀的預定義汽車,但似乎如果用戶想要創建5或6輛汽車,代碼可能會彈出car1-car6。如何在C#中動態實例化類?

class Program 
{ 
    static void Main(string[] args) 
    { 
     Random rnd = new Random(); 
     Foo[] FooCar = new Foo[5]; 
     string[] _NamesForFoo = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf" }; 
     int FoosToMake = rnd.Next(1, _NamesForFoo.Length); 
     for (int i = 0; i < FoosToMake; i++) 
     {     
      FooCar[i] = new Foo(_NamesForFoo[i], rnd.Next(100,500)); 
      Console.WriteLine("You just created a FooCar named {0} with a count of {1}.", 
      FooCar[i]._FooName, FooCar[i]._FooCount); 
     } 
    } 

    public class Foo 
    { 
     public string _FooName { set; get; } 
     public int _FooCount { set; get; } 

     public Foo(string _NameIn, int _CountIn) 
     { 
      _FooName = _NameIn; 
      _FooCount = _CountIn; 
     } 
    } 
} 

響應中提供的鏈接沒有回答這個例子的問題。收集或列表也不起作用。下面是一個例子

class Program 
{ 
    static void Main(string[] args) 
    { 
     Random rnd = new Random(); 
     string[] _NamesForFoo = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf" }; 
     int FoosToMake = rnd.Next(1, _NamesForFoo.Length); 

     List<Foo> FooCar = new List<Foo> 
     { 
      for (int i = 0; i < FoosToMake; i++) 
      {     
       new Foo(){_FooName = _NamesForFoo[i], _FooCount = rnd.Next(100,500)}; 
      } 
     }; 
    } 

    public class Foo 
    { 
     public string _FooName { get; set; } 
     public int _FooCount { get; set; } 

     public Foo(string _NameIn, int _CountIn) 
     { 
      _FooName = _NameIn; 
      _FooCount = _CountIn; 
     } 
    } 
} 
+0

爲什麼不使用任何類型的集合? – Dmitry

+5

[如何在c#中基於循環創建(n)對象]的可能重複(http://stackoverflow.com/questions/24071671/how-to-create-n-objects-in-c-sharp-based-on -loop) –

+0

使用集合是顯而易見的解決方案。真正的問題是以後如何訪問控件。這取決於你的要求。一種選擇是使用字典並使用字符串作爲標識符。另一個就是把它們放在一個ListView中,並讓用戶從中選擇一個CurrentCar ..兩者不是互斥的! – TaW

回答

1

你可以使用一個集合:

var cars = new List<Car>(); 

for (int i = 0; i < NumCarsNeeded; i++) 
{ 
    cars.Add(new Car()); 
} 

//Access car #1 (index 0) 
cars[0].Mileage += 5; 
0

我不知道如果我完全理解,但我覺得這裏是你在找什麼:

// To store common properties and methods, use an abstract class: 
public abstract class Car { 
    public string Make {get; set;}; 
    public string Model {get; set;}; 
// The rest of properties 
} 

// To store special data for a particular type of car: 
public class Van: Car 
{ 
    public string DoorType {get; set;} 
    // the rest of properties 
} 

public class Truck: Car 
{ 
//... 
} 

然後實例化和維護汽車的列表,使用多態:

List<Car> cars = new List<Car>(); 
Van v1 = new Van { Make="...", Model="...", DoorType="..." }; 
Truck t1 = new Truck { Make="...", Model="...", /*...*/ }; 

cars.Add(v1); 
cars.Add(v2); 

string doortype = (cars[0] as Van).DoorType; 
string model = cars[0].Model; 

我想你明白了。

0

原來這個類的數組是一個很好的選擇。這是有用的例子。

class Program 
{ 
    static void Main(string[] args) 
    { 
     Random rnd = new Random(); 
     Foo[] FooCar = new Foo[6]; 
     string[] _NamesForFoo = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf" }; 
     int FoosToMake = rnd.Next(1, _NamesForFoo.Length); 

     Console.WriteLine("Making {0} FooCars.", FoosToMake); 
     //create the cars 
      for (int i = 0; i < FoosToMake; i++)    
      {     
       FooCar[i] = new Foo(_NamesForFoo[i], rnd.Next(100,500)); 
      } 
     //list the cars 
      for (int i = 0; i < FoosToMake; i++) 
      { 
       Console.WriteLine("FooCar {0} has a count of {1}", FooCar[i]._FooName, FooCar[i]._FooCount); 
      } 
     //modify a car 
      FooCar[0]._FooCount += 2500; 
     //show change to specific car 
      Console.WriteLine("FooCar {0} now has a count of {1}", FooCar[0]._FooName, FooCar[0]._FooCount); 

     Console.ReadKey(); 
    } 

    public class Foo 
    { 
     public string _FooName { get; set; } 
     public int _FooCount { get; set; } 

     public Foo(string _NameIn, int _CountIn) 
     { 
      _FooName = _NameIn; 
      _FooCount = _CountIn; 
     } 
    } 
}