-3
我有一個類和該類的對象數組'對象和每個對象都有自己的值。現在我想從另一個班級訪問該班級的所有詳細信息。請幫我解決這個問題。我必須在SecondClass中顯示FirstClass對象的細節(比如f [0] & f [1])。類的對象的數據將被存儲在哪裏?
public class FirstClass{
int first,second,total;
public FirstClass(int first,int second) {
this.first=first;
this.second=second;
this.total=first+second;
}
}
public class SecondClass {
public void display() {
//Display all the details of FirstClass
}
}
public class MainClass {
public static void main(String args[]) {
FirstClass f[]=new FirstClass[2];
f[0]=new FirstClass(2,4);
f[1]=new FirstClass(6,4);
SecondClass sec = new SecondClass();
sec.display();
}
}
最簡單的方法是將FirstClass數組作爲參數提供給sec.display或SecondClass的構造函數。你的標題似乎提出了另一個問題,但......保持一致可能嗎? – Jakumi
什麼Jakumi說,也許二類更有意義被稱爲FirstClassDisplayer;)但真的在這個簡單的例子,爲什麼不把這種顯示方法添加到第一類? –