Java String gibt als null @ zurü

Ich versuche, eine Klasse zu veranlassen, einen String von einer anderen Klasse zurückzugeben, obwohl der Rückgabewert null ist. Ich habe eine set-Methode, mit der die Zeichenfolge in der ursprünglichen Klasse festgelegt werden kann, aber wenn ich die Methode in meiner zweiten Klasse aufrufe, erhalte ich den Wert null.

Hier ist die erste Klasse;

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;
}

}

Und die zweite Klasse, in der ich versuche, die Zeichenfolge aufzurufen, die ich in der setFlavour-Methode im println der sundaeDetails-Methode eingegeben habe.

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");
   }
}

Antworten auf die Frage(8)

Ihre Antwort auf die Frage