标签: constructor-overloading

Scala中的case类重载

我的系统中有遗留消息,我希望能够将新消息映射到我的系统中.

为什么我不能重载我的case类?

case class Message(a:Int, b:Int)
case class NewMessage(a:Int, b:Int, c:Int) {
  def this(msg : Message) = this(a = msg.a, b = msg.b, c = 0)
}
val msg = Message(1,2)
val converted = NewMessage(msg)
Run Code Online (Sandbox Code Playgroud)

这段代码似乎没有编译.:(

scala constructor-overloading

4
推荐指数
2
解决办法
2251
查看次数

Scala重载了构造函数和超级

我无法理解如何在Java上开发类似于以下内容的Scala代码:

public abstract class A {
   protected A() { ... }
   protected A(int a) { ... }
}

public abstract class B {
   protected B() { super(); }
   protected B(int a) { super(a); }
}

public class C extends B {
   public C() { super(3); }
}
Run Code Online (Sandbox Code Playgroud)

虽然很清楚如何开发C类,但我无法获得如何接受B.帮助.

PS我正在尝试创建自己的基于wicket WebPage的BaseWebPage,这是Java的常见做法

constructor scala super constructor-overloading

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

构造函数在java中重载

我收到以下代码片段的错误

错误是:在调用超类型构造函数之前无法引用x(并指出注释1中的语句)

class Con{
    int x =10;

    Con(){
        this(++x); //1
        System.out.println("x :"+x);
    }

    Con(int i){
        x=i++;
        System.out.println("x :"+x);
    }
}
Run Code Online (Sandbox Code Playgroud)

在主要方法中,我有这个说法

    Con c1=new Con();
Run Code Online (Sandbox Code Playgroud)

我不明白这个错误.有人能解释一下这里到底发生了什么吗?

java constructor constructor-overloading

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

为什么(删除)复制构造函数优先于隐式转换?

请考虑以下代码:

struct Bar{};

struct Foo
{
    Foo() = default;
    Foo(const Bar&) {}
    Foo(const Foo&) = delete;

    // IMPLICIT conversion to Bar
    operator Bar(){return {};}
};

int main() 
{
    Foo f1;
    Foo f2(static_cast<Bar>(f1)); // this is OK
    Foo f3(f1); // does not compile, why not implicit conversion to `Bar`?
}
Run Code Online (Sandbox Code Playgroud)

该类Bar有一个用户定义的转换运算符Foo,它接受Bar&s.但是,在最后一行中main,我原本希望Foo f1转换为a Bar然后传递给Foo(const Bar&).但是,只Foo(const Foo&) = delete;考虑删除的构造函数.我知道这个构造函数是一个更好的匹配,但为什么不在Foo(const Bar&)重载集中也是为什么编译器不执行隐式转换?

c++ constructor-overloading implicit-conversion

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

多个默认构造函数

这个堆栈溢出问题中,答案包含以下引用:

...定义表示所有默认构造函数(如果有多个)...

怎么会有多个默认构造函数,为什么这可能有用或被标准允许?

c++ overloading constructor-overloading default-constructor language-lawyer

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

如何从重载的可变参数构造函数调用对象的基本构造函数?

我有一个简单的类,其中包含所有可用字段的构造函数,我想添加一个带有可变参数的重载构造函数,以允许创建对象,即使某些字段未知。

class FooClass {
    int id;
    String first;
    String second;
    String third;

    FooClass(final int id, final String first, final String second, final String third) {
        this.id = id;
        this.first = first;
        this.second = second;
        this.third = third;
    }

    FooClass(final int id, final String... myStrings){
        if(myStrings.length == 1) {
            this.id     = id;
            this.first  = myStrings[0];
        } else if (myStrings.length == 2) {
            this.id     = id;
            this.first  = myStrings[0];
            this.second = myStrings[1];
        } else {
            this.id     = id;
            this.first  = myStrings[0];
            this.second …
Run Code Online (Sandbox Code Playgroud)

java constructor-overloading

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

Java构造函数使用多种方法重载

我在课堂上有一个程序任务.我已经理解了重载的基础知识,但我对一点非常困惑.如何仅从我尝试使用的方法输出?那么让我告诉你代码而不是解释.

public class Box {
 private int length, width, height;

 public Box(int length){
  this.length=length;
  System.out.println("Line created with length of" + length + ".");
 }
 public Box(int length, int width){
  this.length = length;
  this.width = width;
  System.out.println("Rectangle created with the length of " + length + " ");
  System.out.println("and the width of " + width + ".");
 }
 public Box(int length, int width, int height){
  this.length=length;
  this.width=width;
  this.height=height;
  System.out.println("Box created with the length of " + length + ", ");
  System.out.println("the …
Run Code Online (Sandbox Code Playgroud)

java constructor-overloading

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

如何在Python中实现多个构造函数?

在 python 中,不可能多次定义init函数,在了解该语言如何工作的情况下,这是相当公平的。当创建一个对象时,会调用init,因此,拥有两个对象会产生不确定性。然而,在某些设计中,这种特性是需要的。例如:

class Triangle(object):
  def __init__(self,vertices):
    self.v1 = vertices[0]
    self.v2 = vertices[1]
    self.v3 = vertices[2]

 def area(self):
   # calculate the are using vertices
   ...
   return result

class Triangle(object):
  def __init__(self,sides):
    self.v1 = sides[0]
    self.v2 = sides[1]
    self.v3 = sides[2]

 def area(self):
   # calculate the are using sides
   ...
   return result
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我们有相同数量的属性要初始化,而且它们是相关的,这样您就可以从一个属性获得另一个属性。确实,在这个特定的示例中,我们可以处理以下事实:顶点是元组,而边可能是浮点数(或字符串或其他东西)但当然情况并非总是如此。

一种可能的解决方案是将初始化过程委托给其他函数,例如:

class Triangle(object):
  def __init__(self):
    self.v1 = None
    self.v2 = None
    self.v3 = None

  def byVertices(self,vertices):
    self.v1 = vertices[0]
    self.v2 = vertices[1]
    self.v3 = …
Run Code Online (Sandbox Code Playgroud)

python constructor constructor-overloading

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

Python中的重载构造函数点?

我无法准确理解这段代码的用途

class buildArgs:
    def __init__(self, host, port, user, dept):
        self._dept = dept
        self._host = host
        self._port = port
        self._user = user
        self._datacenter = datacenter
        self._path = path
    def __init__(self): 
        self._dept = None
        self._host = None
        self._port = None
        self._user = None
        self._datacenter = None
        self._path = None
Run Code Online (Sandbox Code Playgroud)

然后是一堆@property方法,然后是像这样的setter方法,然后是main方法:

@property
def port(self):
    return self._port
@port.setter
def port(self, port):
    self._port = port
Run Code Online (Sandbox Code Playgroud)

我以前从未__init__在 Python 中的一个类下见过两个函数,我认为这是非 Python 的。这种逻辑的正确方法是什么?

python oop class constructor-overloading

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

除了构造函数重载java之外的最佳实践/设计模式

我有一个具有多个构造函数的类。每个代表不同的用例。

public class ABC {
  public ABC(int x) {
  ...
  }
  public ABC(ArrayList<String> Stringarray) {
  ...
  }
  ..many more constructors..
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,构造函数重载是干净的解决方案,直到我遇到了来自 java 编译器的相同擦除问题。例如,我想添加另一个最终具有相同擦除的构造函数,所以我只是选择包含一个默认参数来解决现在的问题,如下所示:

public ABC(ArrayList<String> stringArray) {
  …
}
public ABC(ArrayList<Integer> integerArray, boolean… sameErasureFlag) {
  …
}
Run Code Online (Sandbox Code Playgroud)

但是我有一种强烈的感觉,对于这个用例来说,有这么多构造函数可能不是一个好的设计模式。也许有一个更好的解决方案或最佳实践设计模式用于这种场景。我正在查找构建器模式,但不确定这是否正确/更好。有什么建议吗?

java design-patterns architectural-patterns constructor-overloading builder-pattern

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