在 main 方法中从 class 调用构造函数?
Calling a constructor from a class in the main method?
我已经阅读了其他一些问题,但似乎仍然无法弄清楚如何让我的问题发挥作用,感谢您的帮助。我到目前为止的代码如下。我希望能够调用 newPointParameters 来创建一个新的 class。
public class Lab4ex1 {
public static void main(String[] args) {
System.out.println("" + 100);
new newPointParameter(42,24);
}
class Point {
private double x = 1;
private double y = 1;
public double getx() {
return x;
}
public double gety() {
return y;
}
public void changePoint(double newx, double newy) {
x = newx;
y = newy;
}
public void newPointParameters(double x1, double y1) {
this.x = x1;
this.y = y1;
}
public void newPoint() {
this.x = 10;
this.y = 10;
}
public double distanceFrom(double x2, double y2) {
double x3 = x2 - this.x;
double y3 = y2 - this.y;
double sqaureadd = (y3 * y3) + (x3 * x3);
double distance = Math.sqrt(sqaureadd);
return distance;
}
}
}
应该是
public static void main(String[] args) {
System.out.println("" + 100);
Point p = new Point();
p.newPointParameter(42,24);
}
newPointParameters
不是构造函数。我认为这就是您想要做的:
public Point(double x1, double y1) {
x = x1;
y = y1;
}
然后您可以使用此构造函数在主 class 中创建一个 Point
对象:
Point p = new Point(42, 24);
您似乎还打算让 newPoint()
成为构造函数,因此它应该如下所示:
public Point() {
x = 10;
y = 10;
}
因此,目前,newPointParameters 和 newPoint 都不是构造函数。相反,它们只是方法。要使它们成为构造函数,它们需要与 class 构造函数
共享相同的名称
class Point {
private double x = 1;
private double y = 1;
public Point() {
this.x = 10;
this.y = 10;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
然后,当您想要创建一个新点时,只需执行以下操作
为默认点
public class Lab4ex1 {
public static void main(String[] args) {
System.out.println("" + 100);
//this will create a new Point object, and call the Point() constructor
Point point = new Point();
}
对于带参数的Point
public class Lab4ex1 {
public static void main(String[] args) {
System.out.println("" + 100);
//this will create a new Point object, and call the
//Point(double x, double y) constructor
Point point = new Point(10.0, 10.0);
}
我已经阅读了其他一些问题,但似乎仍然无法弄清楚如何让我的问题发挥作用,感谢您的帮助。我到目前为止的代码如下。我希望能够调用 newPointParameters 来创建一个新的 class。
public class Lab4ex1 {
public static void main(String[] args) {
System.out.println("" + 100);
new newPointParameter(42,24);
}
class Point {
private double x = 1;
private double y = 1;
public double getx() {
return x;
}
public double gety() {
return y;
}
public void changePoint(double newx, double newy) {
x = newx;
y = newy;
}
public void newPointParameters(double x1, double y1) {
this.x = x1;
this.y = y1;
}
public void newPoint() {
this.x = 10;
this.y = 10;
}
public double distanceFrom(double x2, double y2) {
double x3 = x2 - this.x;
double y3 = y2 - this.y;
double sqaureadd = (y3 * y3) + (x3 * x3);
double distance = Math.sqrt(sqaureadd);
return distance;
}
}
}
应该是
public static void main(String[] args) {
System.out.println("" + 100);
Point p = new Point();
p.newPointParameter(42,24);
}
newPointParameters
不是构造函数。我认为这就是您想要做的:
public Point(double x1, double y1) {
x = x1;
y = y1;
}
然后您可以使用此构造函数在主 class 中创建一个 Point
对象:
Point p = new Point(42, 24);
您似乎还打算让 newPoint()
成为构造函数,因此它应该如下所示:
public Point() {
x = 10;
y = 10;
}
因此,目前,newPointParameters 和 newPoint 都不是构造函数。相反,它们只是方法。要使它们成为构造函数,它们需要与 class 构造函数
共享相同的名称class Point {
private double x = 1;
private double y = 1;
public Point() {
this.x = 10;
this.y = 10;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
然后,当您想要创建一个新点时,只需执行以下操作
为默认点
public class Lab4ex1 {
public static void main(String[] args) {
System.out.println("" + 100);
//this will create a new Point object, and call the Point() constructor
Point point = new Point();
}
对于带参数的Point
public class Lab4ex1 {
public static void main(String[] args) {
System.out.println("" + 100);
//this will create a new Point object, and call the
//Point(double x, double y) constructor
Point point = new Point(10.0, 10.0);
}