问题描述
我遇到一个代码,其中 java 中的 this() 方法接受三个参数,两个是整数,第三个是布尔值.这到底是什么意思呢 ?this() 方法还有其他变体吗?Hera 是实际代码.
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);
}
谢谢..
推荐答案
这意味着当前类中有另一个具有该签名的构造函数.
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_;
}
this
方法只是从另一个构造函数中调用类的一个构造函数的一种方法,以帮助避免代码重复.它只能在构造函数的第一行调用——不能在任何其他方法中调用.
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.
这篇关于这是什么() ?它可以有多个参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!