Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么Point2D的值有时表现为静态?_Java_Variables_Inheritance - Fatal编程技术网

Java 为什么Point2D的值有时表现为静态?

Java 为什么Point2D的值有时表现为静态?,java,variables,inheritance,Java,Variables,Inheritance,我搞乱了Java的Point2D.Double类,在设置为彼此相等时更改点的值时遇到了一些问题 以下是点在Java中的工作方式: /** Testing with Points */ System.out.println("Points: "); // Create a new point: Point2D.Double point1 = new Point2D.Double(1, 1); System.out.println(point1); //

我搞乱了Java的Point2D.Double类,在设置为彼此相等时更改点的值时遇到了一些问题

以下是点在Java中的工作方式:

/** Testing with Points */
    System.out.println("Points: ");

    // Create a new point:
    Point2D.Double point1 = new Point2D.Double(1, 1);
    System.out.println(point1);

    // Create another new point, based on the old point
    Point2D.Double point2 = point1;
    System.out.println(point2);

    // Change point1, print both again.
    point1.setLocation(0, 1);
    System.out.println(point1);
    System.out.println(point2);
该代码的输出将是:

Points: Point2D.Double[1.0, 1.0]Point2D.Double[1.0, 1.0]Point2D.Double[0.0, 1.0]Point2D.Double[0.0, 1.0]
请注意,点2以值[0.0,0.0]结束,即使唯一更改的点是点1


这是同样的代码,但带有原始整数:

/** Testing with Integers */
    System.out.println("Integers: ");

    // Create a new integer (primitive)
    int integer1 = 1;
    System.out.println(integer1);

    // Create another new integer (primitive), based on the old int.
    int integer2 = integer1;
    System.out.println(integer2);

    // Change integer1, print both again.
    integer1 = 0;
    System.out.println(integer1);
    System.out.println(integer2);
此代码的输出将是:

Integers: 1101

似乎只有Point2D类可以像这样在类之间传递值。setLocation函数的Point2D文档如下:

将此点2D的位置设置为指定的双坐标

注意单词THIS


实际上,我可以通过以下代码解决此问题:

Point2D.Double point2 = new Point2D.Double(point1.x, point1.y);
但是我仍然想理解为什么Point2D类是这样工作的,还有哪些类具有相同的属性


感谢您的阅读,我期待着阅读您的回复。

我认为您混淆了引用和实例(对象)。点1是对实例的引用,而点2是对同一实例的引用。修改实例时,观察指向该实例的引用并不重要

对于新的Point2D.Double(point1.x,point1.y),您正在创建一个新实例


对于INT,您也在使用新实例。

Point2D.Double是一个类。您只创建该对象的一个实例。 因此,通过使用:

Point2D.Double point2 = point1;
您只需要创建一个“指针”,它指向与第一个对象相同的内存。 在第二个示例中,将创建点对象的两个不同实例

请看我画得不好的照片


点1和点2引用同一对象。如果您更改了一个,则两个都会更改,除非您使用您发现的
new

它适用于int,因为int是一种基本数据类型,所以没有它的实例

阅读本文了解有关java指针、引用和传递参数的更多信息


非常感谢您的回答:)。根据您所说的,为什么代码“/**不使用Integers*/System.out.println(“Integers:”)进行测试;//创建一个新的整数(类)integer int1=new integer(1);System.out.println(int1);//基于旧整数创建另一个新整数(类)。整数int2=int1;System.out.println(int2);//更改整数1,再次打印两者。int1=0;System.out.println(整数1);System.out.println(整数2);'做与点代码相同的事情?这与改变点调用函数这一事实有关吗?:)我跑的时候得了1101分。当您打算打印int1和int2时,您正在打印integer1和integer2。@Heuster我认为您是对的,很抱歉重复了。在此之前,我从未听说过“参考类型”这个词,因此也没有找到那篇文章。对不起。@user2815708没问题!了解正确的术语是谷歌搜索的先决条件:-)