Java string returns as null(Java 字符串返回为 null)
问题描述
我试图让一个类从另一个类返回一个字符串,尽管我得到的返回是空的.我有一个 set 方法可以在原始类中设置字符串,但是在我的第二个类中调用该方法时,我得到了 null 的返回.
I am attempting to get one class to return a string from another class, though the return I get is null. I have a set method that works in setting the string in the original class, but when calling the method in my second class, I get a return of 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 方法中输入的字符串.
And the second class, in which I am trying to call the string I input in the setFlavour method in the println of the sundaeDetails method.
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 类的构造函数中你有:
In IceCream class constructor you have:
String flavour = getFlavour()
您创建了一个局部变量,而不是对实例属性的引用.getFlavour() 方法返回您从未设置的属性实例,因此它为空.你应该在构造函数中有这样的东西:
You have created a local variable instead a reference to a instance property. getFlavour() method return the property instance that you never set, so its null. You should have something like this in the constructor:
this.flavour = "default value";
或者在构造函数头部设置一个flavor参数:
Or set a flavour parameter on constructor header:
public IceCream(String flavour) {
this.flavour = flavour;
(...)
}
然后这样称呼它:
IceCream chocolat = new IceCream(" chocolat");
如果你想改变风格,请使用 setter.
And if you want change the flavour use the setter.
这篇关于Java 字符串返回为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 字符串返回为 null
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01