什么是最好的构造函数

use*_*799 7 java oop constructor

考虑我们有两个类Line,Point哪个类使用类Point如下:

public class Point {
   private int x;
   private int y;

   public Point(int x, int y) {
      this.x = x;
      this.y = y;
   }
}
Run Code Online (Sandbox Code Playgroud)

public Class Line
{
   private Point start;
   private Point end;
   ...
}
Run Code Online (Sandbox Code Playgroud)

我想知道由于OOP原则,下面哪个构造函数更适合类Line?

public Line(Point start, Point end)
{
   this.start = start;
   this.end = end;
}
Run Code Online (Sandbox Code Playgroud)

要么

public Line(int startX, int startY, int endX, int endY)
{
   start = new Point(startX, startY);
   end = new Point(endX, endY);
}
Run Code Online (Sandbox Code Playgroud)

jra*_*jav 10

绝对是第一个.它更高级,意图更清晰,更简洁,并且对Point类的更改更易于维护.