标签: constructor-chaining

是否有可能在PHP中链式重载构造函数?

这是一个组成的例子,当有很多参数时它会变得更有用.

这会让来电者使用new Person("Jim", 1950, 10, 2)new Person("Jim", datetimeobj).我知道可选参数,这不是我在这里寻找的.

在C#我可以这样做:

public Person(string name, int birthyear, int birthmonth, int birthday)
    :this(name, new DateTime(birthyear, birthmonth, birthday)){ }

public Person(string name, DateTime birthdate)
{
    this.name = name;
    this.birthdate = birthdate;
}
Run Code Online (Sandbox Code Playgroud)

我可以在PHP中做类似的事情吗?就像是:

function __construct($name, $birthyear, $birthmonth, $birthday)
{
    $date = new DateTime("{$birthyear}\\{$birthmonth}\\{$birthyear}");
    __construct($name, $date);
}

function __construct($name, $birthdate)
{
    $this->name = $name;
    $this->birthdate = $birthdate;
}
Run Code Online (Sandbox Code Playgroud)

如果这是不可能的,那么什么是好的选择呢?

php constructor constructor-chaining

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

构造函数链接不使用类成员的默认值?

我有两个班级单位和阿切尔.Archer继承自单位.我尝试使用构造函数链接来设置基类的统计信息,但如果我使用以下代码,则统计信息似乎设置为零:

#include<iostream>

using namespace std;

class Unit{
    int damage = 0;
    int curHp = 0;
    int maxHp = 1;
    int range = 0;
    int cost = 0;

public:
    Unit(int _damage,int _maxHp,int _range,
         int _cost){
        damage = _damage;
        curHp = maxHp = _maxHp;
        range = _range;
        cost = _cost;
    }

    int getCost(){
        return cost;
    }
};

class Archer: public Unit{
    int damage = 25;
    int range = 50;
    int maxHp = 100;
    int cost = 150;
    int stepSize = 25;
    int …
Run Code Online (Sandbox Code Playgroud)

c++ oop inheritance constructor constructor-chaining

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

具有空主体和类似继承的语法的构造函数有什么作用?

public class PhotoList : ObservableCollection<ImageFile>
{


    public PhotoList() { }

    **//this is the line that I  dont recognise!!!!!!!!!!**
    public PhotoList(string path) : this(new DirectoryInfo(path)) { }

    public PhotoList(DirectoryInfo directory)
    {
        _directory = directory;
        Update();
    }

    public string Path
    {
        set
        {
            _directory = new DirectoryInfo(value);
            Update();
        }
        get { return _directory.FullName; }
    }

    public DirectoryInfo Directory
    {
        set
        {
            _directory = value;
            Update();
        }
        get { return _directory; }
    }
    private void Update()
    {
        foreach (FileInfo f in _directory.GetFiles("*.jpg"))
        { …
Run Code Online (Sandbox Code Playgroud)

c# constructor constructor-chaining

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

什么是C#代码:

在以下代码中:

public class A
{
public A():this(null){}
public A(string b){/*code here*/}
}
Run Code Online (Sandbox Code Playgroud)

第一个构造函数有什么用?

c# constructor-chaining

3
推荐指数
2
解决办法
409
查看次数

从参数化构造函数调用默认构造函数

我希望我的默认构造函数能够创建和初始化我的代码片段中显示的所有对象.然后我希望我的参数化构造函数调用默认构造函数,从而创建并初始化这些对象,然后可以在参数化构造函数中使用它们而不会获得NullReferenceException.

在这种情况下,我不确定使用构造函数的最佳方法(最有效,代码较少等)是什么.我更喜欢使用构造函数链接.

再一次,我对构造函数有一个非常基本的理解,所以如果这是不可能的,那么请告诉我,告诉我你在这种情况下会做些什么.

class Rectangle
{
    public Line left { get; set; }
    public Line top { get; set; }
    public Line right { get; set; }
    public Line bottom { get; set; }

    public Rectangle() : this(new Line()) { }

    public Rectangle(Line diagnonal) 
    {
        left = new Line();
        top = new Line();
        right = new Line();
        bottom = new Line();

        Point beginningDiagonalPoint = new Point();
        Point endingDiagonalPoint = new Point();

        beginningDiagonalPoint = diagnonal.startPoint;
        endingDiagonalPoint = diagnonal.endPoint;

        int begXC = …
Run Code Online (Sandbox Code Playgroud)

c# constructor constructor-chaining

3
推荐指数
2
解决办法
9846
查看次数

子类中的构造函数顺序

在后代类中,有没有办法调用public,参数化构造函数以及protected/private构造函数,同时仍然调用基类的构造函数?

例如,给出以下代码:

using System;

public class Test
{
  void Main()
  {
    var b = new B(1);
  }

  class A
  {
    protected A()
    {
      Console.WriteLine("A()");
    }


    public A(int val)
      : this()
    {
      Console.WriteLine("A({0})", val);
    }

  }

  class B : A
  {
    protected B()
    {
      Console.WriteLine("B()");
    }

    public B(int val)
      : base(val)
    {
      Console.WriteLine("B({0})", val);
    }

  }
}
Run Code Online (Sandbox Code Playgroud)

给出的输出是:

A()
A(1)
B(1)
Run Code Online (Sandbox Code Playgroud)

但是,这正是我所期待的:

A()
B()
A(1)
B(1)
Run Code Online (Sandbox Code Playgroud)

有没有办法通过构造函数链来实现这一目标?或者我应该有一个OnInitialize()类型方法,A它是抽象的还是虚拟的,它被覆盖B并从受保护的无参数构造函数中调用A

c# constructor-chaining

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

Java构造函数链接

嗨,我刚学习Java中的构造函数链接,并有一些问题......

  1. 首先,有人可以解释我何时需要使用它?在我的头脑中,我真的想不到一个情况.

  2. 在这个例子中,在没有参数的构造函数中,我调用另一个构造函数.如何访问这个新的"James Bond"对象以备将来使用?

    import java.util.*;
    
    class Employee
    {   
        private String name;
        private double salary;
    
        public Employee()
        {
            this("James Bond", 34000);
        }
    
        public Employee(String n, double s)
        {
            name = n;
            salary = s;
        }
    
        public String getName()
        {
            return name;
        }
    
        public double getSalary()
        {
            return salary;
        }
    
        public static void main(String[] args)
        {
            Employee a = new Employee();
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

java constructor-chaining

3
推荐指数
1
解决办法
2万
查看次数

使用构造函数链接实现此构造的正确方法?

我有第一个OOP课程的作业,我理解所有这些,包括以下声明:

您应该创建一个名为ComplexNumber的类.此类将包含定义为双精度的私有数据成员中复数的实部和虚部.您的类应包含一个构造函数,该构造函数允许将虚数的数据成员指定为构造函数的参数.默认(非参数化)构造函数应将数据成员初始化为0.0.

当然我知道如何在不将它们链接在一起的情况下创建这些构造函数,并且赋值不需要链接它们,但我想按照我的意愿.

没有将它们链接在一起,我的构造函数看起来像这样:

class ComplexNumber
{
    private double realPart;
    private double complexPart;

    public ComplexNumber()
    {
         realPart = 0.0;
         complexPart = 0.0
    }

    public ComplexNumber(double r, double c)
    {
         realPart = r;
         complexPart = c;
    }
    // the rest of my class code...
}
Run Code Online (Sandbox Code Playgroud)

c# constructor complex-numbers constructor-chaining

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

构造函数链是否会在Java中创建多个对象?

如果我使用此关键字调用同一类的另一个构造函数.它会创建两个对象吗?

如果是这样,下面的例子中哪个对象将处于活动状态:

Class Sample {
    int a;
    String b;

    Sample() {
        this("Hello");
        a=10;
    }

    Sample(String temp) {
        b = temp;
    }    
}
Run Code Online (Sandbox Code Playgroud)

我想知道构造函数链接的行为.请解释它是如何工作的?

java this constructor-chaining

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

构造函数在调用之前链接并准备参数(aguments)

我正在做一个Yahtzee游戏.我想为不同的情况提供构造函数.假设您无法提供想要创建新游戏的玩家的名字,我想创建"未命名玩家1","未命名玩家2"等.

这是我试图这样做的方式:

public class YahtzeeGame {
    private List<Player> players = new ArrayList<>();

    public YahtzeeGame(String[] playerNames) {
        for (String playerName : playerNames) {
            players.add(new Player(playerName));
        }
    }

    public YahtzeeGame(int numberOfPlayers) {
        String[] playerNames = new String[numberOfPlayers];
        for (int i = 0; i < numberOfPlayers; i++) {
            playerNames[i] = "Unnamed player " + (i+1);
        }
        this(playerNames); // ERROR: "Constructor call must be the first statement in a constructor.
    }

    public YahtzeeGame(String playerName) {
        this(new String[] {playerName});
    }

    public YahtzeeGame() {
        this("Unnamed player"); …
Run Code Online (Sandbox Code Playgroud)

java constructor constructor-chaining

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