Napisałem sobie klasę i chciałem zadeklarować sobie tablicę obiektów tej klasy jednak zmiana obiektu na np pozycji 6 pociąga za sobą zmianę wszystkich elementów tejże tablicy.
Oto kod klasy:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.yourorghere;
/**
*
* @author Ka
*/
public class Pkt3D {
float x,y,z;
float bold,r,b,g = 1;
public Pkt3D(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
public void madeSign(float bold, float r, float g, float b){
this.bold = bold;
this.r = r;
this.g = g;
this.b = b;
}
public String getPos() {
String info = "x:"+this.x+" y:"+this.y+" z:"+this.z;
return info;
}
public void madePos(float x, float y, float z){
this.x = x;
this.y = y;
this.z = z;
}
}
A tu kod programu:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author Ka
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Pkt3D pu = new Pkt3D((float) 0.5, 3, 2);
Pkt3D tab[] = new Pkt3D[16];
for(int i = 0;i<15;i++){
tab[i]=pu;
tab[i].x=i;
tab[i].y=i;
tab[i].z=i;
for(int j =0;j<=i;j++)System.out.println(tab[i].getPos()+"/");
}
// TODO code application logic here
}
}
Krótko mówiąc, co robię źle ?