简体   繁体   English

从构造函数创建对象

[英]Creating objects from constructors

I am a newbie to Java, just been fiddling with the code for a while. 我是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());
}

Now here is where I get stuck. 现在这是我卡住的地方。 I created an object unitV so I could call the ThreeVector constructor, but the compiler keeps saying to create a new method for ThreeVector . 我创建了一个对象unitV因此可以调用ThreeVector构造函数,但是编译器一直在说要为ThreeVector创建一个新方法。 Not sure whats going on... 不知道发生了什么...

A constructor can be called only using the new keyword. 只能使用new关键字调用构造函数。 What you're doing here: 您在这里做什么:

unitv.ThreeVector(x/magnitude(),y/magnitude(),z/magnitude());

is calling a method called ThreeVector , so the compiler complains that there's no such method in your ThreeVector class. 正在调用名为ThreeVector的方法,因此编译器抱怨在ThreeVector类中没有这样的方法。

To fix this, you must use the ThreeVector constructor with the arguments to create your unitv instance instead: 要解决此问题,必须使用带有参数的ThreeVector构造函数来创建unitv实例:

public ThreeVector unitv(){
    ThreeVector unitv = new ThreeVector(x/magnitude(),y/magnitude(),z/magnitude());
    //and, of course, return this ThreeVector instance
    return unitv;
}

And this code can be shorten to 这段代码可以简化为

public ThreeVector unitv() {
    return new ThreeVector(x/magnitude(),y/magnitude(),z/magnitude());
}

But since you can have your x , y and z with 0 value at the same time, it would be better to change the logic in your unitv method to first get the magnitude value and doing an evaluation, this in order to avoid a division by 0: 但是由于您可以同时将xyz值设置为0 ,因此最好更改unitv方法中的逻辑以首先获取magnitude值并进行求值,这样可以避免除以0:

public ThreeVector unitv() {
    double magnitude = magnitude();
    if (magnitude != 0) {
        return new ThreeVector(x/magnitude, y/magnitude, z/magnitude);
    }
    return new ThreeVector(0, 0, 0);
}

By the way, your constructor logic is wrong, you're assigning the fields values to the arguments, it should be the other way around: 顺便说一句,您的构造函数逻辑是错误的,您正在将字段值分配给参数,应该采用相反的方法:

public ThreeVector (double va1,double va2, double va3) {
    x = va1;
    y = va2;
    z = va3
}

No you need to directly call the constructor: 不,您不需要直接调用构造函数:

ThreeVector unitv= new ThreeVector(x,y,z);

Only after this you can call the magnitude method 只有在此之后,才能调用幅度方法

Also your constructor does the assignments the wrong way: 同样,您的构造函数会以错误的方式进行分配:

Should be: public ThreeVector (double va1,double va2, double va3){x = va1;y = va2;z=va3;} 应该是:public ThreeVector(双va1,双va2,双va3){x = va1; y = va2; z = va3;}

You need to initialize a new ThreeVector(va1, va2, va3) . 您需要初始化一个new ThreeVector(va1, va2, va3)

Attention 注意

Your code in the constructor is wrong. 您在构造函数中的代码是错误的。

{
    // instance field assigned to argument        
    va1=x;va2=y;va3=z;
}

... should be: ... 应该:

{
    // argument assigned to instance field
    x = va1;
    y = va2;
    z = va3;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM