Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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# 减少继承构造函数的键入开销';s参数表_C#_Constructor_Copy Constructor - Fatal编程技术网

C# 减少继承构造函数的键入开销';s参数表

C# 减少继承构造函数的键入开销';s参数表,c#,constructor,copy-constructor,C#,Constructor,Copy Constructor,是否有任何语法和/或语言功能可以减少在继承构造函数中重新键入参数列表的开销 基类 public class Cartesian { public int X { get; set; } public int Y { get; set; } public Cartesian(int x, int y) { X = x; Y = y; } } public class Complex : Cartesian { pu

是否有任何语法和/或语言功能可以减少在继承构造函数中重新键入参数列表的开销

基类

public class Cartesian
{
    public int X { get; set; }
    public int Y { get; set; }

    public Cartesian(int x, int y)
    {
        X = x;
        Y = y;
    }
}
public class Complex : Cartesian
{
    public int I { get; set; }

    // all this retyping becomes daunting and results in duplicate code
    public Complex(int x, int y, int i) : base(x, y)
    {
        I = i;
    }
}
public class Cartesian
{
    public int X { get; set; }
    public int Y { get; set; }

    //public Cartesian(int x, int y) { Init(x, y); }

    public void Init(int x, int y)
    {
        X = x;
        Y = y;        
    }
}
public class Complex : Cartesian
{
    public Cartesian Base { get {return base;} }

    public int I { get; set; }

    //public Complex(int i) { Init(i); }

    public Complex Init(int i)
    {
        I = i;

        return this;
    }
}
继承类

public class Cartesian
{
    public int X { get; set; }
    public int Y { get; set; }

    public Cartesian(int x, int y)
    {
        X = x;
        Y = y;
    }
}
public class Complex : Cartesian
{
    public int I { get; set; }

    // all this retyping becomes daunting and results in duplicate code
    public Complex(int x, int y, int i) : base(x, y)
    {
        I = i;
    }
}
public class Cartesian
{
    public int X { get; set; }
    public int Y { get; set; }

    //public Cartesian(int x, int y) { Init(x, y); }

    public void Init(int x, int y)
    {
        X = x;
        Y = y;        
    }
}
public class Complex : Cartesian
{
    public Cartesian Base { get {return base;} }

    public int I { get; set; }

    //public Complex(int i) { Init(i); }

    public Complex Init(int i)
    {
        I = i;

        return this;
    }
}

这是一种解决方法,但它只是将重复代码移动到Init函数中,所以…:

基类

public class Cartesian
{
    public int X { get; set; }
    public int Y { get; set; }

    public Cartesian(int x, int y)
    {
        X = x;
        Y = y;
    }
}
public class Complex : Cartesian
{
    public int I { get; set; }

    // all this retyping becomes daunting and results in duplicate code
    public Complex(int x, int y, int i) : base(x, y)
    {
        I = i;
    }
}
public class Cartesian
{
    public int X { get; set; }
    public int Y { get; set; }

    //public Cartesian(int x, int y) { Init(x, y); }

    public void Init(int x, int y)
    {
        X = x;
        Y = y;        
    }
}
public class Complex : Cartesian
{
    public Cartesian Base { get {return base;} }

    public int I { get; set; }

    //public Complex(int i) { Init(i); }

    public Complex Init(int i)
    {
        I = i;

        return this;
    }
}
继承类

public class Cartesian
{
    public int X { get; set; }
    public int Y { get; set; }

    public Cartesian(int x, int y)
    {
        X = x;
        Y = y;
    }
}
public class Complex : Cartesian
{
    public int I { get; set; }

    // all this retyping becomes daunting and results in duplicate code
    public Complex(int x, int y, int i) : base(x, y)
    {
        I = i;
    }
}
public class Cartesian
{
    public int X { get; set; }
    public int Y { get; set; }

    //public Cartesian(int x, int y) { Init(x, y); }

    public void Init(int x, int y)
    {
        X = x;
        Y = y;        
    }
}
public class Complex : Cartesian
{
    public Cartesian Base { get {return base;} }

    public int I { get; set; }

    //public Complex(int i) { Init(i); }

    public Complex Init(int i)
    {
        I = i;

        return this;
    }
}
用法示例

var complex = (new Complex()).Init(i).Base.Init(x, y);

如果您拥有ReSharper 8,则有一个名为的功能

在ReSharper 8中出现的一种代码完成形式是 称为生成完成。生成完成的概念是 将代码完成作为更快、更直接的方法用于代码生成 比如说,生成菜单的替代品

换句话说,键入
ctorp
,然后按tab键生成ctor

public class Complex : Cartesian
{
    public int I { get; set; }

    ctorp//tab key
}
产生

public Complex(int x, int y) : base(x, y)
{
     I = x;
}
    //public Complex(int i) : base.ctorp
    public Complex(int x, int y, int i) : base(x, y)
    {
        I = i;
    }
    //public Complex(int i) : base.ctorp
    public Complex(int i) : base()
    {
        I = i;
    }

您仍然需要填写派生类成员并执行作业,但这会减少开销。

来自p.Brian.Mackey回答,如果
ctorp
被替换为关键字,请说
base.ctorp:

public class Complex : Cartesian
{
    public int I { get; set; }

    //public Complex(int x, int y, int i) : base(x, y)
    public Complex(int i) : base.ctorp
    {
        I = i;
    }
}
然后让编译器根据签名确定调用哪个构造函数,并相应地替换为CTROP(并且可能有一些解释性符号来补充):

产生

public Complex(int x, int y) : base(x, y)
{
     I = x;
}
    //public Complex(int i) : base.ctorp
    public Complex(int x, int y, int i) : base(x, y)
    {
        I = i;
    }
    //public Complex(int i) : base.ctorp
    public Complex(int i) : base()
    {
        I = i;
    }

产生

public Complex(int x, int y) : base(x, y)
{
     I = x;
}
    //public Complex(int i) : base.ctorp
    public Complex(int x, int y, int i) : base(x, y)
    {
        I = i;
    }
    //public Complex(int i) : base.ctorp
    public Complex(int i) : base()
    {
        I = i;
    }

编译器不喜欢含糊不清。您会提出什么替代方案?使用params int[]构造函数?我可以向大家展示,如果这看起来是一个值得探索的选择,那么简单的回答可能是“不”。我想您可以传递结构而不是单个参数,至少可以将它们捆绑起来。这样你就可以控制损失了。但是这个例子就是你要传递的结构。那么,也许复杂构造函数应该采用笛卡尔函数,而不是它的单个参数?但是你在复杂的声明中牺牲了什么,你把复杂的消费者放在上面,所以他们不得不说新的复杂(新笛卡尔(x,y),i)。所以这也不太好。我还要说,我不认为你的例子是减少打字的有力理由。如果有10多个论点的话。你只需要做一次。不要低估通过工作解决问题的能力。具有良好宏或剪切粘贴/多插入点功能的编辑器也能很好地减少键入。13参数是一个明确的标志,表明您应该将其中一些参数封装到一个新类中。这些参数之间的关系是什么?除了直接的语言支持之外,我无法想象还有什么比这更好的了。我真的需要安装2017。