【问题标题】:How to send an Object through a constructor?如何通过构造函数发送对象?
【发布时间】:2013-06-02 23:41:12
【问题描述】:

我目前遇到问题,找不到方法。问题来了……

完成下面的 Point 类:

public class Point{
  private int x;
  private int y;
  public Point(int x, int y){
    this.x = x;
    this.y = y;
  }
}

这样下面的代码就会产生如下输出:

public class TestClass{
  public static void testEqual(Point p1, Point p2){
    if (p1.equals(p2)){
      System.out.println("The two points are equal\n"+p1);
    }else{
      System.out.println("The two points are not equal");
      System.out.println("First Point: "+ p1);
      System.out.println("Second Point: " + p2);
    }
  }
  public static void main(String [] args){
    Point p1 = new Point(2,3);
    Point p2 = Point.clonePoint(p1);
    Point p3 = new Point(1,1);
    Point p4 = new Point(2,3);
    testEqual(p1,p2);
    testEqual(p1,p3);
    testEqual(p1,p4);
    testEqual(p2,p4);
  }
}

输出

The two points are equal
The X Coordinate is 2 and the Y Coordinate is 3
The two points are not equal
First Point: The X Coordinate is 2 and the Y Coordinate is 3
Second Point: The X Coordinate is 1 and the Y Coordinate is 1
The two points are equal
The X Coordinate is 2 and the Y Coordinate is 3
The two points are equal
The X Coordinate is 2 and the Y Coordinate is 3

除了这行Point p2 = Point.clonePoint(p1);,我什么都能理解 我该如何解决?

【问题讨论】:

  • 您正在查看静态方法,而不是构造函数。
  • return new Point(p.x, p.y);
  • 解决什么? Point 类在级别类中有一个静态方法,该方法返回一个 Point
  • @nachokk "完成下面的课程"

标签: java object constructor


【解决方案1】:

我认为您的作业希望您像这样将cloning method 添加到Point 类中:

public class Point{
  private int x;
  private int y;
  public Point(int x, int y){
    this.x = x;
    this.y = y;
  }
  public static Point clonePoint(Point p){ return new Point(p.x,p.y); }
}

【讨论】:

  • 应该是静态方法。
  • @Aggieboy 发现 1 个错误:文件:C:\Users\Tushar\TestClass.java [行:13] 错误:无法从静态上下文引用非静态方法 clonePoint(Point)跨度>
  • 哇,我的坏笑已修复。我确定其他答案正是您想要的。
【解决方案2】:

这就是你所需要的:

[注意最后一个方法 'clone' 是一个静态(类)方法,您可以通过在类名 'Point' 后面加上 '.' 作为前缀来访问它]。

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 "The X Coordinate is " + x + " and the Y Coordinate is " + y;
    }

    @Override
    public boolean equals(Object otherPoint) {
        if (this == otherPoint) return true;
        if (otherPoint == null || getClass() != otherPoint.getClass()) return false;

        Point point = (Point) otherPoint;

        return x == point.x && y == point.y;
    }

    @Override
    public int hashCode() {
        int result = x;
        result = 31 * result + y;
        return result;
    }

    public static Point clonePoint(Point p) {
        return p != null ? new Point(p.x, p.y) : null;
    }
}

【讨论】:

    【解决方案3】:

    添加get 方法将点坐标返回到您的Point 类:

    public int getX()
    {
       return x;
    }
    
    public int getY()
    {
       return y;
    }
    

    然后将以下clonePoint() 静态方法添加到您的Point 类中:

    public static Point clonePoint(Point p)
    {
       return new Point(p.getX(), p.getY());
    }
    

    然后将以下equals() 方法添加到您的Point 类中:

    public boolean equals(Point p)
    {
       return (p.getX() == getX()) && (p.getY() == getY());
    }
    

    【讨论】:

    • 由于类可以访问其实例的私有成员,gets() 是不必要的。由于它们是公开的,因此它还可能会向其他类提供对 x 和 y 成员的不必要访问。
    • 允许公众访问坐标有什么危害? Point 类对其他任何类都是无用的。
    • 它们最初被定义为私有的,所以我认为这是有原因的。否则,它们应该首先被宣布为 public XD
    • 公开班级成员被认为是糟糕的设计。这就是为什么需要get 方法的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    相关资源
    最近更新 更多