gpt4 book ai didi

c# - 指定我需要一个实现 2 个或更多接口(interface)的对象的接口(interface)引用

转载 作者:行者123 更新时间:2023-11-30 23:11:10 25 4
gpt4 key购买 nike

假设我这里的代码过于简单:

class Person
{
public int age;
public string name;
public Person(int age, string name)
{
this.age = age;
this.name = name;
}
}
public class MySimpleDatabase
{
List<Person> people;

internal List<Person> People
{
get
{
if(people == null)
{
people = new List<Person>();
people.Add(new Person(24, "ASD"));
people.Add(new Person(35, "QWE"));
people.Add(new Person(12, "MNB"));
}
return people;
}
}
}
public class ProcessPeopleConcrete
{
public void WriteNames()
{
List<Person> people = new MySimpleDatabase().People;
foreach (var person in people)
{
Console.WriteLine(person.name);
}
}
}
public class ProcessPeopleInterface
{
public void WriteNames()
{
//question for here.
IEnumerable<Person> people = new MySimpleDatabase().People;
foreach (var person in people)
{
Console.WriteLine(person.name);
}
}
}

这里我有具体的处理器以及基于接口(interface)的处理器。这里的接口(interface)处理器当然更易于维护,但据我所知

   //edited here to clarify

我只能为我需要的变量指定一个类型,在我的例子中是IEnumerable<Person> .

如果我需要的东西不是一个,而是两个接口(interface)同时,与每个都无关怎么办其他(一个不实现另一个)?

假设我需要任何必须实现两者的集合 ICloneableIEnumerator .在评论中正确地指出我可以定义另一个实现它们的接口(interface)。但是如果我使用预定义的集合,我就不能那样做,因为那样我就不能扔掉它们中的任何一个,因为它们显然没有实现我定制的接口(interface)。

在这种情况下,我会为变量 people 指定哪种类型(评论为“这里有问题”)?如果有像 <IEnumerable, ICloneable> people; 这样的变量声明这在想象中意味着我需要实现两者的东西 IEnumerableICloneable 。但是有没有类似的语言特征,或者它就是我所说的 - 只是虚构的?

最佳答案

如您所知,您不能强制类实现它们不实现的接口(interface) ;)

您还注意到接口(interface)用于解决特定问题。在一种情况下,他们解决了如何枚举集合,在另一种情况下,他们解决了如何克隆对象。

根据您的问题,我认为您希望能够说明返回的对象解决了这两个问题,并且您想知道如何在方法契约中说明这一点。

您可以通过定义一个新接口(interface)来实现:

public interface ISuperClonableList<T> : IEnumerable<T>, IClonable
{
}

就是这样。

如果列表本身不可复制,您需要将其包装在一个实现这两个接口(interface)的新类中。将实际列表作为构造函数参数传递,并在每个 IEnumerable 方法实现中调用它。

克隆接口(interface)的问题在于它们通常不指定它应该是深克隆还是浅克隆。了解这一点很重要,因为如果包含的项目是可变的,它可能会导致大问题。

从 ReSharper 生成的示例:

public class MyListWrapper<T> : ISuperClonableList<T>
{
private readonly ISuperClonableList<T> _innerList;

public MyListWrapper(ISuperClonableList<T> innerList)
{
_innerList = innerList;
}

public IEnumerator<T> GetEnumerator()
{
return _innerList.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable) _innerList).GetEnumerator();
}

public void Add(T item)
{
_innerList.Add(item);
}

public void Clear()
{
_innerList.Clear();
}

public bool Contains(T item)
{
return _innerList.Contains(item);
}

public void CopyTo(T[] array, int arrayIndex)
{
_innerList.CopyTo(array, arrayIndex);
}

public bool Remove(T item)
{
return _innerList.Remove(item);
}

public int Count
{
get { return _innerList.Count; }
}

public bool IsReadOnly
{
get { return _innerList.IsReadOnly; }
}

public int IndexOf(T item)
{
return _innerList.IndexOf(item);
}

public void Insert(int index, T item)
{
_innerList.Insert(index, item);
}

public void RemoveAt(int index)
{
_innerList.RemoveAt(index);
}

public T this[int index]
{
get { return _innerList[index]; }
set { _innerList[index] = value; }
}
}

关于c# - 指定我需要一个实现 2 个或更多接口(interface)的对象的接口(interface)引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44901808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com