English 中文(简体)
1. 书写家庭类型通用的
原标题:Writing a generic API for a type family

我与一个小的阶层一起工作,像以下那样,我要说,在我悲伤的无助世界中,没有任何其他种类的动物(我不担心能否恢复扩大):

public abstract class Animal {};
public sealed class Dog : Animal {};
public sealed class Cat : Animal {};
public sealed class Turtle : Animal {};
public sealed class Bird : Animal {};

我只想像APIC那样对待所有动物,但很明显,在以下情况下,这些动物的反应很少不同:

public class AnimalCare {
    public void Feed<T> (T pet) where T: Animal;
    public T PickUp<T> (PetStore store);
}

首先,在不给动物类以饲料方法的情况下,模糊了喂养的想法(动物,我知道这个例子正在达到这一目的,但为了证明动物动物体是我的动物体体,而Imtty adamant是担心分离的)会暗示一名游客。 但是,我真的想要做的是,让以上是动物饲养业唯一需要担心的亚特兰大消费者,而我做的是以下事情:

public class AnimalCare {
    public void Feed<T> (T pet) where T : Animal;
    public T PickUp<T> (PetStore store);
    public void Feed<Dog> (Dog rover)
    {
        // dump out alpo, lift toilet seat
    }
    public void Feed<Turtle> (Turtle franklin)
    {
        // do turtles eat? let him figure it out himself.
    } // ...etc.

    public Dog PickUp<Dog> (PetStore store)
    {
        // get bone, tennis ball, leash
    }
    public Bird PickUp<Bird> (PetStore store)
    {
        // make sure not dead, nailed to perch
    } // ...etc.
}

因此。 我知道,第一部分(Fed(Fed)方法)在不需要一般用途的情况下对超负荷工作进行罚款,但是,这仍然给我留下了为PickUp(因为正如我上文所列举的那样,执行法律并不合法)而实行的宽松措施,例如PickUpDog/PickUpBird等。 我非常希望避免对动物中心消费者及其志同道合的朋友单独提出“观点”,以便他们必须关心。

我在玩弄专门班子和其他假肢,试图组成或交替,但我似乎无法正确,我 st。 是否有像我所希望的那样做些什么的干净方法,或者我是否辞职为每个具体动物执行动物保护区?

http://www.ohchr.org。

Joel谈到工厂/储藏点,我认为还有一点。 更合理的做法是:

public class AnimalPresentation {
    public void Show (Dog dog);
    public void Show (Cat cat);
    //..etc.
    public Animal Get (PetStore store);
}

这首先会让我相信。 在“Get”时已知的“PetStore”下游动物类型,但在“Get”的一般实施中,一旦确定这种类型,它就分到特定超负荷。 佩特施店的具体子类是在这里的最佳/唯一方式吗?

最佳回答

我认为,<代码>AnimalCare的问题在于,它基本上是一个用于<代码>Animal的工厂,或者至少可以进入<代码>Animal的存放处。 因此,可能的话,<代码>PickUp功能应当退回,而不是特定类型。

或者,您不能将<代码>AnimalCare具体类型(AnimalCare<T>)作为基准? 实例很少难以衡量实际意图,因此,如果这些想法似乎没有打上标记的话,就会出现道歉。

问题回答

edit
You might consider something like this:

首先,与你们的动物有<条码>。 他们不了解任何方法。 然后,建立新的接口,如:

interface IAnimalCareClient<T>
{
    void Init(T animal);
    void Feed();
    T Pickup(PetStore store);
}

之后,每个动物类,如动物类,都建立了客户。

class DogClient : IAnimalCareClient<Dog>
{
    void Init(Dog animal) { //bla }
    void Feed() {}
    Dog Pickup(PetStore store) { //bla again }
}

然后,你的客户应当存放在一份清单中,这些类型可以居住在不同的批次中。 你只是想一想。

然后是公正的

class AnimalCare
{
    void Feed<T>(T animal)
    {
         //GrabWorkerFromStack<T>() should return the type IAnimalCareClient<T>
         IAnimalCareClient<T> worker = Activator.CreateInstance(GrabWorkerFromStack<T>());
         worker.Init(animal);
         worker.Feed();
    }
}

original answer
Basically you want to lay the responsibility at the animal itself, as you cannot force your AnimalCare class to implement all properties for all animals. Then becomes something simple like:

interface IAnimal
{
    void Feed();
    IAnimal Pickup(PetStore store);
}

class Dog : IAnimal
{
    void Feed() { /* feed */ }
    IAnimal Pickup(PetStore store) { /* grab animal and return */ }
}

class AnimalCare
{
    void Feed(IAnimal animal)
    {
        animal.Feed();
    }

    T Pickup<T>(T animal, PetStore store) where T: IAnimal
    {
         return (T)animal.Pickup(store);
    }
}

你们可以使用接口,我不知道有哪些具体要求,但如果动物可能拥有一个与其他动物共享的海峡,但必须有一个接口是一个良好的开端。

例如:

interface IFeedable
{
    void Feed();
}

interface IMammal<TChild>
    where TChild: IMammal<TChild>
{
    IEnumerable<TChild> Reproduce();
}

class Dog: IFeedable, IMammal<Puppy>
{
    // ...
}

我利用了所有时间的交接(可能过度使用),特别是在通常继承可能不是最佳方式的情况下。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签