简体   繁体   English

这是什么() ? 它可以有多个参数吗?

[英]what is this() ? can it have multiple parameters?

I encountered a code where this() method in java takes three parameters two being integers and the third one is boolean value. 我遇到了一个代码,其中java中的this()方法采用三个参数,两个是整数,第三个是布尔值。 what exactly does that mean ? 这到底是什么意思呢 ? Are there any other variants of this() method ? this()方法还有其他变体吗? Hera is the actual code. 赫拉是实际的代码。

 public SegmentConstructor(int seqNum_, int length_) {
        this(seqNum_, length_, false);
    }

Thank You.. 谢谢..

It means that there is another constructor in the current class that has that signature. 这意味着当前类中还有另一个具有该签名的构造函数。

public SegmentConstructor(int seqNum_, int length_) {
    this(seqNum_, length_, false); // calls the constructor below.
}

public SegmentConstructor(int seqNum_, int length_, boolean required_) {
    seqNum = seqNum_;
    length = length_;
    required = required_;
}

The this method is just a way to call one of your class's constructors from within another constructor, to help avoid code duplication. this方法只是从另一个构造函数中调用类的一个构造函数的一种方法,以帮助避免代码重复。 It can only be called on the first line of a constructor--never from within any other method. 只能在构造函数的第一行上调用它-决不能在任何其他方法中调用它。

this simply invokes another constructor to run. this只是调用另一个构造函数来运行。 So, look for other constructors with that signature. 因此,寻找具有该签名的其他构造函数。

As said before this invokes another constructor, mostly as a convenience method. 如前所述,这将调用另一个构造函数,主要是作为一种便捷方法。

Trivial example: 琐碎的例子:

class A {
 private int value;

 public A(int val) {
  value = val;
 }

 public A() {
  this(0); //0 as default
 }
}

Normally you do use calls to this() when the most specific constructor (that one with the most parameters) is not just assignment but contains more logic that you don't want to repeat/copy etc. 通常,当最具体的构造函数(具有最多参数的那个构造函数)不只是赋值而是包含更多您不想重复/复制的逻辑时,您确实会使用对this()的调用。

Just because it fits in here: super() can have parameters, too, ie this calls a super class' constructor with parameters from the sub class' constructor. 仅仅因为它适合这里: super()也可以有参数,即,它使用子类的构造函数中的参数调用超类的构造函数。

It is a constructor call. 这是一个构造函数调用。 If your class implements different constructors with a differing number of arguments, you can chain your constructors like this: 如果您的类使用不同数量的参数实现不同的构造函数,则可以像这样链接您的构造函数:

class A {
    public A(boolean arg) {
        ...
    }

    public A() {
        this(false); // invokes the constructor with the boolean argument
    }
}

Sometimes it makes sense to create a private constructor taking different arguments and provide public and/or protected constructors with other/fewer arguments and delegate object construction to that private constructor. 有时,创建带有不同参数的私有构造函数并为公共和/或受保护的构造函数提供其他/更少的参数并将对象构造委托给该私有构造函数是有意义的。

It is important to know that no other code may be placed before the call to this(...). 重要的是要知道在调用this(...)之前不能放置其他代码。 However, after calling this(...), you can do everything you could in any other constructor. 但是,在调用this(...)之后,您可以执行任何其他构造函数中可以做的所有事情。

Edit: Since this(...) calls a constructor, it can only be called from within other constructors (belonging to the same class). 编辑:由于this(...)调用构造函数,因此只能从其他构造函数(属于同一类)中调用它。

class MyClass { private int var1; class MyClass {private int var1; private int var2; private int var2; private boolean flag; 私有布尔标志;

public MyClass(int var1_,int var2_) { this(var1_,var2_,false); 公共MyClass(int var1_,int var2_){this(var1_,var2_,false); } }

public MyClass(int var1_,int var2_,boolean flag_) { var1 = var1_; 公共MyClass(int var1_,int var2_,boolean flag_){var1 = var1_; var2 = var2_; var2 = var2_; flag = flag_; 标志= flag_; } }

public String toString() { return (new Boolean(flag).toString()); public String toString(){return(new Boolean(flag).toString()); } }

public static void main(String[] args) { MyClass my = new MyClass(5,6); 公共静态无效main(String [] args){MyClass my = new MyClass(5,6); System.out.println(my); System.out.println(my); } }

} }

So it works. 这样就行了。

this() is not a method but is a reserved keyword pointing to a overloaded constructor of the same class. this()不是方法,而是一个保留关键字,指向同一类的重载构造函数。 The number of parameters you pass should point to an existing corresponding constructor defined in the class. 您传递的参数数量应指向该类中定义的现有对应构造函数。

The super() also has the semantics however the constructor is defined in one of its parent hierarchy. super()也具有语义,但是构造函数是在其父层次结构之一中定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM