简体   繁体   English

将 int 传递给另一个 class 包含的 class

[英]Passing an int to a class that is contained by another class

I'm very new to object orientated programming and was wondering if it's possible to pass an int to a class that is contained by another class.我对面向 object 的编程非常陌生,想知道是否可以将 int 传递给另一个 class 包含的 class。 My code compiles but when I run printList in my main the itemID is never printed.我的代码可以编译,但是当我在 main 中运行 printList 时,itemID 永远不会打印。

Here's my ArrayList class这是我的 ArrayList class

import java.util.ArrayList;
public class ItemList{
    int itemListID
    String itemName;
    ArrayList<Item> itemList = new ArrayList<>();
     
    public ItemList(String name, int listID) {
        itemName = name;
        itemListID = listID
    }
    public void addItem(String name, double weight, double price){
        itemList.add(new Item(name,weight,price));
    }
    public int getItemListID(){
        return itemListID;
    }
    public void printItems(){
        for(int i=0; i < itemList.size(); i++){
            System.out.println(itemList.get(i));
        }
    }
}

Here's the class of items contained within the arrayList这是 arrayList 中包含的项目的 class

public class Item {
    String name;
    double weight;
    double price;
    int itemListID;
  
    public Item(String itemName,double itemWeight, double itemPrice){
        this.name = itemName;
        this.weight = itemWeight;
        this.price = itemPrice;
    }
    public int setItemListID(){
        itemListID = ItemList.getItemListID();
    }
    
    public String toString(){
        return(name + " has a weight of " + weight + " and a price of " + price + "item is in itemlist"
        + itemListID);
    }    
}

Here's the main这是主要的

public class HopefulShopMain{
    public static void main (String[] args){
         ItemList myList = new ItemList("Fruit",1);
         
         myList.addItem("Apple", 100, 60);
         myList.addItem("Banana", 118, 50);
         
         myList.printItems();
    }
}

was wondering if it's possible to pass an int to a class that is contained by another class想知道是否可以将 int 传递给另一个 class 包含的 class

Yes.是的。 This can be done at various levels (constructor, or method), and in different ways.这可以在不同的级别(构造函数或方法)以不同的方式完成。

In your example, you could pass the itemListID into the Item's constructor:在您的示例中,您可以将 itemListID 传递给 Item 的构造函数:

    public void addItem(String name, double weight, double price){
        itemList.add(new Item(name,weight,price,this.itemListID));
    }

Then your Item class can keep that value in a field just like it does with the other values it's getting in its constructor.然后,您的 Item class 可以将该值保留在字段中,就像它在其构造函数中获取的其他值一样。

public class Item {
    String name;
    double weight;
    double price;
    int itemListID;
  
    public Item(String itemName,double itemWeight, double itemPrice, int itemListID){
        this.name = itemName;
        this.weight = itemWeight;
        this.price = itemPrice;
        this.itemListID = itemListID;
    }
    
    public String toString(){
        return(name + " has a weight of " + weight + " and a price of " + price + "item is in itemlist"
        + itemListID);
    }    
}

Alternatively, you could pass the entire ItemList into the constructor so it can refer to any value that ItemList has on it或者,您可以将整个ItemList传递给构造函数,以便它可以引用ItemList上的任何值

Or you can make Item a (non-static) nested class inside of ItemList , so it can simply reference the field directly.或者您可以在ItemList中将Item设为(非静态)嵌套 class ,因此它可以直接引用该字段。 This might be appropriate in your case, since it appears you only expect Item to be created and accessed by the ItemList class itself.这可能适合您的情况,因为您似乎只希望ItemList class 本身创建和访问Item

import java.util.ArrayList;
public class ItemList{
    int itemListID
    String itemName;
    ArrayList<Item> itemList = new ArrayList<>();
     
    public ItemList(String name, int listID) {
        itemName = name;
        itemListID = listID
    }
    public void addItem(String name, double weight, double price){
        itemList.add(new Item(name,weight,price));
    }
    public int getItemListID(){
        return itemListID;
    }
    public void printItems(){
        for(int i=0; i < itemList.size(); i++){
            System.out.println(itemList.get(i));
        }
    }
    class Item {
        String name;
        double weight;
        double price;
      
        public Item(String itemName,double itemWeight, double itemPrice){
            this.name = itemName;
            this.weight = itemWeight;
            this.price = itemPrice;
        }
        
        public String toString(){
            return(name + " has a weight of " + weight + " and a price of " + price + "item is in itemlist"
            + ItemList.this.itemListID);
        }    
    }
}

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

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