我是java的新手,並且正在嘗試理解面向對象的編程。Java,將對象傳遞給不同的類
我想存儲一個對象在數組中。但是,該數組與原始對象位於不同的類中。我得到一個編譯器錯誤(不兼容類型)需要(1類)找到(另一類)。
請看看我的代碼,讓我知道我做錯了什麼。
這是來自「集羣」類,並且此方法應該將類「FeatureVector」中的對象存儲到名爲「featureV」的數組中。
public boolean add (FeatureVector examples){
int currentPosition;
if (this.getSize()==this.featureV.length){
return false;
}
else
currentPosition=this.getSize();
featureV[currentPosition+1]=examples;
return true;
編輯:
這是羣集的方式被定義爲以及featureV:
class Cluster{
private Cluster [] featureV;
Cluster(int capacity){
Cluster [] featureV= new Cluster [capacity];
錯誤:
錯誤:不兼容的類型 需要:羣集 實測值:FeatureVector
如果它幫助,這裏是要求,這是我首先實現了輸出:
Cluster c;
c = new Cluster(10);
c.add(new FeatureVector("A", new double[] { 0.0, 0.0 }));
c.add(new FeatureVector("B", new double[] { 1.0, 1.0 }));
c.add(new FeatureVector("C", new double[] { 1.0, 0.0 }));
c.add(new FeatureVector("D", new double[] { 2.0, 0.0 }));
FeatureVector.setVerbose(true);
System.out.println(c);
以上語句將在控制檯上顯示如下:
Cluster: {A: {0.0, 0.0}, B: {1.0, 1.0}, C: {1.0, 0.0}, D: {2.0, 0.0}}
如果需要了解更多信息,請讓我知道。
非常感謝!這個論壇是一個啓示。 :)
如何聲明'featureV'? – zmbq
請發佈確切的錯誤,並指出此錯誤引用的源代碼中的位置。還顯示如何聲明類型,例如什麼是'featureV'? – nos
想一想你的對象模型。 Cluster對象包含一個Cluster對象數組沒有多大意義。什麼是集羣?* – EJP