-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が言ったことは、おそらくsecondclassはFirstClassDisplayerという意味になります;)しかし、この単純な例では、この表示メソッドを最初のクラスに追加してみませんか? –