gpt4 book ai didi

java - Java中同名的点类对象

转载 作者:行者123 更新时间:2023-12-01 18:11:17 25 4
gpt4 key购买 nike

我正在做一个类(class)项目。我们必须设置 2 个坐标对的值,然后找到它们之间的长度。我在整个程序中使用名为Point的对象时遇到错误。它给我一个错误,说它是一个重复的方法。我已附加我的并标记了我的错误

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();
}

}

最佳答案

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

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

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

点对象=new Point(10.00)

您认为哪个点会被初始化? x1 或 x2?

为了避免这些事情你不能有两个具有相同签名的构造函数。

关于java - Java中同名的点类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32816749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com