theo t nghĩ, vấn đề ở chỗ cái hàm init()
t sửa code như sau:
Code:
class TrongSo
{
private int x;
private int w;
public TrongSo()
{
}
public TrongSo(int x,int w){
this.x=x;
this.w=w;
}
public void show()
{
System.out.println("x= "+this.x+", w= "+this.w);
}
}
public class Bai2 {
public static void main(String[] args) {
TrongSo []arr=new TrongSo[3];
for(int i=0;i<arr.length;i++)
{
arr[i]=new TrongSo(i,i+2);
}
for(int i=0;i<arr.length;i++)
{
arr[i].show();
}
System.out.println(".......");
}
} CT vẫn khởi tạo ra mảng arr[] 3 phần tử, và gán theo ý bạn
hồi mình học cũng đc dạy cái init này, nhưng đôi khi s.dụng thấy phức tạp quá, nên mình toán viết như vậy (trông nó giống C++
)