Java 使用;这";带构造函数的关键字-如何使用;这";是否引用点对象?
我拿着这个,不明白这个例子中的这个是怎么指Java 使用;这";带构造函数的关键字-如何使用;这";是否引用点对象?,java,this,Java,This,我拿着这个,不明白这个例子中的这个是怎么指 origin = new Point(0,0); 反对 矩形类的第一个示例 public class Rectangle { public int width = 0; public int height = 0; public Point origin; // four constructors public Rectangle() { origin = new Point(0, 0); } public Rectangle(Point p)
origin = new Point(0,0);
反对
矩形类的第一个示例
public class Rectangle {
public int width = 0;
public int height = 0;
public Point origin;
// four constructors
public Rectangle() {
origin = new Point(0, 0);
}
public Rectangle(Point p) {
origin = p;
}
public Rectangle(int w, int h) {
origin = new Point(0, 0);
width = w;
height = h;
}
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
// a method for moving the rectangle
public void move(int x, int y) {
origin.x = x;
origin.y = y;
}
// a method for computing the area of the rectangle
public int getArea() {
return width * height;
}
}
使用this关键字的矩形示例
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 1, 1);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
...
}
第二个示例在何处引用原点=新点(0,0)对象
如果您没有添加这个
关键字,那么默认情况下它是为类的实例成员添加的(隐式)
因此,无论您是否使用this
关键字,都是一样的
请看一看
阅读另一篇关于
如果您没有添加这个
关键字,那么默认情况下它是为类的实例成员添加的(隐式)
因此,无论您是否使用this
关键字,都是一样的
请看一看
阅读另一篇关于
如果您没有添加这个
关键字,那么默认情况下它是为类的实例成员添加的(隐式)
因此,无论您是否使用this
关键字,都是一样的
请看一看
阅读另一篇关于
如果您没有添加这个
关键字,那么默认情况下它是为类的实例成员添加的(隐式)
因此,无论您是否使用this
关键字,都是一样的
请看一看
阅读另一篇关于的文章,第二个例子似乎没有提到Point类 出于某种奇怪的原因,他们显然只是改变了类的工作方式,用整数x和y替换了“Point”类(实际上只是两个整数) 要重新编写第二个类,使其实际工作方式与第一个类相同,请按如下方式编写:
public class Rectangle {
private Point origin;
private int width, height;
public Rectangle() {
this(new Point(0,0), 1, 1);
}
public Rectangle(int width, int height) {
this(new Point(0,0), width, height);
}
//One more constructor just for fun
public Rectangle(int x, int y, int width, int height) {
this(new Point(x, y), width, height);
}
//Note: All of the constructors are calling this method via this(Point, int, int)
public Rectangle(Point origin, int width, int height) {
this.origin = origin;
this.width = width;
this.height = height;
}
...
}
看起来第二个示例没有引用Point类 出于某种奇怪的原因,他们显然只是改变了类的工作方式,用整数x和y替换了“Point”类(实际上只是两个整数) 要重新编写第二个类,使其实际工作方式与第一个类相同,请按如下方式编写:
public class Rectangle {
private Point origin;
private int width, height;
public Rectangle() {
this(new Point(0,0), 1, 1);
}
public Rectangle(int width, int height) {
this(new Point(0,0), width, height);
}
//One more constructor just for fun
public Rectangle(int x, int y, int width, int height) {
this(new Point(x, y), width, height);
}
//Note: All of the constructors are calling this method via this(Point, int, int)
public Rectangle(Point origin, int width, int height) {
this.origin = origin;
this.width = width;
this.height = height;
}
...
}
看起来第二个示例没有引用Point类 出于某种奇怪的原因,他们显然只是改变了类的工作方式,用整数x和y替换了“Point”类(实际上只是两个整数) 要重新编写第二个类,使其实际工作方式与第一个类相同,请按如下方式编写:
public class Rectangle {
private Point origin;
private int width, height;
public Rectangle() {
this(new Point(0,0), 1, 1);
}
public Rectangle(int width, int height) {
this(new Point(0,0), width, height);
}
//One more constructor just for fun
public Rectangle(int x, int y, int width, int height) {
this(new Point(x, y), width, height);
}
//Note: All of the constructors are calling this method via this(Point, int, int)
public Rectangle(Point origin, int width, int height) {
this.origin = origin;
this.width = width;
this.height = height;
}
...
}
看起来第二个示例没有引用Point类 出于某种奇怪的原因,他们显然只是改变了类的工作方式,用整数x和y替换了“Point”类(实际上只是两个整数) 要重新编写第二个类,使其实际工作方式与第一个类相同,请按如下方式编写:
public class Rectangle {
private Point origin;
private int width, height;
public Rectangle() {
this(new Point(0,0), 1, 1);
}
public Rectangle(int width, int height) {
this(new Point(0,0), width, height);
}
//One more constructor just for fun
public Rectangle(int x, int y, int width, int height) {
this(new Point(x, y), width, height);
}
//Note: All of the constructors are calling this method via this(Point, int, int)
public Rectangle(Point origin, int width, int height) {
this.origin = origin;
this.width = width;
this.height = height;
}
...
}
你的确切问题是什么?你是在说这个(…)电话吗?好的,我明白了,他们不能把前两个承包商除掉吗?此外,在使用“this”时,这是否会去除点对象?@Jigar Joshi它不是重复的。OP谈论的
这个
用于示例成员。你的确切问题是什么?你是在说这个(…)电话吗?好的,我明白了,他们不能把前两个承包商除掉吗?此外,在使用“this”时,这是否会去除点对象?@Jigar Joshi它不是重复的。OP谈论的这个
用于示例成员。你的确切问题是什么?你是在说这个(…)电话吗?好的,我明白了,他们不能把前两个承包商除掉吗?此外,在使用“this”时,这是否会去除点对象?@Jigar Joshi它不是重复的。OP谈论的这个
用于示例成员。你的确切问题是什么?你是在说这个(…)电话吗?好的,我明白了,他们不能把前两个承包商除掉吗?此外,在使用“this”时,这是否会去除点对象?@Jigar Joshi它不是重复的。OP谈论这个
用于示例成员。是的,我知道两个示例是相同的。我不明白在“this”示例中,点对象的引用在哪里……对不起,我不清楚。你在问什么?请再解释一下。您在代码中谈论的是哪一行?在非“this”示例中,我们将第一个构造函数设置为使用public class Point()。在第二个例子中,使用“this”的位置是什么?啊!在第二个例子中,我看不到这个。origin=新点(0,0)。请看biggus的评论。@StacyM您的问题有些令人困惑。你混合了很多东西。下次在第二个示例中使用?
,如果您对代码有疑问,请说明是否更清楚。是的,我知道两个示例是相同的。我不明白在“this”示例中,点对象的引用在哪里……对不起,我不清楚。你在问什么?请再解释一下。您在代码中谈论的是哪一行?在非“this”示例中,我们将第一个构造函数设置为使用public class Point()。在第二个例子中,使用“this”的位置是什么?啊!在第二个例子中,我看不到这个。origin=新点(0,0)。请看biggus的评论。@StacyM您的问题有些令人困惑。你混合了很多东西。下次在第二个示例中使用?
,如果您对代码有疑问,请说明是否更清楚。是的,我知道两个示例是相同的。我不明白在“this”示例中,点对象的引用在哪里……对不起,我不清楚。你在问什么?请再解释一下。您在代码中谈论的是哪一行?在非“this”示例中,我们将第一个构造函数设置为使用public class Point()。在第二个例子中,使用“this”的位置是什么?啊!在第二个示例中,我没有看到这个。origin=新点(0,0)。请参见biggus的注释。@Sta