Java中具有相同名称的Point类对象

时间:2015-09-28 06:17:53

标签: java

我正在研究一个班级项目。我们必须设置2 coordinate pairs的值,然后找到它们之间的长度。我在object中使用名为Point的program时遇到错误。它给我一个错误,说它是重复的method。我已附上我的class并标记了我的error

public class Point 
{
private double distance;
private double x1;
private double x2;
private double y1;
private double y2;

public Point(double x1) //ERROR
{
    this.x1 = x1;
}
public void setX1(double x1)
{
    this.x1 = x1;
}
public double getx1(double x1)
{
    return x1;
}
public Point(double x2) //ERROR
{
    this.x2 = x2;
}
public void setX2(double x2)
{
    this.x2 = x2;
}
public double getX2(double x2)
{
    return x2;
}
public Point(double y1) //ERROR
{
    this.y1 = y1;
}
public void setY1(double y1)
{
    this.y1 = y1;
}
public double getY1(double y1)
{
    return y1;
}
public Point(double y2) // ERROR
{
    this.y2 = y2;
}
public void setY2(double y2)
{
    this.y2 = y2;
}
public double getY2(double y2)
{
    return y2;
}
public double distance()
{
    return Math.sqrt((Math.pow(x2 - x1, 2.0) + (Math.pow(y2 - y1, 2))));
}

}

我做了类似于此的事情,我使用相同的名称并且它起作用,所以我不确定这里有什么问题。谢谢你的帮助。

编辑:我有类似设置的代码,但没有错误。那是为什么?

public class Car 
{
private String make;
private String model;
private int year;

public Car()
{

}
public Car(String make)
{
    this.make = make;
}
public void setMake(String carMake)
{
    this.make = carMake;
}
public String getMake()
{
    return make;
}
public void Car(String model)
{
    this.model = model;
}
public void setModel(String carModel)
{
    this.model = carModel;
}
public String getModel()
{
    return model;
}
public Car(int year)
{
    this.year = year;
}
public void setYear(int carYear)
{
    this.year = carYear;
}
public int getYear(int year)
{
    return year;
}
public boolean isAntique()
{
    if ((2015 - year) >= 45){
        return true;
    }
    if ((2015 - year) < 45){
        return false;
    }
    return false;
}
public String toString()
{
    return "Car Make: " + make + " Car Model: " + model + " Car Year: " + year + " Antique? " + isAntique();
}

}

7 个答案:

答案 0 :(得分:2)

public Point(double x1) //ERROR
{
    this.x1 = x1;
}

public Point(double x2) //ERROR
{
    this.x2 = x2;
}

让我们考虑这两个构造函数正常工作,编译器不会抛出任何错误。 现在,当调用构造函数时,

Point object=new Point(10.00)

你认为哪一点会被初始化? x1或x2?

为避免这些事情,您不能拥有两个具有相同签名的构造。

答案 1 :(得分:1)

public Point(double x1)public Point(double y2)是一回事。如果你写了new Point(1.0);,应该拨打哪一个?

构造函数和方法需要根据名称,参数类型或参数数量而有所不同。你很可能没有一个带有单个double值的构造函数,因为很难用一个坐标定义一个点。

我怀疑唯一(或至少是主要的)构造函数是一个需要4个double值的构造函数。

答案 2 :(得分:1)

除非参数类型不同,否则不能有多个具有相同名称的函数。这称为overloading。 (我想这就是为什么你可以回忆起过去这样做的原因。)

例如,您可以执行以下操作:

public Point(String s) {
    ...
}
public Point(int i) {
    ...
}
public Point(double f) {
    ...
}
public Point(int i, double f) {
    ...
}

答案 3 :(得分:0)

简而言之:在java类中不能有重复的构造函数,通过重复,我们指的是2个或更多具有相同参数类型和顺序的构造函数,在您的情况下,您有2个带有double参数的构造函数,这对于编译器来说是相同的方法

public Point(double x1) //first declared constructor with double argument
{
    this.x1 = x1;
}

public Point(double x2) //remove this constructor
{
    this.x2 = x2;
}

答案 4 :(得分:0)

特征

Java通过其名称和参数类型知道方法/构造函数(参数名称在编译时被擦除,它们主要是开发实用程序)。方法名称和参数类型(例如Point(double))的组合称为方法的签名在任何给定的课程中,您只能找到一次签名

为什么?

在这里,你在课堂上有Point(double)四次。

当您致电new Point(13.37)时,JVM无法知道您是在呼叫Point(double x1)Point(double x2)Point(double y1)还是Point(double y2)。因此禁止这样做,并在编译时截获,产生错误。

你能做什么

您应该检查构造函数以传递4个double值,或者根本不传递,并使用setter代替。

public Point() {
    // this is it, use setters to initialize the values
    // useful when using serialization
}

public Point(double x1, double x2, double y1, double y2) {
    this.x1 = x1;
    this.x2 = x2;
    this.y1 = y1;
    this.y2 = y2;
}

为什么Car有效?

让我们检查一下签名:

public Car()
public Car(String make)
public void Car(String model)

Car()Car(String)显然不同。

你还有一个方法Car(String)(object.Car(“sth”)) and a constructor Car(String)( new Car(“sth”)`)。这些是不一样的,但方法是不正确的(调用一个方法,因为它不会被建议,因为它可能会令人困惑,实际上让你困惑)。

答案 5 :(得分:0)

删除所有“公共点(...”方法

添加单一方法 与

public Point(double x1, double x2, double y1, double y2) {
    this.x1 = x1;
    this.x2 = x2;
    this.y1 = y1;
    this.y2 = y2;
}

答案 6 :(得分:0)

否则,相同的方法签名必须具有不同的参数类型,而不是参数名称。在运行时,JVM甚至没有注意到参数名称,它只是程序员跟踪事物的便捷方式。因此,你的构造函数存在四次,都是相同的(从Java的角度来看)。

此外,将参数命名为与您正在设置的字段不同的形式是一种很好的形式。例如,我通常在前面为参数添加一个p,这样你就不会忘记特定的y2或者你正在谈论的任何东西。