我是Java的新手,剛剛擺弄了一段時間的代碼。從構造函數創建對象
public class ThreeVector {
private double x,y,z; // definign local variables
public ThreeVector(){} // a constructor that has no input
public ThreeVector (double va1,double va2, double va3){va1=x;va2=y;va3=z;};// creatign a constructor , so can be used for calling by a method later
// Takes 3 values
public double magnitude(){
double y1= Math.sqrt(x*x+y*y+z*z);
return y1 ; // finds the magnitude of a vector
}
public ThreeVector unitv(){
ThreeVector unitv= new ThreeVector();
unitv.ThreeVector(x/magnitude(),y/magnitude(),z/magnitude());
}
現在這裏是我卡住的地方。我創建了一個對象unitV
,所以我可以調用ThreeVector
構造函數,但編譯器不停地說要爲ThreeVector
創建一個新方法。 不知道怎麼回事......
你是如何創建對象?你可以添加試圖使用'ThreeVector'的代碼嗎? –
閱讀http://www.javabeginner。com/learn-java/java-constructors – MayurB
你有一個方法定義爲'public ThreeVector unitv()...'但不返回ThreeVector的實例。我想你想要移除unitv方法並做一些像Luiggi建議的事情。 – km1