Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从基方法返回派生类型_C#_Class_Return_Base_Derived - Fatal编程技术网

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

C# 如何从基方法返回派生类型,c#,class,return,base,derived,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 {

为了更好地理解,我想做以下几点:

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;
    }
}
因此,基本上,它不起作用,因为颜色的类型是Animal,即使我返回这个.GetType()(它给了我正确的类型),返回值也不是根据需要键入的


希望我足够清楚,有人能帮助我

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

您可以这样做。这真的很难看,但你问什么就做什么

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;
    }
}
类程序
{
静态void Main(字符串[]参数)
{
狗=新狗()颜色(“红色”)。爪子(4);
鸟=新鸟()颜色(“蓝色”)。翅膀(1);
}
}
公共抽象类动物
{
私有字符串颜色=”;
公共动物颜色(字符串颜色)
{
颜色=_颜色;
归还这个;
}
}
公共抽象类ChainingAnimal:Animal
T:动物
{
公共T颜色(字符串颜色)
{
返回(T)基色(_色);
}
}
公家犬:链式动物
{
私有整数=0;
公共狗爪(内爪编号)
{
pawNumber=_pawNumber;
归还这个;
}
}
公共级鸟类:链状动物
{
私有整数wingNumber=0;
公共鸟翼(内部翼号)
{
wingNumber=_wingNumber;
归还这个;
}
}

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

据我所知,您希望
Animal.Color(“Red”)
在第一行返回
Dog
实例。 但是,您希望
Animal.Color(“Blue”)
返回一个
Bird
实例


实际上,您希望编译器猜测这两个实例中的哪一个会返回给您。

我不能确切地告诉您想做什么-我要指出,试图构建一个继承层次结构,让基类知道子类的详细信息,这会带来痛苦和痛苦。我也不明白为什么有爪子的动物会变成狗,而有翅膀的动物会变成鸟;猫和蝙蝠会不敢苟同

这就是说,沿着这条线做的事情将实现您试图实现的目标,即在基类中使用静态方法,允许创建具有不同顺序参数的已知子类的实例:

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...
}

您的代码示例有点胡说八道,因为您有返回this
的静态方法,而在静态方法中没有this
。返回
this.GetType()
是无意义的,因为返回类型是
Animal
,没有
this
,而
GetType()
将返回
type
对象。然而,我认为你无论如何都不想要这些。更好地解释你的实际问题,但我认为你可能需要考虑一个代码< > AnimalFactory <代码>类,方法名如<代码> CealOutOG,<代码> CureBoe<代码>等(<代码>动物< /代码>本身不应该与派生类有关。)对不起,到处都是静态的,我很难从代码中重写它。我是新来的。关于我试图实现的目标,正如问题一开始所说的,现在还不清楚你想要实现什么。我假设您来自函数式语言背景,其中没有状态或变量,每个调用都是另一个调用的结果。在面向对象语言中,情况并非如此。在OO中,方法应该返回在其主体上下文中有意义的值。在本例中,您可以调用
Paw()
来设置一个私有字段,但不需要返回实例对象,因为您已经可以在调用者方法中访问它(即
dog
变量)。你能确切地解释一下你想要实现什么吗?visual studio本身显示了你代码中的错误。这个想法是所有动物都有一些共同点,但它们也有一些特定的属性。我想设置一些我喜欢的属性。所以返回“this”允许我做一些类似的事情:动物。颜色(“红色”)。爪子(3)。攻击性(“低”)。尾巴(“短”)。其中颜色和攻击性来自基类(动物),爪子和尾巴来自派生类。然后,颜色、攻击性、爪子、尾巴都是可选的,我可以按我想要的顺序来调用它们,而不是特定的顺序。如果我返回这个,我会得到错误:类型“Animal”不包含“Paw”的定义,并且找不到类型“Animal”的扩展方法“Paw”。我不知道为什么它被认为是丑陋的,但它完全按照需要工作。感谢您抽出时间回答,最重要的是,您试图理解我的问题。为了更好的理解和知识,你能告诉我应该在Google上输入什么关键字来了解更多关于这个语法的信息吗?你试图实现的语法叫做“方法链接”。当您还需要继承时,让它正常工作要麻烦得多,但正如您所看到的,这是可能的。我称之为丑陋,因为您需要另一个类“ChainingAnimal”,以便能够重用Animal类的功能。在这个例子中,这没关系,但是想象一下,您将进一步使用您的类层次结构,例如,从Dog继承Bulldog。然后你需要一个链狗来让Bulldog拥有狗的功能。谢谢你的回答。但我在评论中说。我想避免使用多参数方法,而更喜欢使用多选项方法。可以被发现是丑陋的,但满足我的需要。