我是一个仍然在java的新手,你能告诉我这两个构造函数之间的区别吗?
第一:
public class Plan
{
ArrayList<Point2D> points;
public Plan(ArrayList<Ponto2D> points)
{
this.points = new Arraylist<Point2D>(points);
}
}
Run Code Online (Sandbox Code Playgroud)
这个:第二个:
public class Plan
{
public Plan(ArrayList<Point2D> lpoints)
{
points = new ArrayList<Point2D>();
for(Point2D p : lpoints) point.add(p.clone());
}
}
Run Code Online (Sandbox Code Playgroud)
小智 1
在第一种情况下,参数 ArrayList 共享相同的点(p1.equals(p2) 将为 true,p1 == p2 为 true)在第二种情况下,它们具有不同的点副本(p1.equals(p2) 将为为真,但 p1 == p2 为假)