2016-09-25 56 views
1

新的這裏,我一直在學習C#約一個月。(C#)訪問/修改列表中的對象

不管怎樣,我一直在尋找的StackOverflow幾天的現在,但沒有找到一個明確的答案我的問題......

//Here's my Class 

public class Guy 
{ 
    public static int ID { get; set; } 
    public static int LifeExpectancy { get; set; } 
    public static bool Living { get; set; } 

    public Guy(int id, int lifeExpectancy, bool living) 
    { 
     ID = id; 
     LifeExpectancy = lifeExpectancy; 
     Living = living; 
    } 
} 

我想要做的就是創建一個「someGuy」對象的具體數量,然後將它們放到一個公開名單使用此方法...

public static List<Guy> Guys = new List<Guy>(); 

public static void makeSomeGuys(int howManyGuys) 
{ 
    for (int i = 0, i <= howManyGuys; i++) 
    { 
     int id = i; 
     int lifeExpectancy = 80; 
     bool alive = true; 

     Guys.Add(New Guy(id, lifeExpectancy, alive)); 

     Console.WriteLine("Made a new Guy {0}", id);   
    } 
    return; 
} 

問題的重要性排列:

如何訪問特定對象及其參數?(從列表「夥計」。訪問)

如何從這個名單訪問對象在另一個類?(不,我絕對需要,我很好奇)

我可以搜索對象在使用它的參數列表?(而不是做這樣的事情... humanPopulation[number]

我是否應爲已對其參數修改對象的新名單?(而不是把它留在原來的列表)

是否可以從列表中刪除項目?(只是一般,是一個東西的人嗎?如果是這樣,爲什麼?)

我真的只需要回答的第一個問題。其餘的只是獎金。謝謝!

+2

你看過了'API文檔List'?這裏有一個鏈接:https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx – wulfgarpro

回答

0
using System; 
using System.Collections.Generic; 
using System.Linq; 

namespace test 
{ 
    public class Guy 
    { 
     private int m_ID; 
     private int m_LifeExpectancy; 
     private bool m_Living; 

     public int ID 
     { 
      get { return m_ID; } 
      set { m_ID = value; } 
     } 
     public int LifeExpectancy 
     { 
      get { return m_LifeExpectancy; } 
      set { m_LifeExpectancy = value; } 
     } 
     public bool Living 
     { 
      get { return m_Living; } 
      set { m_Living = value; } 
     } 

     public Guy(int id, int lifeExpectancy, bool living) 
     { 
      ID = id; 
      LifeExpectancy = lifeExpectancy; 
      Living = living; 
     } 
    } 

    public class MyFactory 
    { 
     public IList<Guy> MakeSomeGuys(int howManyGuys) 
     { 

      IList<Guy> localGuys = new List<Guy>(); 

      for (int i = 0; i <= howManyGuys; i++) 
      { 

       int id = i; 
       int lifeExpectancy = 80; 
       bool alive = true; 

       localGuys.Add(new Guy(id, lifeExpectancy, alive)); 

       Console.WriteLine("Made a new Guy {0}", id); 
      } 


      return localGuys; 
     } 
    } 

    public class program 
    { 
     public void Main() 
     { 
      MyFactory mf = new MyFactory(); 
      IList<Guy> guys = mf.MakeSomeGuys(5); 

      //How do I access a specific object as well as its parameters? (Accessing from the list "Guys".) 
      int GetFirstGuyId = guys.FirstOrDefault().ID; //LEARN LINQ 

      //How do I access an object from this list in another class? (Not that I absolutely need to, I'm curious) 
      //you need to learn about object oriented encapsulation for better understanding. 

      //Can I search for an object in a list by using its parameters? (As opposed to doing something like...humanPopulation[number]) 
      Guy guyById = guys.Where(g => g.ID == 5).FirstOrDefault(); // returns the first match (need to learn lambda expression) 

      //Should I create a new list for objects that have had their parameters modified? (As opposed to leaving it in the original list) 
      // you need to learn about passing values by value/reference (by reference you already changing the original!). 

      //Is it possible to remove items from a list? (Just in general, is that a thing people do? if so, why?) 
      //yes 
      guys.Remove(guyById); 

     } 
    } 
} 
+0

第一個問題可以是:int firstGuyLifeExpectancy = guys [0] .LifeExpectancy; –

+0

我不明白你寫的代碼的最上面的部分,當你糾正我的錯誤封裝。按照你這樣做的方式,是否有區別?或者它是正確的? – Grover

+0

這樣你就可以控制你的輸入/輸出get/set例如設置值,只有當它大於零時,等等...... –

0

您可能不熟悉C#和麪向對象編程,因此我在此答案中包含了一些很好的鏈接。

關於質詢的1只:

首先,你Guy類屬性未正確封裝。確保您正確地確定了,LifeExpectancyLiving屬性,如this文章中所示。

如果你想訪問一個特定項目,也就是具有特定ID的Guy,你會更好使用關聯容器一樣Dictionary

如果你高興的List容器,你需要使用上GuysFind方法,如在鏈接的例子。你會注意到術語謂詞在文檔中,this鏈接會詳細說明。

3

首先,你需要從蓋伊類的屬性中刪除靜態修改,即:

public int ID { get; set; } 
public int LifeExpectancy { get; set; } 
public bool Living { get; set; } 

因爲靜電造成的財產是類本身的屬性,而不是實例班級(個人'傢伙')。

要訪問的第一個男人的預期壽命(零):

Console.WriteLine(Guys[0].LifeExpectancy); 

要進入第五個傢伙的壽命:

Console.WriteLine(Guys[4].LifeExpectancy);