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

发布于 2024-10-20 07:42:36 字数 236 浏览 2 评论 0原文

我遇到了一段代码,其中java中的this()方法采用三个参数,其中两个是整数,第三个是布尔值。 这到底是什么意思? this() 方法还有其他变体吗? Hera 是实际的代码。

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

谢谢..

I encountered a code where this() method in java takes three parameters two being integers and the third one is boolean value.
what exactly does that mean ? Are there any other variants of this() method ?
Hera is the actual code.

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

Thank You..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

书间行客 2024-10-27 07:42:36

这意味着当前类中还有另一个具有该签名的构造函数。

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_;
}

this 方法只是从另一个构造函数中调用一个类的构造函数的一种方法,以帮助避免代码重复。它只能在构造函数的第一行调用,而不能在任何其他方法中调用。

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. It can only be called on the first line of a constructor--never from within any other method.

咋地 2024-10-27 07:42:36

this 只是调用另一个构造函数来运行。因此,寻找具有该签名的其他构造函数。

this simply invokes another constructor to run. So, look for other constructors with that signature.

明月松间行 2024-10-27 07:42:36

如前所述,这会调用另一个构造函数,主要作为一种便捷方法。

简单的例子:

class A {
 private int value;

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

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

通常,当最具体的构造函数(具有最多参数的构造函数)不仅仅是赋值而是包含更多您不想重复/复制等的逻辑时,您确实会使用对 this() 的调用。

只是因为它适合这里: super() 也可以有参数,即它使用子类构造函数的参数调用超类的构造函数。

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.

Just because it fits in here: super() can have parameters, too, i.e. this calls a super class' constructor with parameters from the sub class' constructor.

柏拉图鍀咏恒 2024-10-27 07:42:36

这是一个构造函数调用。如果您的类使用不同数量的参数实现不同的构造函数,您可以像这样链接构造函数:

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

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

有时,创建一个采用不同参数的私有构造函数并为公共和/或受保护的构造函数提供其他/更少的参数和委托对象构造是有意义的到那个私有构造函数。

重要的是要知道在调用 this(...) 之前不能放置其他代码。但是,调用 this(...) 后,您可以在任何其他构造函数中执行所有操作。

编辑:由于 this(...) 调用构造函数,因此只能从其他构造函数(属于同一类)中调用它。

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(...). However, after calling this(...), you can do everything you could in any other constructor.

Edit: Since this(...) calls a constructor, it can only be called from within other constructors (belonging to the same class).

爱冒险 2024-10-27 07:42:36

我的班级
{
私有 int var1;
私有 int var2;
私有布尔标志;

公共 MyClass(int var1_,int var2_)
{
这(var1_,var2_,假);
}

public MyClass(int var1_,int var2_,布尔标志_)
{
var1 = var1_;
var2 = var2_;
标志=标志_;
}

公共字符串 toString()
{
return (new Boolean(flag).toString());
}

公共静态无效主(字符串[] args)
{
MyClass my = new MyClass(5,6);
System.out.println(我的);
所以

有效。

class MyClass
{
private int var1;
private int var2;
private boolean flag;

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

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

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

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

}

So it works.

逆光下的微笑 2024-10-27 07:42:36

this() 不是方法,而是指向同一类的重载构造函数的保留关键字。
您传递的参数数量应指向类中定义的现有相应构造函数。

super() 也具有语义,但构造函数是在其父层次结构之一中定义的。

this() is not a method but is a reserved keyword pointing to a overloaded constructor of the same class.
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文