我有java類適配器,這是錯誤的(Groceries b:getData()),因爲對象無法轉換爲Groceries.java,如果我改爲(Object b:的getData())我不能從Groceries.java調用一個方法b.getProduct()getSn()錯誤:不兼容的類型對象不能轉換爲(java類)
DataAdapter.java
public Groceries getBelBySN(String sn) {
Groceries pp = null;
for (Groceries b : getData()) {
if (b.getProduct().getSn().equals(sn)) {
pp = b;
break;
}
}
return pp;
}
public void updateTotal() {
long jumlah = 0;
for (Groceries b : getData()) {
jumlah = jumlah + (b.getProduct().getHarga() * b.getQuantity());
}
total = jumlah;
}
這是Groceries.java,我請適配器。
public class Groceries {
protected Product product;
protected int quantity;
public Groceries(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
public void setProduct(Product product) {
this.product = product;
}
public Product getProduct() {
return product;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getQuantity() {
return quantity;
}
getData()返回什麼?你能告訴我們'getData()'的代碼嗎? –
getData()是從列表 – Rizal