无法正确设置构造函数
Cannot set up constructor correctly
这是我手头的任务:(使用 netbeans)
a.Does 不继承任何其他 class
b.Contains四个私有实例,都是“Point”类型,变量调用
point1
point2
point3
point4
c。
创建一个采用以下值的构造函数:
我。 double x1, double y1,double x2, double y2,double x3, double y3, double x4,double y4
二。构造函数应创建并设置四个 Point 实例变量
d.The class 应包含以下吸气剂:
我。 getPoint1( )
二. getPoint2( )
三. getPoint3( )
四. getPoint4( )
e.The class 应该包含一个名为 getCoordinatesAsString()
的方法,returns 格式化的 String "%s, %s, %s, %s\n", point1, point2, point3, point4
这是我目前的情况:
public class Quadrilateral {
private Point point1;
private Point point2;
private Point point3;
private Point point4;
public Quadrilateral(double x1, double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
point1 = x1,y1;
point2 = x2,y2;
point3 = x3,y3;
point4 = x4,y4;
}
public Point getPoint1()
{
return point1;
}
public Point getPoint2()
{
return point2;
}
public Point getPoint3()
{
return point3;
}
public Point getPoint4()
{
return point4;
}
public String getCoordinatesAsString()
{
return String.format("%s, %s, %s, %s\n", point1,point2,point3,point4);
}
public String toString()
{
return String.format("%s:\n%s", "Coordinates of Quadrilateral are", getCoordinatesAsString());
}
}
我不知道如何正确设置构造函数。我收到一条错误消息,提示类型不兼容,double cannot be converted to Point.
您没有正确构建积分。
尝试:
public Quadrilateral(double x1, double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
point1 = new Point(x1,y1);
point2 = new Point(x2,y2);
point3 = new Point(x3,y3);
point4 = new Point(x4,y4);
}
当您创建 class(一个对象)的新实例时,您会调用它的构造函数。
这是通过使用 new
关键字完成的。
Point
结构是一个 class,您希望创建它的一个实例。
您试图将其分配给 double
值,而您应该创建一个新的 Point
对象并将两个双精度值作为 parameters
传递给点 constructor
.
因此,在 Quadrilateral
构造函数中创建点的方式与创建 Quadrilateral
对象的方式相同,即使用 new
关键字!
您也可以将点发送到 Quadrilateral
构造函数。制作 Quadrilateral
构造函数的重载版本 -
public Quadrilateral(Point point1, Point point2, Point point3, Point point4)
{
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
this.point4 = point4;
}
然后从 Quadrilateral
的客户端你可以像这样构造一个 Quadrilateral
-
Quadrilateral aQuadrilateral = new Quadrilateral(new Point(x1, y1), new Point(x2, y2),new Point(x3, y3), new Point(x4, y4) );
将四边形class的构造函数更改为
point1 = new Point(x1,y1);
point2 = new Point(x2,y2);
point3 = new Point(x3,y3);
point4 = new Point(x4,y4);
你的 Point class 应该看起来像
Class Point {
private double x;
private double y;
public Point (double x, double y) {
this.x = x;
this.y = y;
}
public String getCoordiantes () {
return String.format("("+ %f +","+ %f +")" , this.x, this.y);
}
(...) //other methods
}
这是我手头的任务:(使用 netbeans)
a.Does 不继承任何其他 class
b.Contains四个私有实例,都是“Point”类型,变量调用
point1
point2
point3
point4
c。 创建一个采用以下值的构造函数:
我。 double x1, double y1,double x2, double y2,double x3, double y3, double x4,double y4
二。构造函数应创建并设置四个 Point 实例变量
d.The class 应包含以下吸气剂:
我。 getPoint1( )
二. getPoint2( )
三. getPoint3( )
四. getPoint4( )
e.The class 应该包含一个名为 getCoordinatesAsString()
的方法,returns 格式化的 String "%s, %s, %s, %s\n", point1, point2, point3, point4
这是我目前的情况:
public class Quadrilateral {
private Point point1;
private Point point2;
private Point point3;
private Point point4;
public Quadrilateral(double x1, double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
point1 = x1,y1;
point2 = x2,y2;
point3 = x3,y3;
point4 = x4,y4;
}
public Point getPoint1()
{
return point1;
}
public Point getPoint2()
{
return point2;
}
public Point getPoint3()
{
return point3;
}
public Point getPoint4()
{
return point4;
}
public String getCoordinatesAsString()
{
return String.format("%s, %s, %s, %s\n", point1,point2,point3,point4);
}
public String toString()
{
return String.format("%s:\n%s", "Coordinates of Quadrilateral are", getCoordinatesAsString());
}
}
我不知道如何正确设置构造函数。我收到一条错误消息,提示类型不兼容,double cannot be converted to Point.
您没有正确构建积分。
尝试:
public Quadrilateral(double x1, double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
point1 = new Point(x1,y1);
point2 = new Point(x2,y2);
point3 = new Point(x3,y3);
point4 = new Point(x4,y4);
}
当您创建 class(一个对象)的新实例时,您会调用它的构造函数。
这是通过使用 new
关键字完成的。
Point
结构是一个 class,您希望创建它的一个实例。
您试图将其分配给 double
值,而您应该创建一个新的 Point
对象并将两个双精度值作为 parameters
传递给点 constructor
.
因此,在 Quadrilateral
构造函数中创建点的方式与创建 Quadrilateral
对象的方式相同,即使用 new
关键字!
您也可以将点发送到 Quadrilateral
构造函数。制作 Quadrilateral
构造函数的重载版本 -
public Quadrilateral(Point point1, Point point2, Point point3, Point point4)
{
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
this.point4 = point4;
}
然后从 Quadrilateral
的客户端你可以像这样构造一个 Quadrilateral
-
Quadrilateral aQuadrilateral = new Quadrilateral(new Point(x1, y1), new Point(x2, y2),new Point(x3, y3), new Point(x4, y4) );
将四边形class的构造函数更改为
point1 = new Point(x1,y1);
point2 = new Point(x2,y2);
point3 = new Point(x3,y3);
point4 = new Point(x4,y4);
你的 Point class 应该看起来像
Class Point {
private double x;
private double y;
public Point (double x, double y) {
this.x = x;
this.y = y;
}
public String getCoordiantes () {
return String.format("("+ %f +","+ %f +")" , this.x, this.y);
}
(...) //other methods
}