public GObject(Point3D[] v, Face[] f){
vertex = v;
face = f;
}
public GObject(String fileName){
try{
...//read contents of file and store in an array
Point3D[] vertices = new Point3D[numOfVertices];
} catch(Exception e){
System.out.println("Can't read file " + e.getMessage());
}
}
두 번째 생성자는 전달 된 파일을 읽고 해당 값을 vertices 배열에 성공적으로 저장했지만 두 번째 생성자의 vertices 배열을 매개 변수로 첫 번째 생성자로 전달하면 어떻게 v = vertices
됩니까?
당신은 사용해야합니다 this
-
public GObject(String fileName){
this(new Point3D[numOfVertices], new Face[5]); // `5` is just for example.
try{
...//read contents of file and store in an array
Point3D[] vertices = new Point3D[numOfVertices];
} catch(Exception e){
System.out.println("Can't read file " + e.getMessage());
}
}
이 방법을 사용하는 경우 call to this
는 두 번째 생성자의 첫 번째 문이어야합니다. 이 제한 사항을 따르기 어렵다면 다음과 같이하시기 바랍니다.
public GObject(Point3D[] v, Face[] f){
setV(v);
face = f;
}
public GObject(String fileName){
try{
...//read contents of file and store in an array
setV(new Point3D[numOfVertices]);
} catch(Exception e){
System.out.println("Can't read file " + e.getMessage());
}
}
private void setV(Point3D[] v) {
vertex = v;
}
Face
다른 생성자를 호출하기 위해 배열 을 생성하도록 강요하지 않기 때문에 두 번째 방법이 더 낫다고 생각합니다 . 또한 나중에 setting logic
쉽게 유효성 검사를 변경 하거나 통합 할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다