java this 代表什么_java-“ this()”方法是什么意思?

java-“ this()”方法是什么意思?

我遇到了这段代码,我不退出这一行来理解它的含义或它在做什么。

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);

}

}

我了解this.method()或this.variable是什么,但是this()是什么?

8个解决方案

55 votes

这是构造函数重载:

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);

}

}

}

您可以通过缺少返回类型来区分此代码是构造函数,而不是方法。这非常类似于在构造函数的第一行中调用super()以初始化扩展类。 您应该在构造函数的第一行中调用this()(或this()的任何其他重载),从而避免构造函数代码重复。

您也可以看看这篇文章:Java中的构造方法重载-最佳实践

Avi answered 2020-07-14T14:57:18Z

10 votes

使用this()这样的函数,实质上是调用该类的Constructor。 这使您可以在一个构造函数中进行所有通用的初始化,而在其他构造函数中进行特殊化。 因此,例如在这段代码中,对2966227781681681677312的调用正在调用具有一个int参数的Digraph构造函数。

Sinkingpoint answered 2020-07-14T14:57:38Z

9 votes

此代码段是一个构造函数。

对this的此调用将调用同一类的另一个构造函数

public App(int input) {

}

public App(String input) {

this(Integer.parseInt(input));

}

在上面的示例中,我们有一个采用super()的构造函数和一个采用String的构造函数。采用String的构造函数将String转换为int,然后委托给int构造函数。

请注意,对另一个构造函数或超类构造函数(super())的调用必须是构造函数中的第一行。

也许看一下它,以获得有关构造函数重载的更详细说明。

Boris the Spider answered 2020-07-14T14:58:16Z

4 votes

几乎一样

public class Test {

public Test(int i) { /*construct*/ }

public Test(int i, String s){ this(i); /*construct*/ }

}

Antimony answered 2020-07-14T14:58:36Z

3 votes

调用this本质上将调用类Constructor。例如,如果要扩展某些内容,则可以与296622909280433030一起使用:this.add(JComponent).

user2228462 answered 2020-07-14T14:58:56Z

3 votes

具有int参数的Digraph类的另一个构造函数。

Digraph(int param) { /* */ }

Nándor Krácser answered 2020-07-14T14:59:16Z

2 votes

构造函数重载:

例如:

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

}

}

LMK answered 2020-07-14T14:59:41Z

2 votes

this()是构造函数,用于在一个类中调用另一个构造函数,例如:-

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

}

}

注意: 我没有在默认构造函数中使用this()构造函数,因为它将导致死锁状态。

希望这个能对您有所帮助:)

user27 answered 2020-07-14T15:00:10Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值