2017-05-19 2 views
-2

으로 변환 할 수 없습니다. java 클래스 어댑터가 있습니다.이 객체는 Groceries.java로 변환 할 수 없으므로 오류가 발생합니다 (Groceries b : getData()). B : GetData의은()) 나 메소드 b.getProduct()를 호출 할 수 getSn()를 Groceries.java에서오류 : 호환되지 않는 유형의 객체를 (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; 
} 
+2

'getData()'는 무엇을 반환합니까? 우리에게'getData()'의 코드를 보여줄 수 있습니까? –

+0

getData()는 목록에서입니다 – Rizal

답변

0

getData()은 Groceries 객체를 반환하지 않습니다. 그것에 대한 구현을 제공해 주시겠습니까, 제발. Java의 모든 객체는 Object.class에서 상속되므로 아무 문제없이 캐스팅 할 수 있습니다. Object.class에는 식료품 기능이 없으므로 호출하는 동안 오류가 발생합니다. 먼저 Java에서 OOP 및 OOP에 대한 좋은 책을 읽어야합니다.

편집 :

당신의 getData() 기능이 어떻게 보이는지 모르겠어요하지만 루프 작업에 대한 고급하려면이 같은해야한다 :

ArrayList<Groceries> myGroceries = new ArrayList<Groceries>(); 

public ArrayList<Groceries> getData(){ 
    return myGroceries; 
} 

그런 다음 루프해야 잘 지내라.

for (Groceries b : getData()) { 
    // Do stuff 
} 
+0

고마워 지금 작동하고, 나는 어댑터에 추가 – Rizal

관련 문제