假设我想为宠物主人制定饮食计划,让他们保持宠物健康,我有以下课程:
// Pets
public class Pet { ... }
public class Dog : Pet { ... }
public class Cat : Pet { ... }
// Meals and diets
public class Meal<T> : ICollection<T> where T : Pet
{
...
private List<T> allowedPets { get; set; } = new List<T>();
...
}
public class Diet<T> : ICollection<Meal<T>> where T : Pet
{
...
private List<Meal<T>> dietMeals { get; set; } = new List<Meal<T>>();
...
}虽然我可以有一个宠物的清单,可以遵循一定的饮食,而不是一个宠物的名单,被允许吃某些食物,这只是一个例子。无论哪种方式,我都可以做一些像这样的任意饮食,在某种应用中使用它们。
// Dogs and their diets
Dog labrador = new Dog() { ... };
Dog dalmatian = new Dog() { ... };
Meal<Dog> mediumDogBreakfast = new Meal<Dog>(...) { labrador, dalmatian };
Meal<Dog> mediumDogLunch = new Meal<Dog>(...) { labrador, dalmatian };
Meal<Dog> mediumDogDinner = new Meal<Dog>(...) { labrador, dalmatian };
Diet<Dog> mediumDogDiet = new Diet<Dog>()
{
mediumDogBreakfast,
mediumDogLunch,
mediumDogDinner
};
// Cats and their diets
Cat siamese = new Cat() { ... };
Cat sphynx = new Cat() { ... };
Meal<Cat> orientalCatBreakfast = new Meal<Cat>(...) { siamese, sphynx };
Meal<Cat> orientalCatLunch = new Meal<Cat>(...) { siamese, sphynx };
Meal<Cat> orientalCatDinner = new Meal<Cat>(...) { siamese, sphynx };
Diet<Cat> orientalCatDiet = new Diet<Cat>()
{
orientalCatBreakfast,
orientalCatLunch,
orientalCatDinner
};但是如果我想把这些饮食放在一个单一的列表中,我会得到一个转换错误:
// This isn't allowed!
List<Diet<Pet>> = new List<Diet<Pet>>()
{
mediumDogDiet,
orientalCatDiet
};我的印象是,列表将允许Cat和Dog对象,因为Pet约束更一般,但显然这里有问题。我如何改变我的Pet类或集合实现,以允许一个列表,可以同时容纳狗和猫的饮食(或任何其他宠物衍生产品)?
发布于 2020-08-13 12:40:23
你需要互相配合:
注意到,这意味着您必须创建接口IDiet的集合。
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace ConsoleApp8
{
public class Pet { }
public class Dog : Pet { }
public class Cat : Pet { }
// Meals and diets
public interface IMeal<out T> : IReadOnlyCollection<T> where T : Pet
{
}
public interface IDiet<out T> : IReadOnlyCollection<IMeal<T>> where T : Pet
{
}
public class Meal<T> : Collection<T>, IMeal<T> where T : Pet
{
public List<T> AllowedPets { get; set; } = new List<T>();
}
public class Diet<T> : Collection<IMeal<T>>, IDiet<T> where T : Pet
{
public List<Meal<T>> DietMeals { get; set; } = new List<Meal<T>>();
}
class Program
{
static void Main(string[] args)
{
Dog labrador = new Dog() { };
Dog dalmatian = new Dog() { };
Meal<Dog> mediumDogBreakfast = new Meal<Dog>{ labrador, dalmatian };
Meal<Dog> mediumDogLunch = new Meal<Dog>{ labrador, dalmatian };
Meal<Dog> mediumDogDinner = new Meal<Dog>{ labrador, dalmatian };
Diet<Dog> mediumDogDiet = new Diet<Dog>()
{
mediumDogBreakfast,
mediumDogLunch,
mediumDogDinner
};
// Cats and their diets
Cat siamese = new Cat() { };
Cat sphynx = new Cat() { };
Meal<Cat> orientalCatBreakfast = new Meal<Cat>(){ siamese, sphynx };
Meal<Cat> orientalCatLunch = new Meal<Cat>(){ siamese, sphynx };
Meal<Cat> orientalCatDinner = new Meal<Cat>(){ siamese, sphynx };
Diet<Cat> orientalCatDiet = new Diet<Cat>()
{
orientalCatBreakfast,
orientalCatLunch,
orientalCatDinner
};
List<IDiet<Pet>> diets = new List<IDiet<Pet>>
{
mediumDogDiet,
orientalCatDiet
};
}
}
}https://stackoverflow.com/questions/63395171
复制相似问题