lib*_*ibi 8 c# queue inheritance
我是C#的新手,想知道C#中是否存在类似私有继承的东西(比如在C++中)?
我的问题如下:我想实现一个队列(将其命名为SpecialQueue),并进行以下更改:
在c ++中,我会从队列中私有ihnerit,只暴露我想要的方法,并根据我的意愿改变其他方法.但不幸的是,队列中的所有方法都没有"覆盖"修饰符,我不知道如何在C#中实现它.
有帮助吗?
问候,丹
Vla*_*lad 15
使用构图:包含一个通常Queue作为你的领域SpecialQueue.私有继承实际上与组合非常相似.
有关讨论,请参阅http://www.parashift.com/c++-faq-lite/private-inheritance.html#faq-24.3.
实施可能是这样的:
public class SpecialQueue<T>
{
private int capacity;
private Queue<T> storage;
public SpecialQueue(int capacity)
{
this.capacity = capacity;
storage = new Queue<T>();
// if (capacity <= 0) throw something
}
public void Push(T value)
{
if (storage.Count == capacity)
storage.Dequeue();
storage.Enqueue(value);
}
public T Pop()
{
if (storage.Count == 0)
throw new SomeException("Queue is empty");
return storage.Dequeue();
}
public int Count
{
get { return storage.Count; }
}
}
Run Code Online (Sandbox Code Playgroud)
如果要SpecialQueue支持它们,则需要添加更多功能/接口.但是我不建议实施IEnumerable,因为这将允许Peek(你想要禁止).