public class Point {主类:
private int x;
private int y;
public Point(int x, int y){
this.x=x;
this.y=y;
}
@Override
public String toString() {
return [x= ”+this.x+, y= ”+this.y+]”;
}
}
import java.util.ArrayList;Run:
public class Main {
public static void main(String[] args) {
ArrayList<项目>list = 新的 ArrayList<项目>()
{
private static final long serialVersionUID = 1L;
@Override
public String toString()
{
return super.toString();
}
};
list.add(新点 (2, 4));
list.add(新点 (1, 6));
list.add(新点 (5, 2));
list.add(新点 (3, 7));
list.add(新点 (8, 9));
System.out.println(list.toString());
}
}
[[x= 2, y= 4], [x= 1, y= 6], [x= 5, y= 2], [x= 3, y= 7], [x= 8, y= 9]]在此示例中,ArrayList 使用方法 toString() 在 Point 类中声明。如果我们删除方法 toString() 在 Point 类中,我们将采用以下格式:
[Point@4a5f634c, Point@3c7038b9, Point@6b9c18ae, Point@55187eb3, Point@3b26456a]
Please disable your ad blocker and refresh the window to use this website.