我是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