【发布时间】:2010-09-21 03:12:15
【问题描述】:
所以,我有这门课:
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; }
在另一个班级我有这个:
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(??));
}
}
如果这些数据是用户而不是我生成的,我会使用getID() 方法,对吗?
那么在我的班级发票中,如何使用方法getID(),以便我可以在参数中使用它作为我在HashMap中的键值?还有没有办法在不创建新类的情况下将 3 个值(名称信息 quan)添加到哈希图中?
【问题讨论】:
标签: java class methods hashmap