I ran into this block of code, and there is this one line I don't quit understand the meaning or what it is doing.
public Digraph(In in) {
this(in.readInt());
int E = in.readInt();
for (int i = 0; i < E; i++) {
int v = in.readInt();
int w = in.readInt();
addEdge(v, w);
}
}
I understand what this.method()
or this.variable
are, but what is this()
?
This is constructor overloading:
public class Diagraph {
public Diagraph(int n) {
// Constructor code
}
public Digraph(In in) {
this(in.readInt()); // Calls the constructor above.
int E = in.readInt();
for (int i = 0; i < E; i++) {
int v = in.readInt();
int w = in.readInt();
addEdge(v, w);
}
}
}
You can tell this code is a constructor and not a method by the lack of a return type.
This is pretty similar to calling super()
in the first line of the constructor in order to initialize the extended class. You should call this()
(or any other overloading of this()
) in the first line of your constructor and thus avoid constructor code duplications.
You can also have a look at this post: Constructor overloading in Java - best practice
Using this() as a function like that, essentially calls the Constructor of the class. This allows you to all the generic initializations in one constructor and have specializations in others. So in this piece of code for example, the call to this(in.readInt())
is calling the Digraph constructor that has one int argument.
This code snippet is a constructor.
This call to this
calls another constructor of the same class
public App(int input) {
}
public App(String input) {
this(Integer.parseInt(input));
}
In the above example we have a constructor that takes an int
and one that takes a String
. The constructor that takes a String
converts the String
to an int
and then delegates to the int
constructor.
Note that a call to another constructor or a superclass constructor (super()
) must be the first line in a constructor.
Maybe take a look at this for a more detailed explanation of constructor overloading.
It's nearly the same
public class Test {
public Test(int i) { /*construct*/ }
public Test(int i, String s){ this(i); /*construct*/ }
}
Calling this
essentially calls the class Constructor.
For example, if you're extending something, than along with add(JComponent)
, you could do: this.add(JComponent).
An other constructor of the class Digraph with an int parameter.
Digraph(int param) { /* */ }
Constructor Overloading:
ex:
public class Test{
Test(){
this(10); // calling constructor with one parameter
System.out.println("This is Default Constructor");
}
Test(int number1){
this(10,20); // calling constructor with two parameter
System.out.println("This is Parametrized Constructor with one argument "+number1);
}
Test(int number1,int number2){
System.out.println("This is Parametrized Constructor with two argument"+number1+" , "+number2);
}
public static void main(String args[]){
Test t = new Test();
// first default constructor,then constructor with 1 parameter , then constructor with 2 parameters will be called
}
}
this();
is constructor which is used to call another constructor in a class,
for example:-
class A{
public A(int,int)
{ this(1.3,2.7);-->this will call default constructor
//code
}
public A()
{
//code
}
public A(float,float)
{ this();-->this will call default type constructor
//code
}
}
Note:
i did not use this()
constructor in default constructor because it will lead to deadlock state.
Hope this will help you:)
来源:https://stackoverflow.com/questions/15867722/what-does-this-method-mean