2016-10-03 49 views
0

我有一个要求,以增加不同类型的对象(即使它们是不同的,它们共享相同的基类,它们是兄弟姐妹。我怎么可以在不同类型的对象存储在C#泛型列表

考虑这个类层次

public abstract class Fruit 
{ 
    public int Quality { get; set; } 

    public string Name { get; set; } 

} 

public sealed class Apple :Fruit 
{ 
    public string Color { get; set; } 

    public Apple(string color ="Red") 
    { 
     Color = color; 
    } 
} 

public sealed class Orange : Fruit 
{ 
    public Orange(string type = "WithSeed") 
    { 
     Type = type; 
    } 
    public string Type { get; set; } 
} 

我怎样才能做这样的事情

_apples = new List<Apple> 
     { 
      new Apple 
      { 
       Name = "Fiji Apple", 
       Quality = 9, 
       Color ="Green" 
      }, 
      new **Orange** 
      { 
       Name = "Indi Orange", 
       Quality = 7, 
      } 
     }; 

这个泛型列表的消费者会使用它使用的基类,例如:使用IEnumerable<Fruit>。 有人可以建议吗?

回答

2

有一个Fruit的列表。这样,该列表将包含任何Fruit对象以及从Fruit继承的任何类型的对象。

List<Fruit> _list = new List<Fruit> 
{ 
    new Apple 
    { 
     Name = "Fiji Apple", 
     Quality = 9, 
     Color ="Green" 
    }, 
    new Orange 
    { 
     Name = "Indi Orange", 
     Quality = 7 
    } 
}; 
+0

可以在不使用基类的情况下完成。我的意思是说,即使不同类型的物体(苹果,橙子,菠萝等)被传递来准备这个数组,它们也应该被视为苹果。 – ViBi

+0

我不知道在这方面“投射”是什么意思。你是否希望软件根据你对橙色的描述来组成苹果?橙色没有颜色属性,所以它怎么能做到这一点?你不能混合苹果和橙子;) –

+0

你会如何将橙色作为一个苹果? “Apple”有一大堆“Orange”没有的东西,反之亦然。您将不得不创建一个新的Apple对象,该对象尽可能与原始的“Orange”对象共享,但是您将失去对原始对象的引用以及所有“Orange”特定信息。 – Abion47

1

列表必须总是知道它包含哪种类型。因此,为了使其包含AppleOrange,您必须将该列表定义为包含任何超类或接口,该超类或接口涵盖您打算放入列表中的所有项目。

在这种情况下,这将使List<Fruit>List<object>可能包含两者。

+1

你是正确的,因为列表本身并不需要知道它包含的项目的任何内容,它只是内部指针或结构列表,这就是为什么'对象'足够好。然而,编译器需要知道,以便它可以键入检查放入列表中的项目。 – Cine

0

为此目的使用类层次结构是一个常见的错误。 有一个接口并为该接口创建一个List,那么您甚至可以存储不属于该类层次结构的对象。

当您需要存储不同类型的对象时,只需实现该类型的接口,这样您就不必对存储列表代码进行任何更改。

0

由于所有的物体从水果继承就足够了,如果我有一个类,它not extend Fruit必须创建一个List of objects预定义了所有类型(值类型和引用创建的ListFruit

List<Fruit>_apples = new List<Fruit>{...} 

和用户定义的)从Object直接或间接继承。

List<Object>_apples = new List<Object>{...} 

请记住,如果你不延长水果不能访问诸如qualitycolor

0

使用此代码及其属性的列表

List<Fruit> fruits = new List<Fruit>(); 
      fruits.Add(new Apple("Red")); 
      fruits.Add(new Orange("Withseed")); 
0

我相信添加不同的对象你想要做的是使用不同类型的Fruit的列表来存储它们或将它们传递给List,而不需要单独的List<Apple>,List<Orange>等,但你仍然需要使用类型特定的东西(如Apple.ColorOrange.Type)。

为了演示这一点,我创建了一个​​类来存储所有不同的Fruit对象和Project()它们的独特属性。

public class FruitBasket 
{ 
    public List<Fruit> FruitList { get; } 

    public FruitBasket(List<Fruit> fruitList) 
    { 
     FruitList = fruitList; 
    } 

    public List<string> Project() 
    { 
     var result = new List<string>(); 
     foreach (var fruit in FruitList) 
     { 
      if (fruit is Apple) 
      { 
       var apple = (Apple) fruit; 
       result.Add("This is a " + apple.Color + " " + apple.Name); 
      } 
      else if (fruit is Orange) 
      { 
       var orange = (Orange) fruit; 
       result.Add("A " + orange.Name + " with type: " + orange.Type); 
      } 
      else 
      { 
       result.Add("An unknown " + fruit.Name); 
      } 
     } 
     return result; 
    } 
} 

虽然循环通过各种不同的Fruit的对象,它的检查水果的种类,并把唯一的字符串在列表中。

这个例子中的用法是:

var result = new FruitBasket(new List<Fruit> 
{ 
    new Apple 
    { 
     Name = "Fiji Apple", 
     Quality = 9, 
     Color = "Green" 
    }, 
    new Orange 
    { 
     Name = "Indi Orange", 
     Quality = 7, 
    } 
}).Project(); 

而且result将是:

这是一个绿色的斐济苹果

一悠橙类型:WithSeed

相关问题