2016-08-07 2 views
2

많이 검색되었지만 이것을 이해할 수는 없습니다. 몇 가지 속성을 가진 클래스 갑옷을 입고 있습니다. 나는 그것을 추가 할 수 있으며, 나는 또한 정렬을 추가하는 함수를 통해 그것을하고있다. 생성자가있는 클래스를 기반으로 항목을 일반 목록에서 제거 하시겠습니까?

public class Item : IComparable<Item> 
{ 
    public string name, description; 
    public bool stackable; 
    public int value; 
    public string coin_type; 
    public int weight; 
    // public string model_path; 

    public Item (string c_name, string c_description, bool c_stackable, int c_value, string c_coin_type, int c_weight) 
    { 
     name = c_name; 
     description = c_description; 
     stackable = c_stackable; 
     value = c_value; 
     coin_type = c_coin_type; 
     weight = c_weight; 
     // model_path = c_model_path; 
     //, string c_model_path) 
    } 
    public int CompareTo(Item other) 
    { 
     return String.Compare (name, other.name); // a < ab < b (sortowanie wg. litery) 
    } 
} 

가 그럼 난 클래스 항목에서 파생되는 "아이"클래스가 :

나는 클래스 항목이 있습니다.

public void AddToCharacterInventory(Item it) 
    { 
     if (it is Weapon) 
     { 
      charInvWeapon.Add((Weapon)it); 
      charInvWeapon.Sort(); 
     } 
     else if (it is Armor) 
     { 
      charInvArmor.Add((Armor)it); 
      charInvArmor.Sort(); 
     } 
} 

은 그럼 항목을 추가 :

AddToCharacterInventory(new Armor("Leather armor", "Description.", false, 10, "gp", 10, "Leather", "Light Armor", 11, 0, "")); 

charInvArmor 이름 목록에 추가되는 것

public class Armor : Item, IComparable <Armor> 
{ 
    public string armor_prof; 
    public string armor_category; 
    public int armor_class; 
    public int armor_str_req; 
    public string armor_stealth_mod; 
    //public string armor_property; 
    public Armor (string c_name, string c_description, bool c_stackable, int c_value, string c_coin_type, int c_weight, string c_armor_prof, string c_armor_category, int c_armor_class, int c_armor_str_req, string c_armor_stealth_mod) : base (c_name, c_description, c_stackable, c_value, c_coin_type, c_weight) 
    { 
     armor_prof = c_armor_prof; 
     armor_category = c_armor_category; 
     armor_class = c_armor_class; 
     armor_str_req = c_armor_str_req; 
     armor_stealth_mod = c_armor_stealth_mod; 
    // armor_property = c_armor_property; 
    } 

항목을 추가하고 정렬하는 기능입니다. 이제, 일부 속성을 기반으로 제거하는 방법은 무엇입니까? 예를 들어, "Leather Armor"라는 이름으로. 그래서 클래스 아머가 클래스 항목을 상속하는 public string name입니다. 단순한 문자열 인 경우 목록에서 위치를 쉽게 제거 할 수 있지만 유형이 속성을 갖는 클래스 인 경우에는이 작업을 수행하는 방법은 무엇입니까?

답변

2

예를 들어 다음과 같은 고려, 그것은 올바른 방향으로 당신을 안내합니다 :) 당신이 할 수있는 다른 방법이

static void Main() 
{ 
    var items = new List<Item>() 
    { 
     new Item("Axe", true), 
     new Item("Armor", false), 
     new Item("Horse", false) 
    }; 

    var remove = items.FirstOrDefault(item => 
    { 
     return item.Name.Equals("Armor", StringComparison.InvariantCultureIgnoreCase) && 
       !item.Active; 
    }); 

    if (remove != null) 
     items.Remove(remove); 

    Console.WriteLine(string.Join(", ", items.Select(item => item.Name))); 
    Console.ReadLine(); 
} 

public class Item 
{ 
    public Item(string name, bool active) 
    { 
     Name = name; 
     Active = active; 
    } 

    public string Name { get; set; } 

    public bool Active { get; set; } 
} 

example

1

. 한 가지 방법은 개체를 찾을 수 다음과 같이 제거하는 것입니다

var item= charInvArmor.First(c=>c.Name =="something"); 
charInvArmor.Remove(item); 

다른 방법이 구현되어 IEquatable (See this post)

RemoveAt을 사용하는 것도 가능하지만, 나 자신은 보통 방법 항목

를 사용
+0

좋습니다.하지만 문자열을 비교하는 더 좋은 방법이 있습니다. 문자열을 비교할 때 수행 할 비교 유형을 명시 적으로 지정하는 메소드를 사용해야합니다. –

+0

@MikkoViitala 감사합니다. 그러나 그는 그것에 대해 묻지 않았습니다. –

+0

네, 맞습니다. 문자열을 비교할 때 == 및! = 연산자를 사용하지 않는 것이 가장 좋습니다. 복사/붙여 넣기 예제는 다른 사람의 프로그램에 삽입 될 수 있습니다. –