如何从基本方法返回派生类型

时间:2014-10-10 18:41:27

标签: c# class return base derived

为了更好地理解,我想做的是:

Dog dog = Animal.Color("Red").Paw(4); 
Bird bird = Animal.Color("Blue").Wing(1);

使用以下代码,我可以执行以下操作:

Dog dog = Animal.Paw(4).Color("Red"); 
Bird bird = Animal.Wing(1).Color("Blue");

但我想先做第一条。

到目前为止,这是我的代码设计:

public class Animal 
{
    public static string color = "";

    public Animal Color( string _color )
    {
        color = _color;

        return this.GetType() ;
    }
}

public class Dog : Animal 
{
    public static int pawNumber = 0;

    public static Dog Paw( int _pawNumber )
    {
        pawNumber = _pawNumber;

        return this;
    }
}

public class Bird : Animal 
{
    public static int wingNumber = 0;

    public Bird Wing( int _wingNumber )
    {
        wingNumber = _wingNumber;

        return this;
    }
}

所以,基本上,它不起作用,因为Color被输入为Animal,即使我返回this.GetType()(它给我正确的类型),返回值也不会根据需要输入。

希望我足够清楚,有人可以帮助我!

4 个答案:

答案 0 :(得分:0)

你可以这样做。它真的很难看,但却按照你的要求行事。

class Program
{
    static void Main(string[] args)
    {
        Dog dog = new Dog().Color("Red").Paw(4);
        Bird bird = new Bird().Color("Blue").Wing(1);
    }
}

public abstract class Animal
{
    private string color = "";
    public Animal Color(string _color)
    {
        color = _color;
        return this;
    }
}

public abstract class ChainingAnimal<T> : Animal
    where T : Animal
{
    public T Color(string _color)
    {
        return (T)base.Color(_color);
    }
}

public class Dog : ChainingAnimal<Dog>
{
    private int pawNumber = 0;
    public Dog Paw(int _pawNumber)
    {
        pawNumber = _pawNumber;
        return this;
    }
}

public class Bird : ChainingAnimal<Bird>
{
    private int wingNumber = 0;
    public Bird Wing(int _wingNumber)
    {
        wingNumber = _wingNumber;
        return this;
    }
}

答案 1 :(得分:0)

实现你想要实现的目标是不可能的。

据我了解,您希望Animal.Color("Red")在第一行返回Dog个实例。 但是,您希望Animal.Color("Blue")返回Bird个实例。

你真的希望编译器猜测这两个实例中的哪一个会返回给你。

答案 2 :(得分:0)

我无法告诉完全你正在尝试做什么 - 我会注意到尝试构建一个继承层次结构,其中基类知道子类的细节是痛苦和痛苦的食谱。我也不明白为什么有爪子会让动物成为狗而有翅膀使它成为鸟;猫和蝙蝠会有所不同。

那就是说,沿着这些方向做的事情会做你想要完成的出现的事情,那就是在基类中有静态方法,允许用参数创建已知子类的实例不同的订单:

public class Animal
{
    public string Color { get; private set; }
    public Animal(string color)
    {
        this.Color = color;
    }

    // These static methods **really** should be in a separate
    // factory class
    public static Bird CreateBird(string color, int wings)
    {
        return new Bird(color, wings);
    }
    public static Bird CreateBird(int wings, string color)
    {
        return new Bird(color, wings);
    }
    public static Dog CreateDog(string color, int paws)
    {
        return new Dog(color, paws);
    }
    public static Dog CreateDog(int paws, string color)
    {
        return new Dog(color, paws);
    }
}
public class Bird : Animal
{
    public int Wings { get; private set; }
    public Bird(string color, int wings)
        : base(color)
    {
        this.Wings = wings;
    }
}
public class Dog : Animal
{
    public int Paws { get; private set; }
    public Dog(string color, int paws)
        : base(color)
    {
        this.Paws = paws;
    }
}

用法如下:

public void OneCodeSample()
{
    Dog dog = Animal.CreateDog("Red", 4);
    Bird bird = Animal.CreateBird("Blue", 1);
    // do something useful...
}
public void AnotherCodeSample()
{
    Dog dog = Animal.CreateDog(4, "Red");
    Bird bird = Animal.CreateBird(1, "Blue");
    // do something else useful...
}

答案 3 :(得分:-1)

您应该返回this而不是this.GetType()。后者的结果是Type

类型