Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.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/3/arrays/12.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对象被覆盖而不是新建_Java_Arrays_Oop_Object - Fatal编程技术网

Java对象被覆盖而不是新建

Java对象被覆盖而不是新建,java,arrays,oop,object,Java,Arrays,Oop,Object,在我的下一个Java项目中,我想创建一个Vector(Vektor命名)类,以便自己保存1、2或3维向量。类Vektor对于创建时输入的每个参数都有三个构造函数。它还有一个show()方法,可以将Vektor打印为数组。启动VEKTOR时,出现以下问题: 起始代码: public static void main(String[] args) { Vektor x = new Vektor(1); Vektor xy = new Vektor(2,3); Vektor xyz = new Vek

在我的下一个Java项目中,我想创建一个Vector(Vektor命名)类,以便自己保存1、2或3维向量。类Vektor对于创建时输入的每个参数都有三个构造函数。它还有一个show()方法,可以将Vektor打印为数组。启动VEKTOR时,出现以下问题: 起始代码:

public static void main(String[] args) {
Vektor x = new Vektor(1);
Vektor xy = new Vektor(2,3);
Vektor xyz = new Vektor(4,5,6);
x.show();
xy.show();
xyz.show();
输出:

[4,5,6]
[4,5,6]
[4,5,6]
Vektor类如下所示:

public static int[] alpha = new int[3];
public Vektor(int x) {
this.x = x;
alpha[0] = x;
}
public int[] show() {
System.out.println(Arrays.toString(alpha));
return alpha;
当然,构造函数会随着参数的数量而变化。除了在向量中存储所有变量的数组alpha之外,还有变量x、y、z,当它们出现在构造函数中时,它们的值会被填充。 我真的希望有人能帮我弄清楚为什么我会得到三次相同的输出,而不是三次不同的输出,因为我创建了三个不同的对象。谢谢,卢卡

更新:发现问题。数组不应归类为“静态”。
谢谢大家。

根据您提供的代码,似乎每次实例化
Vektor
类时,
alpha
数组都会更新,以保存最近实例化的一些(如果不是全部)最新构造函数参数


您需要将alpha数组从静态更改为公共(理想情况下使用getter/setter实现私有),以将
alpha
数组封装在创建它的每个对象中

请显示
alpha
的声明。我猜是静态的。。。(提示:始终显示一个变量,而不仅仅是代码的一部分。)静态变量对于每个对象都是相同的。删除“静态”应该有效