我试图让一个类从另一个类返回一个字符串,尽管我得到的返回值为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");
}
}
答案 0 :(得分:1)
在IceCream类构造函数中,您有:
String flavour = getFlavour()
您已创建局部变量,而不是对实例属性的引用。 getFlavour()方法返回您从未设置的属性实例,因此其null。你应该在构造函数中有这样的东西:
this.flavour = "default value";
或者在构造函数头上设置flavor参数:
public IceCream(String flavour) {
this.flavour = flavour;
(...)
}
并称之为:
IceCream chocolat = new IceCream(" chocolat");
如果你想改变味道,请使用setter。
答案 1 :(得分:0)
你永远不会打电话给flavour.setFlavour()
,所以毫不奇怪flavour.getFlavour()
返回null。
答案 2 :(得分:0)
在调用private String flavour;
时设置IceCream()
并将构造函数内的局部变量String flavour = getFlavour()
设置为null,因此{{1>}没有初始化flavour
,因为{{1} }}返回getFlavour()
。此外,构造函数中的本地null
变量会遮盖String flavour
类的String flavour
字段。尝试将flavor作为构造函数的参数,然后将该字段设置为该字符串
IceCream
或在使用您创建的对象之前调用public IceCream(String f) {
this.flavour = f;
price = 0.50;
}
。
答案 3 :(得分:0)
问题从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";