简体   繁体   English

Java参数化的构造函数

[英]Java parameterized constructors

I'm new to programming and also new to Java. 我是编程新手,也是Java新手。 What i'm trying right now is using a parameterized constructor, and then use the created objects with a specific method. 我现在正在尝试使用参数化构造函数,然后将创建的对象与特定方法一起使用。 This is the code i have: 这是我的代码:

public class Car {
int fuelcap;
int mpg;

Car(int f, int m) {      //here
    fuelcap = f;         //and here
    mpg = m;             //and here
}

int range() {
    return mpg * fuelcap;
}

public static void main(String[] args) {
    Car sedan = new Car(16, 21);
    Car sportscar = new Car(14, 16);
    System.out.println(sedan.range());
    System.out.println(sportscar.range());

}

}

The problem is, i don't know why the parameters of the constructor Car - 'int f' and 'int m' are different from the fields: 'int fuelcap;' 问题是,我不知道为什么构造函数Car-'int f'和'int m'的参数与以下字段不同:'int fuelcap;' 'int mpg;'. 'int mpg;'。 Can't we just create are constructor like this: 我们不能只是创建像这样的构造函数:

Car(int fuelcap, int mpg){
}

and then just pass values to those parameters when creating the objects? 然后在创建对象时将值传递给那些参数?

No, you cannot. 你不能。 You must assign the constructor parameters to the class fields in the body of your constructor, or they will not be saved. 您必须将构造函数参数分配给构造函数主体中的class字段,否则将不会保存它们。 That's just how Java works. 这就是Java的工作方式。 There's nothing "magical" about the constructor parameters. 构造函数参数没有什么“神奇”的东西。

Also, observe proper style: 另外,请注意适当的样式:

Car(int fulecap, int mpg) {     
    this.fuelcap = fuelcap;         
    this.mpg = mpg;            
}

If you have more fields then ctor parameters, that's just fine. 如果您还有更多字段,那么可以使用ctor参数,就可以了。 Just think of the constructor as a normal method that gets called after the object is created. 只需将构造函数视为创建对象后调用的普通方法即可。 (That last statement is only sorta-kinda true, but will give you the correct idea for this purpose.) (最后的陈述只是一种正确的说法,但会为此目的提供正确的想法。)

Yes off course you can, but in that case your constructor should look like as follows: 是的,您当然可以,但是在那种情况下,您的构造函数应如下所示:

public Car(int fuelcap, int mpg){
 this.fuelcap = fuelcap;
 this.mpg = mpg;
}

You have the instance variables fuelcap and mpg but if you do the constructor you mention, they will never be assigned to anything. 您具有实例变量fuelcapmpg但是如果您提到的构造函数,则它们永远不会分配给任何东西。

You could do this to make it more readable, 您可以这样做以使其更具可读性,

int fuelcap;
int mpg;

Car(int fuelcap, int mpg) { 
    this.fuelcap = fuelcap; //this is refrencing the instance variable in the class    
    this.mpg = mpg;             
}

int range() {
    return mpg * fuelcap;
}

The constructor just doesn't recognize the way you want because it can cause errors in variables with the same names. 构造函数无法识别您想要的方式,因为它可能导致名称相同的变量出错。

Parameterized constructor is used to provide different values to the distinct objects. 参数化构造函数用于为不同的对象提供不同的值。

if you want to use 如果你想使用

Car(int fuelcap, int mpg){}

you must use this keyword 您必须使用this关键字

so the code becomes 所以代码变成

Car(int f, int m) {      
this.fuelcap = f;         
this.mpg = m; }

If you dont use this keyword 如果您不使用this关键字

all objects will have the same values as in the instance variables ie, fuelcap and mpg right now its value is 0 所有对象的值将与实例变量中的值相同,即fuelcapmpg现在的值为0

Just a few hints considering good practice: 考虑良好做法的几点提示:

First you should make your members private ( link ). 首先,您应该将成员privatelink )。 Moreover, it is good style to make your objets "immutable", meaning, once they are created, they cannot change the state ( link ). 此外,将objets设置为“不可变的”是一种很好的样式,这意味着一旦创建它们,它们就无法更改状态( link )。 You can achieve this by declaring your fields as final . 您可以通过将字段声明为final来实现。

The Car class would then look like this: Car类将如下所示:

public class Car
{
  private final int fuelcap;

  private final int mpg;


  public Car( final int fuelcap, final int mpg )
  {
    this.fuelcap = fuelcap;
    this.mpg = mpg;
  }


  public int range()
  {
    return this.mpg * this.fuelcap;
  }
}

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

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