标签: constructor-chaining

如何链接这些构造函数(C#)?

我只是将构造函数链接起来的概念,但我无法弄清楚如何将这两个特定的构造函数链接在一起,所以如果有人可以帮助我,我将不胜感激.

谢谢!

构造函数

// default constructor
// purpose: initialize data members to zero
// Parameters: none
// returns: none
public Line()
{
    startPoint.xCoord = 0;
    startPoint.yCoord = 0;
    endPoint.xCoord = 0;
    endPoint.yCoord = 0;
}


// parameterized constructor
// purpose: initialize data members to p1 and p2
// Parameters: Point objects p1 and p2
// returns: none
public Line(Point p1, Point p2)
{
    startPoint = p1;
    endPoint = p2;
}
Run Code Online (Sandbox Code Playgroud)

c# constructor constructor-chaining

1
推荐指数
1
解决办法
308
查看次数

Delphi:重载的虚拟构造函数后代未被重载调用

关于Delphi中构造函数的一系列问题还有另一个问题.

我有一个具有虚拟构造函数的基类:

TComputer = class(TObject)
public
    constructor Create(Teapot: Integer); virtual;
end;
Run Code Online (Sandbox Code Playgroud)

构造函数是虚拟的,适合有人需要调用的时间

var
   computerClass: class of TComputer;
   computer: TComputer;
begin     
   computer := computerClass.Create(nTeapot);
Run Code Online (Sandbox Code Playgroud)

构造函数overridden位于后代:

TCellPhone = class(TComputer) 
public
   constructor Create(Teapot: Integer); override;
end;

TiPhone = class(TCellPhone ) 
public
   constructor Create(Teapot: Integer); override;
end;
Run Code Online (Sandbox Code Playgroud)

哪里TCellPhoneTiPhone后代都有机会进行自己的初始化(为了便于阅读而不包括成员).

但是现在我向一些祖先添加了一个重载的构造函数:

TCellPhone = class(TComputer) 
public
   constructor Create(Teapot: Integer); override; overload;
   constructor Create(Teapot: Integer; Handle: string); overload;
end;
Run Code Online (Sandbox Code Playgroud)

TCellPhone中的备用构造函数调用另一个虚拟构造函数,因此它始终获得正确的重写行为:

constructor TCellPhone.Create(Teapot: Integer; Handle: string);
begin
   TCellPhone.Create(Teapot); //call sibling virtual …
Run Code Online (Sandbox Code Playgroud)

delphi constructor delphi-5 constructor-chaining

1
推荐指数
1
解决办法
2054
查看次数

这些是什么类型的java构造函数?构造函数链接?

这些是来自github上的spring amqp示例, 网址https://github.com/SpringSource/spring-amqp-samples.git这些类型的java构造函数是什么类型的?他们是吸气者和制定者的简称吗?

public class Quote {

    public Quote() {
        this(null, null);
    }

    public Quote(Stock stock, String price) {
        this(stock, price, new Date().getTime());
    }
Run Code Online (Sandbox Code Playgroud)

反对这个

public class Bicycle {

public Bicycle(int startCadence, int startSpeed, int startGear) {
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;
}
Run Code Online (Sandbox Code Playgroud)

java spring constructor constructor-chaining

1
推荐指数
1
解决办法
424
查看次数

用Java链接构造函数

无法提出更好的标题。

一个经典的学习示例:class Human,其中属性是姓名,年龄,母亲和父亲。父母双方Human也是。

public class Human {
    String name;
    int age;
    Human mother;
}
Run Code Online (Sandbox Code Playgroud)

我想创建3个构造函数:

  1. Human();
  2. Human(String name, int age);
  3. Human(String name, int age, Human mother)

我确实了解链接的工作原理,这就是我所做的:

Human() {
    this("Jack", 22);
}

Human(int age, String name) {
    this(age, name, new Human()); // new Human() will cause SOF Error.
}

Human(int age, String name, Human mother) {
    this.age = age;
    this.name = name;
    this.mother = mother;
}
Run Code Online (Sandbox Code Playgroud)

如上所述,我StackOverflowError再次收到,我我知道为什么会这样。公平地说,我想我会得到像人类杰克这样的东西,而他的母亲也是人类杰克 …

java constructor-overloading constructor-chaining

1
推荐指数
1
解决办法
64
查看次数

重载的构造函数链

原始问题

请考虑以下情形:


public abstract class Foo
{
    public string Name { get; set; }

    public Foo()
    {
        this.Name = string.Empty;
    }
    public Foo(string name)
    {
        this.Name = name;
    }
}
public class Bar : Foo
{
    public int Amount { get; set; }

    public Bar()
        : base()
    {
        this.Amount = 0;
    }
    public Bar(string name)
        : base(name)
    {
        this.Amount = 0;
    }
    public Bar(string name, int amount) 
        : base(name)
    {
        this.Amount = amount;
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有更优雅的方式链接结构,以便它们之间没有重复的代码?在此示例中,我最终必须复制代码以将Bar.Amount属性的值设置为第二个构造函数中amount参数的值.随着类中变量的数量增加,构造的排列可能变得非常复杂.它只是闻起来很有趣. …

c# constructor-chaining

0
推荐指数
1
解决办法
1674
查看次数

变量可能已经被分配

以下代码有错误:

class A
{

  private final String val;

  public A(){
    this.val = null;
  }

  public A(String val){
    this();
    this.val = val;
  }
}
Run Code Online (Sandbox Code Playgroud)

错误是“可能已经分配了变量 val

是否有解决此错误的方法,而无需重新编写可能在默认构造函数中的任何代码?这是一个最小的工作示例;如果你问自己“默认构造函数中有哪些代码”,请记住,一个真实的例子可能有很多你不想在其他构造函数中重复的代码(分配其他最终变量等)。

还请记住,这是一个最小的例子,同样的问题存在于大量的构造函数中。

java constructor variable-initialization constructor-chaining

0
推荐指数
1
解决办法
3553
查看次数

C# 中的构造函数链

我正在尝试链接这些构造函数:

public class Method1
{
    public Method1(string a, string b) : this(a, b, "", "")
    {

    }

    public Method1(string c, string d) : this("", "", c, d)
    {

    }

    public Method1(string a, string b, string c, string d)
    {

    }
} 
Run Code Online (Sandbox Code Playgroud)

我有什么办法可以实现这个目标吗?
目前它显示编译时错误。

c# constructor constructor-chaining c#-4.0

0
推荐指数
1
解决办法
1480
查看次数