java-关于“的解释”;这";陈述
我是Java新手,对以下程序的结果感到困惑,有人能详细介绍一下交换场景吗 另外,有人能给我举几个关于java-关于“的解释”;这";陈述,java,Java,我是Java新手,对以下程序的结果感到困惑,有人能详细介绍一下交换场景吗 另外,有人能给我举几个关于这个关键字的例子吗 class P { int i; void test1(P p1, P p2) { int i = p1.i; p1.i = p2.i; p2.i = i;; } void test2(P p1) { int i = this.i; this.i = p1.i
这个
关键字的例子吗
class P {
int i;
void test1(P p1, P p2) {
int i = p1.i;
p1.i = p2.i;
p2.i = i;;
}
void test2(P p1) {
int i = this.i;
this.i = p1.i;
p1.i = i;
}
public static void main(String[] args) {
P p1 = new P();
P p2 = new P();
p1.i = 1;
p2.i = 2;
p1.test1(p1, p2);
System.out.println(p1.i + "," + p2.i);
p1.test(p2);
System.out.println(p1.i + "," + p2.i);
}
}
谢谢。
此
是对当前对象的引用
使用this
关键字的最常见原因是字段被方法或构造函数参数隐藏
您可以将this
用作构造函数->this()
this()
是对同一类中另一个构造函数的引用
public Rectangle() {
this(0, 0, 0, 0); // calls the constructor below
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
此方法可以是静态的,因为它只是交换值,不依赖于实例
void test2(P p2) {
int i = this.i;
this.i = p2.i;
p2.i = i;
}
这里,this.i
指向您调用了test2
方法的对象,在您的例子中,它是p1
,您将p2
作为参数传递,该参数也执行相同的交换操作
因此,由于两种方法的作用相同:
在test1()之后以值1,2--
开始。
-->在test2()之后以值1,2--开始。这是一个指向自身的指针。
有时一个方法需要引用调用它的对象。为此,Java定义了“this”关键字。此外,“this”可以在任何方法中用于引用当前对象
例如,当我们有一个displayName()方法时
现在我们用对象f1调用它
f1.displayName() //compiler converts this thing in displayName(f1)
在displayName()方法中,它像这样收集对象f1
displayName(this)
{
System.out.println("Name: " + this.name); //here this.name means its referring to class member variable.
}
首先,它总是引用它所引用的当前对象。它的用法有不同的上下文:
- 如果在字段名前面使用,则它引用的是特定于该对象的字段名
下面的例子<构造函数中的code>fooField
会对字段进行阴影处理,但由于我们使用的是this
,所以我们指的是特定的Foo
对象的字段
public class Foo {
private String fooField;
public Foo(String fooField) {
this.fooField = fooField;
}
}
除非必要,否则不要用这个。它可能会导致代码混乱和不必要的歧义
this
的使用必须是构造函数声明中的第一个调用
下面的例子。这里我有两个不同的构造函数用于点
,我可以通过调用this(0,0,0)
来调用一个无参数的构造函数
test1
用它的变量做了一些有趣的事情……你可以把左右两边都考虑进去(为了消除歧义)
- 您可以为被调用方分配左边的值。//P1.i=1
- 左侧现在取右侧的值。//P1.i=2
- 右侧接受被调用方的值。//P2.i=1
test2
略有不同。您只有一个参数,因此它将是“参数”(为了消除歧义)
- 您可以显式地将被调用方的值重新分配给自身。//P1.i=P1.i=2
- 将参数的值赋给被调用方。//P1.i=1
- 将被调用方的值赋给参数。//P2.i=2
因此,您的输出结果为1,2。此关键字引用类的当前实例。 您还可以使用“this”来创建在每次调用后返回对象的方法。这对于类配置器非常有用 用法示例:
public class SomeCLass {
//some fields
//some constructors
public SomeClass setWidth(width) {
this.width = width;
return this;
}
public SomeClass setLength(length) {
this.length = length;
return this;
}
}
public class Execute {
public static void main(String[] args) {
(new SomeClass()).setWidth(10).setLength(15);
}
构造函数中的this()引用当前类的可用构造函数。因此,在构造函数中,是的,
this()
是一个方法。this()
引用当前对象-在构造函数中,可以调用this()
引用空构造函数,调用方式与调用super()
的方式大致相同。在代码中this.
-this
引用当前对象this()
是当前类的构造函数super()
指的是超类构造函数。顺便问一下,您在哪里使用this()
?我看到了this
但没有看到this()
?对不起,我的意思是说this
参考而不是this()
方法。请使用术语<代码>此既不是“声明”,也不是“方法”。感谢您的回复。在下面的方框中,“inti”和“i”在其相对术语中指的是什么。有什么具体原因吗<代码>无效测试1(p1,p2){inti=p1.ip1.i=p2.ip2.i=i;}
displayName(this)
{
System.out.println("Name: " + this.name); //here this.name means its referring to class member variable.
}
public class Foo {
private String fooField;
public Foo(String fooField) {
this.fooField = fooField;
}
}
public class Point {
private int x, y, z;
public Point() {
this(0, 0, 0);
}
public Point(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
public class SomeCLass {
//some fields
//some constructors
public SomeClass setWidth(width) {
this.width = width;
return this;
}
public SomeClass setLength(length) {
this.length = length;
return this;
}
}
public class Execute {
public static void main(String[] args) {
(new SomeClass()).setWidth(10).setLength(15);
}