简体   繁体   English

一个类到另一个类的LinkedList中的特定元素

[英]specific elements from a LinkedList from one class to another

I have this current class that contains all these ingredients which I am trying to add into another linked list but I am struggling. 我目前正在上课,其中包含所有这些成分,我试图将它们添加到另一个链接列表中,但是我在努力。

import java.util.*;
public class Kitchen {
    public static final Category CRUST = new Category("crust", 1, 1);
    public static final Category SAUCE = new Category("sauce", 1, 1);
    public static final Category TOPPING = new Category("topping", 2, 3);
    private Category[] categories = { CRUST, SAUCE, TOPPING };
    private LinkedList<Ingredient> ingredients = new LinkedList<Ingredient>();

    public Kitchen() {
        ingredients.add (new Ingredient("Thin", 3.00, CRUST));
        ingredients.add (new Ingredient("Thick", 3.50, CRUST));
        ingredients.add (new Ingredient("Tomato", 1.00, SAUCE));
        ingredients.add (new Ingredient("Barbeque", 1.00, SAUCE));
        ingredients.add (new Ingredient("Capsicum", 0.50, TOPPING));
        ingredients.add (new Ingredient("Olives", 1.50, TOPPING));
        ingredients.add (new Ingredient("Jalapenos", 1.00, TOPPING));
        ingredients.add (new Ingredient("Beef", 2.75, TOPPING));
        ingredients.add (new Ingredient("Pepperoni", 2.50, TOPPING));
    }

this is the class that contains all of the ingredients and this is the class which I am trying to copy some of the ingredients to. 这是包含所有成分的课程,这是我尝试将某些成分复制到的课程。

import java.util.*;
import java.text.*;

public class Pizza {
    private LinkedList<Ingredient> ingredients = new LinkedList<Ingredient>();
    private int sold;

    public void add(){  
        ...
        ...
            if (...);
                ingredients.add(...);
        ...
    }

How should I go about adding SPECIFIC ingredients from Kitchen into ingredients in Pizza (not just clone the whole thing :))? 我应该如何将来自Kitchen的特殊食材添加到Pizza的食材中(而不仅仅是克隆整个东西:))? I've tried just adding the ingredient itself but that does not seem to work. 我试过只添加成分本身,但这似乎不起作用。 (inredients.add(ingredient)) (inredients.add(成分))

You can use enum instead of LinkedList to store all of the ingredients. 您可以使用enum代替LinkedList来存储所有成分。 Add a enum for ingredient in class Kitchen. 在“厨房”类中添加一个成分的枚举。

public enum Ingredient {
    THIN("Thin", 3.00, CRUST),
    THICK("Thick", 3.50, CRUST);
    // add other ingredient
    private String name;
    private double cost;
    private Category category;

    private Ingredient(String name, double cost, Category category) {
        this.name = name;
        this.cost = cost;
        this.category = category;
    }
}

Then in class Pizza. 然后在披萨课上。

public void add() { 
    ingredients.add(Ingredient.THICK);
    // add other ingredient needed
}

You can add a getIngredients() method to the Kitchen class which returns the provided ingredients by the Kitchen. 您可以向Kitchen类添加getIngredients()方法,该方法返回Kitchen提供的成分。 This allows you to access the ingredients of the Kitchen out of the Pizza class (and further classes). 这样,您就可以访问“ Pizza类(以及其他类)之外的“ Kitchen ”成分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM