"this"是什么意思?
What is the meaning of "this"?
class ServerSocket{
....
public ServerSocket(int port) throws IOException {
this(port, 50, null);
}
....
}
我知道这个关键字是用来表示活动对象或当前对象的。在这段代码中,"this" 是什么意思?
我从 (here)
得到了这段代码
在您的代码片段中,this(port, 50, null)
表示在同一个 class 中对 构造函数 的显式调用,它具有三个参数,其中两个您传递硬编码参数(50
和 null
)
this(port, 50, null);
表示调用当前 class 的构造函数,它以 int, int, Object
(我猜)作为参数。this
指的是当前的Object
。这里它用于从其他构造函数 调用构造函数,即 从 ServerSocket(int port)
调用 ServerSocket(int port, int num, Object x)
this(port, 50, null)
调用当前 class 构造函数。
@this keyword
- can be used to refer current class instance variable.
- this() can be used to invoke current class constructor.
- this keyword can be used to invoke current class method (implicitly)
- this can be passed as an argument in the method call.
- this can be passed as argument in the constructor call.
- this keyword can also be used to return the current class instance @.
class ServerSocket{
....
public ServerSocket(int port) throws IOException {
this(port, 50, null);
}
....
}
我知道这个关键字是用来表示活动对象或当前对象的。在这段代码中,"this" 是什么意思? 我从 (here)
得到了这段代码在您的代码片段中,this(port, 50, null)
表示在同一个 class 中对 构造函数 的显式调用,它具有三个参数,其中两个您传递硬编码参数(50
和 null
)
this(port, 50, null);
表示调用当前 class 的构造函数,它以 int, int, Object
(我猜)作为参数。this
指的是当前的Object
。这里它用于从其他构造函数 调用构造函数,即 从 ServerSocket(int port)
ServerSocket(int port, int num, Object x)
this(port, 50, null)
调用当前 class 构造函数。
@this keyword
- can be used to refer current class instance variable.
- this() can be used to invoke current class constructor.
- this keyword can be used to invoke current class method (implicitly)
- this can be passed as an argument in the method call.
- this can be passed as argument in the constructor call.
- this keyword can also be used to return the current class instance @.