public class Array
{
static String[] a = new String[] {"red", "green", "blue"};
static Point[] p = new Point[] {new Point(1, 2), "3,4"};
public static void main(String[] args)
{
System.out.println("hello");
}
class Point
{
int x;
int y;
Point(int x, int y)
{
this.x = x;
this.y = y;
}
Point(String s)
{
String[] a = s.split(",");
x = a[0].parseInt();
y = a[1].parseInt();
}
}
}
En el programa anterior, la estática Point
gama de inicialización falla, el informe de errores:matriz de objetos estáticos
Array.java:4: non-static variable this cannot be referenced from a static context
static Point[] p = new Point[] {new Point(1, 2), "3,4"};
embargo, la String
matriz estática tiene éxito. ¿Cual es la diferencia entre ellos?
Realmente necesito una matriz de objetos estáticos, porque es fácil de consultar sin instanciar la clase externa.
Gracias
Gracias. Quiero "3,4" para activar el punto (cadena). Parece que no puede hacerlo. – pengguang001
'Punto (Cadena xy) {Cadena [] tmp = xy.split (", "); x = Integer.parseInt (tmp [0]); y = Integer.parseInt (tmp [1]); } '? – khachik