簡體   English   中英

如何在構造函數中傳遞參數?

[英]How can I pass argument in the constructor?

我正在做一個簡單的Java作業。 看來我的語法和邏輯正確。 但是,構造函數無法理解我嘗試傳遞的參數,這就是所有計算錯誤的原因。 我已經附上了輸出。 有人知道如何解決此問題嗎? 任何幫助將不勝感激。 謝謝!

public class DT_CarStats
 {
   private double gallons;
   private double mpg;
   private double ppg;

public DT_CarStats(double gallons, double mpg, double ppg)
{
  gallons = gallons;
  mpg = mpg;
  ppg = ppg;
}

public double CostPer100()
{ 
  return 100 / (mpg * ppg);
}

public double MaxDistance()
{
  return mpg * gallons;
}
}
import java.util.Scanner;

public class DT_CarStatsTester
{
  public static void main(String[] args)
  {
    Scanner scannerObject = new Scanner(System.in);
    double gallons, mpg, ppg;
    System.out.printf("******************************************* \n");
    System.out.printf("* Welcome to my Distance to Empty App  * \n");
    System.out.printf("******************************************* \n");
    System.out.printf("Please enter the number of gallons of gas in the     tank: ");
    gallons = scannerObject.nextDouble();
    System.out.printf("Please enter the fuel efficiency (miles per gallon): ");
    mpg = scannerObject.nextDouble();
    System.out.printf("Please enter the price of gas per gallon: ");
    ppg = scannerObject.nextDouble();
    DT_CarStats cs = new DT_CarStats(gallons,mpg,ppg);
    System.out.printf("\n");
    System.out.printf("To drive 100 miles, it will cost $" + cs.CostPer100() + ". \n");
    System.out.printf("The car can currently drive a maximum of " + cs.MaxDistance() + " miles. \n");
    System.out.printf("******************************************* \n");
    System.out.printf("* Thanks for using our App, Safe Travels! * \n");
    System.out.printf("******************************************* \n");
  }
}

樣品運行:

******************************************* 
******************************************* 
* Welcome to my Distance to Empty App  * 
******************************************* 
Please enter the number of gallons of gas in the tank: 17.6
Please enter the fuel efficiency (miles per gallon): 24.8
Please enter the price of gas per gallon: 2.36

To drive 100 miles, it will cost $Infinity. ==> This is wrong, should be $9.52 
The car can currently drive a maximum of 0.0 miles. ==> This should be 436.48 miles
******************************************* 
* Thanks for using our App, Safe Travels! * 
******************************************* 

使用this來指定您的意思是類字段,而不是構造函數參數,因為它們的名稱相同。

public DT_CarStats(double gallons, double mpg, double ppg)
{
  this.gallons = gallons;
  this.mpg = mpg;
  this.ppg = ppg;
}
gallons = gallons;
mpg = mpg;
ppg = ppg;

提示:在這段代碼中, gallonsmpgppg什么?

它們是參數! 無論它們位於=哪一側,它們始終是參數。 =左側的變量名沒有特殊處理。

要訪問這些字段,請使用this

this.gallons = gallons;
this.mpg = mpg;
this.ppg = ppg;

或在字段中為參數指定不同的名稱。

此處,實例變量被方法參數覆蓋。 因此,要使用方法參數區分實例變量(兩個名稱相同時),請在構造函數中使用“ this”關鍵字。

 this.gallons = gallons;

在您的情況下,方法參數再次分配給一個值,而實例變量保持不變,因此值是錯誤的

在構造函數中,您要引用與傳入參數同名的字段。 您應該使用“ this”關鍵字來確保設置對象的變量this.gallons =加侖等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM