从 Java 中的另一个类获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7966970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:15:01  来源:igfitidea点击:

Getting data from another class in Java

javaclassgetter

提问by yoshifish

public class Item {

    /**
    * Instance variables for this class
    */

    private String itemName;
    private int itemQuantity;

    /**
    * Contructor for this class
    */

    public Item (String itemName, int itemQuantity) {
        this.itemName = itemName;
        this.itemQuantity = itemQuantity;
    }
    //setter and getter methods
    public String getItemName () { 
        return itemName;
    }
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public int getItemQuantity () {
        return itemQuantity;
    }
    public void setItemQuantity(int itemQuantity) {
        this.itemQuantity = itemQuantity;
    }
}

Ok..I already have the class for item. Now I have to write the CartItem class. The description that was given are as follows:

好的..我已经有了项目的课程。现在我必须编写 CartItem 类。给出的描述如下:

class CartItem{ 
/*
Objects of this class are used to hold items that the shopper purchases in the super market. 
There are two attributes in this class, an item (an object created from the Item class) and a quantity (the number of that item that the shopper purchases). You have to write these two attributes. Note that one of the two will have a user defined data type. 
*/

}


public class CartItem {
    private Item item; //item from the item class
    private int itemQuantity; //quantity how much shopper buys

    public CartItem(Item itemName, int itemQuantity) {
        this.getItem();
        this.getQuantity();
    }


    public Item getItem() {
        return item;
    }
    public void setItem(Item item) {
        this.item = item;
    }

    public int getQuantity() {
        return itemQuantity;
    }
    public void setQuantity(int quantity) {
        this.itemQuantity = itemQuantity;
    }
}

Just wondering if it's correct though.

只是想知道它是否正确。

回答by Jon Skeet

No, it's not correct. Look at your constructor:

不,这是不正确的。看看你的构造函数:

public CartItem(Item itemName, int itemQuantity) {
    this.getItem();
    this.getQuantity();
}

Here you're calling the gettersand completely ignoring the values the caller has passed in. I don't think you want to do that... think about what the constructor needs to do in order to populate the newly constructed object...

在这里,您正在调用getter并完全忽略调用者传入的值。我认为您不想这样做……想想构造函数需要做什么才能填充新构造的对象……

(You should also considermaking these classes immutable, but that's a slightly different matter.)

(您还应该考虑使这些类不可变,但这是一个稍微不同的问题。)

回答by Jigar Joshi

Few things.

一些事情。

1 Person may shop more than one Itemso have Listof Item
2 Constructor isn't correct, which should be

1人可能店多于一个Item这样具有ListItem
2构造是不正确的,这应该是

public CartItem(Item itemName, int itemQuantity) {
        this.item = itemName;
        this.itemQuantity = itemQuantity;

    }

回答by JB Nizet

No it's not.

不,这不对。

The constructor for CartItem just calls this.getItem()and this.getQuantity(). This will just call the methods, which will obviously return null, since the attributes are never initialized. It should be:

CartItem 的构造函数只调用this.getItem()this.getQuantity()。这只会调用方法,显然会返回 null,因为属性从未初始化。它应该是:

public CartItem(Item itemName, int itemQuantity) {
    this.item = itemName;
    this.itemQuantity = itemQUantity;
}

Another problem is that you add getters and setters for all the fields, without even knowing if those methods are necessary. Try to favor immutability, and only provide setters if they are absolutely necessary. I won't explain all the advantages of immutability, because it would be too early given what you already know. But a good rule of thumb is : don't add a method to a class if it's not used.

另一个问题是您为所有字段添加了 getter 和 setter,甚至不知道这些方法是否必要。尽量支持不变性,并且仅在绝对必要时才提供 setter。我不会解释不变性的所有优点,因为考虑到您已经知道的情况,现在还为时过早。但是一个很好的经验法则是:如果一个方法没有被使用,不要将它添加到一个类中。