标签: multiple-constructors

javascript:相同类型对象的不同构造函数

是否可以在javascript中为一个类创建多个构造函数?即一个零参数,一个一个,一个两个,等等...

如果是这样,怎么样?

谢谢!

javascript multiple-constructors

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

C++构造函数问题

绝对初学者,第2版书的C++编程中,有以下声明:

HeapPoint::HeapPoint(int x, int y): thePoint(new Point(x,y)) { }
Run Code Online (Sandbox Code Playgroud)

这等于:

HeapPoint::HeapPoint(int x, int y) { thePoint = new Point(x,y); }
Run Code Online (Sandbox Code Playgroud)

而且,既然我们在构造函数中执行此操作,那么为x和分配的值是y什么?我们应该写值insted的中xynew Point(x,y)?或者,这是正确的吗?

更新:我认为我有了初始化的想法,x并且y正如在书中它在函数中有以下内容:

HeapPoint myHeapPoint(2,4);
Run Code Online (Sandbox Code Playgroud)

c++ pointers multiple-constructors

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

Scala:具有多个构造函数的泛型类

我正在尝试创建这样的泛型类:

class A[T](v: Option[T]) {
  def this(v: T) = this(Some(v))
  def this() = this(None)
  def getV = v 
}
Run Code Online (Sandbox Code Playgroud)

然后我做了一些测试:

scala> new A getV
res21: Option[Nothing] = None
scala> new A(8) getV
res22: Option[Int] = Some(8)
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.但是当我尝试调用主构造函数时,我得到了这个:

scala> new A(Some(8)) getV
<console>:9: error: ambiguous reference to overloaded definition,
both constructor A in class A of type (v: T)A[T]
and  constructor A in class A of type (v: Option[T])A[T]
match argument types (Some[Int])
       new A(Some(8)) getV
       ^

scala> new A(None) getV …
Run Code Online (Sandbox Code Playgroud)

generics scala multiple-constructors scala-2.9

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

Spring DI 同时有两个构造函数

这是一种反模式,但我很好奇实际会发生什么。

如果显式定义一个无参数构造函数和一个带自动装配参数的构造函数,spring框架将如何初始化它?

@Service
class Clazz {

    private MyBean myBean;

    public Clazz(){}

    @Autowired
    public Clazz(MyBean myBean){
        this.myBean = myBean;
    }
}
Run Code Online (Sandbox Code Playgroud)

java spring dependency-injection javabeans multiple-constructors

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

从PHP中的构造函数调用另一个构造函数

我想在PHP类中定义一些构造函数.但是,我的构造函数代码目前非常相似.如果可能的话,我宁愿不重复代码.有没有办法从php类中的一个构造函数中调用其他构造函数?有没有办法在PHP类中有多个构造函数?

function __construct($service, $action)
{
    if(empty($service) || empty($action))
    {
        throw new Exception("Both service and action must have a value");
    }
    $this->$mService = $service;
    $this->$mAction = $action;

    $this->$mHasSecurity = false;
}
function __construct($service, $action, $security)
    {
        __construct($service, $action); // This is what I want to be able to do, so I don't have to repeat code

        if(!empty($security))
        {
            $this->$mHasSecurity = true;
            $this->$mSecurity = $security;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我知道我可以通过创建一些Init方法来解决这个问题.但有没有办法绕过这个?

php constructor multiple-constructors

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

如何在基类中声明构造函数,以便子类可以在不声明它们的情况下使用它们?

我希望子类使用其父类的构造函数.但似乎我总是需要在子类中再次定义它们才能使它工作,如下所示:

public SubClass(int x, int y) : base (x, y) {
    //no code here
}
Run Code Online (Sandbox Code Playgroud)

所以我想知道我是不是在父类中正确地声明了构造函数,或者根本没有直接的构造函数继承?

c# inheritance multiple-constructors inherited-constructors

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

如何简化多个构造函数?

我想为一个类有两个构造函数,如下所示:

public MyClass()
{
    // do stuff here
}

public MyClass(int num)
{
    MyClass();
    // do other stuff here
}
Run Code Online (Sandbox Code Playgroud)

以上是达到我目的的正确方法吗?有什么样的速记更好吗?

c# constructor visual-studio-2005 multiple-constructors

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

C#alternate无参数构造函数

是否可以为C#类定义备用无参数构造函数?

换句话说,我有一堂课Foo.我想要一个默认的构造函数Foo()和另一个构造函数SpecialFoo().我不介意SpecialFoo()构造函数是否调用Foo()构造函数.

我能这样做吗?

c# constructor multiple-constructors

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

在类中使用关键字this和多个构造函数

我不明白使用构造函数Rational()创建Rational对象时会发生什么.我的书说它将创建一个Rational对象,其值为0但内部存储为0/1.这个(0)如何存储为0/1?不是num和den 0的实例变量的默认值吗?

public class Rational{

  public Rational(){
      this(0);
  }

  public Rational(int n){
      this(n,1);
  }

  public Rational(int x, int y){
     num = x; 
     den = y; 
  }

  private int num;
  private int den; 
}
Run Code Online (Sandbox Code Playgroud)

java instance-variables this multiple-constructors

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

以这种方式使用多个构造函数是否正确?

我不太确定这是如何工作的,但是如果我想给一个类的对象提供更多或更少的变量的选项,这是否适用于这样的多个构造函数?

假设我想创建一个多项选择问卷,但是我不知道我的用户想要输入多少个答案,可能是 2、3、4、5、6?所以为此:

public class Quiz {
    private int counter;
    private String question;
    private String answer1;
    private String answer2;
    private String answer3;
    private String answer4;
    private String answer5;
    private String answer6;
    private String rightAnswer;

    public Quiz(int counter,String question, String answer1, String answer2, String rightAnswer){
        super();
        this.counter = counter;
        this.question = question;
        this.answer1 = answer1;
        this.answer2 = answer2;
        this.rightAnswer = rightAnswer;
    }
    public Quiz(int counter, String question, String answer1, String answer2, String answer3, String rightAnswer) {
        super();
        this.counter = counter;
        this.question …
Run Code Online (Sandbox Code Playgroud)

java multiple-constructors

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

两个具有相同编号的构造函数.参数但不同的数据类型

在这里,当我运行下面的代码时,我得到called输出,我想知道为什么不called new.因为1来自两个shortint范围.

public class MyClass {

        private int x;

        public MyClass(){
            this(1);
        }

        public MyClass(int x){
            System.out.println("called");
            this.x = x;
        }

       public MyClass(short y){
            System.out.println("called new");
            this.x = y;
        }

        public static void main(String args[]) {
        MyClass m = new MyClass();
            System.out.println("hello");
        }
    }
Run Code Online (Sandbox Code Playgroud)

java multiple-constructors

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

C#字符串插入与可选参数混淆

我是C#的新手,并试图找出字符串插入(即"some {0} string", toInsert),并遇到了一个我没想到的问题......

在您有两个构造函数的情况下:

public MyClass(String arg1) { ... }

public MyClass(String arg1, String arg2) { ... }
Run Code Online (Sandbox Code Playgroud)

我可以使用带有字符串插入的第一个构造函数吗?

...
toInsert = "def"
myClass = new MyClass("abc{0}ghi", toInsert)
...
Run Code Online (Sandbox Code Playgroud)

或者C#将此解释为第二个构造函数并将文字"abc{0}ghi"作为第一个参数传递?

c# string-formatting multiple-constructors

0
推荐指数
2
解决办法
989
查看次数

Kotlin主要构造函数调用次要构造函数

为什么不编译?

class test
{
  constructor() {
      var a = Date().day
      this(a)
  }

  constructor(a:Int) {
  }
}
Run Code Online (Sandbox Code Playgroud)

错误是:无法将类型为“ test”的表达式“ this”作为函数调用。找不到函数“ invoke()”。

建议的解决方案是添加以下内容:

private operator fun invoke(i: Int) {}
Run Code Online (Sandbox Code Playgroud)

为什么?

class-constructors multiple-constructors kotlin

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