[英]Java string returns as null
我正在嘗試讓一個類從另一類返回字符串,盡管我得到的返回值為null。 我有一個set方法可以在原始類中設置字符串,但是在第二個類中調用該方法時,返回的是null。
這是頭等艙;
public class IceCream
{
// instance variables - replace the example below with your own
private String flavour;
public static double price;
/**
* Constructor for objects of class IceCream
*/
public IceCream()
{
// initialise instance variables
String flavour = getFlavour();
price = 0.50;
}
/**
* Gets price in pence.
*
*
* @returns the price of the ice cream.
*/
public static double getPrice()
{
// put your code here
return price;
}
public int getScoops()
{
return scoop;
}
public void setPrice(int newPrice)
{
price = newPrice;
}
public void setScoops(int scoopNumber)
{
scoop = scoopNumber;
}
public double totalCost()
{
double cost;
cost = scoop * price;
return cost;
}
public String getFlavour()
{
return flavour;
}
public void setFlavour(String whatFlavour)
{
flavour = whatFlavour;
}
}
第二類,我試圖在其中調用在sundaeDetails方法的println中的setFlavour方法中輸入的字符串。
import java.util.ArrayList;
/**
* Write a description of class Sundae here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Sundae
{
// instance variables - replace the example below with your own
private IceCream flavour;
private Topping SundaeTopping;
private int scoops;
/**
* Constructor for objects of class Sundae
*/
public Sundae()
{
flavour = new IceCream();
SundaeTopping = new Topping();
scoops = 0;
}
/**
* Set scoop number.
*/
public void setScoops(int scoopNumber)
{
scoops = scoopNumber;
}
/**
* Return scoop variable.
*/
public int getScoops()
{
return scoops;
}
/**
* Get the price of the sundae.
*/
public void getPrice()
{
double cost;
double scoopPrice = scoops * IceCream.getPrice();
if ( scoops > 0) {
cost = scoopPrice * Topping.getToppingPrice();
System.out.println("Cost of Sundae: " + cost);
}
else {
System.out.println("Need to have a scoop of ice cream in your Sundae.");
}
}
/**
* Return the details of the sundae; price, flavour, scoops etc.
*/
public void sundaeDetails()
{
System.out.println("You have " + scoops + " scoops of " + flavour.getFlavour() + "ice cream");
}
}
在IceCream類構造函數中,您可以:
String flavour = getFlavour()
您已經創建了一個局部變量,而不是對實例屬性的引用。 getFlavour()方法返回您從未設置的屬性實例,因此它為null。 您應該在構造函數中具有以下內容:
this.flavour = "default value";
或在構造函數標頭上設置一個flavor參數:
public IceCream(String flavour) {
this.flavour = flavour;
(...)
}
並這樣稱呼:
IceCream chocolat = new IceCream(" chocolat");
如果您想改變口味,請使用二傳手。
您永遠不會調用flavour.setFlavour()
,因此flavour.getFlavour()
返回null flavour.getFlavour()
。
風味不是初始化的private String flavour;
String flavour = getFlavour()
,在調用IceCream()
您都設置了String flavour = getFlavour()
,這String flavour = getFlavour()
構造函數內部的局部變量flavour
設置為null,因為getFlavour()
返回null
。 同樣,構造函數中的本地String flavour
變量也會使IceCream
類的String flavour
字段蒙上陰影。 嘗試將flavor作為構造函數的參數,然后將字段設置為該字符串
public IceCream(String f) {
this.flavour = f;
price = 0.50;
}
或在創建對象之前調用setFlavour()
。
問題始於IceCream類的構造函數。
private String flavour;
public IceCream()
{
// initialise instance variables
String flavour = getFlavour();
price = 0.50;
}
public String getFlavour()
{
return flavour;
}
第一個問題是,您正在隱藏成員變量的flavour
-您在聲明另一個具有相同名稱但范圍僅限於構造函數的變量。 第二個問題是您要分配getFlavour()
返回的值,該值只是一個返回成員變量本身(其初始值為null)的訪問器。
要修復它,您需要在構造函數中分配一個初始值
public IceCream()
{
// initialise instance variables
this.flavour = "Vanilla"
price = 0.50;
}
聲明初始值時也可以賦值
private String flavour = "Vanilla";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.