如何从Java中的另一个类调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3757047/
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
How to call a method from another class in Java
提问by Hyman
So, I have this class:
所以,我有这门课:
public class Product {
private String name, id, info ;
private int quantity;
public Product(String newName, String newID, String newInfo, Integer newQuantity){
setName(newName);
setID(newID);
setPrice(newInfo);
setQuantity(newQuantity);}
public void setName(String name) {
this.name = name; }
public void setID(String id) {
this.id = id; }
public void setPrice(String info) {
this.info = info; }
public void setQuantity(Integer quantity) {
this.quantity = quantity; }
public String getID( ) {
return id; }
public String getName( ) {
return name; }
public String getInfo( ) {
return info; }
public int getQuantity( ) {
return quantity; }
In another class i have this:
在另一堂课上,我有这个:
public class Invoice implements Group<Product> {
private HashMap<String, Product> prod = new HashMap<String, Product>( );
public Invoice(){ }
public void addProd(Product a) {
prod.put(??getID()??,new Product(??));
}
}
If this data was user generated rather than me, I would use the getID()
method right?
So in my class invoice, how do i use the method getID()
, so that I can use it in the parameter for my key value in the HashMap? Also is there a way to add 3 values (name info quan) to the hashmap without making a new class?
如果这些数据是用户生成的而不是我生成的,我会使用该getID()
方法吗?那么在我的类发票中,我如何使用该方法getID()
,以便我可以在 HashMap 中我的键值的参数中使用它?还有一种方法可以在不创建新类的情况下向哈希映射添加 3 个值(名称信息权)?
采纳答案by YoK
I see that you get Product
object with ref "a
" as parameter to your addProd
method.
我看到你得到了Product
带有 ref " a
" 的对象作为你addProd
方法的参数。
And you can get id by just using a.getID(). It should look as:
你可以通过使用 a.getID() 来获取 id。它应该如下所示:
public void addProd(Product a) {
prod.put(a.getID(),a);
}
I didn't understand second part of your question.. I think you already have 3 values in your Product object and you put Product object to Map, So why do you require another way ?
我不明白你问题的第二部分。我认为你的 Product 对象中已经有 3 个值,并且你将 Product 对象放到 Map 中,那么为什么你需要另一种方式呢?
回答by Synesso
Your class Product does not compile, because you have the name Item in your constructor. The constructor name must match the class name. So change that to Product. The same applies to Invoice vs ShoppingCart. Constructor and Class names must match.
您的 Product 类无法编译,因为您的构造函数中有名称 Item。构造函数名称必须与类名称匹配。所以把它改成产品。这同样适用于发票与购物车。构造函数和类名称必须匹配。
As per your comment, you'd like to add four product values to a Map. The key being one of the values of the product itself. Try this:
根据您的评论,您想向地图添加四个产品值。关键是产品本身的价值之一。尝试这个:
Product p = new Product(name, id, info, quantity);
cart.addProd(p);
...
public void addProd(Product p) {
prod.put(p.getId(), p);
}
Maps can only map a single value to a single key, so you must have some sort of container for the values you wish to collate into one value. This can be an object (Product) or you could use a collection (e.g. List). I strongly recommend the former.
Maps 只能将单个值映射到单个键,因此您必须为要整理成一个值的值提供某种容器。这可以是一个对象(产品),也可以使用一个集合(例如列表)。我强烈推荐前者。
回答by Glide
For your question about putting 3 values in your map, I don't think there's a way for you to put 3 values into one key without creating a class. An alternative is to store a Map<String, List<String>>
assuming your 3 values are type String, or, Map<String, Map<String, String>>
.
对于您关于在地图中放置 3 个值的问题,我认为您没有办法在不创建类的情况下将 3 个值放入一个键中。另一种方法是存储 aMap<String, List<String>>
假设您的 3 个值是类型 String, or, Map<String, Map<String, String>>
。