简体   繁体   English

Java 问题“无法实例化类型Point2D”

[英]Java problem "Cannot instantiate the type Point2D"

I'm trying to use the Point2D class in java and I simple cannot create an object.我正在尝试在 java 中使用 Point2D class,但我无法创建 object。

import java.awt.geom.Point2D;
import java.util.Scanner;

public class TestPoint2D {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter point1's x-, y-coordinates: ");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        System.out.println("Enter point2's x-, y-coordinates: ");
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();

        Point2D p1 = new Point2D(x1,y1);
        System.out.println("p1 is ")+ p1.toString();
        
    }

}

That is not how you instantiate a Point2D instance.这不是您实例化Point2D实例的方式。 I believe you want the Point2D.Double(double, double) constructor .我相信你想要Point2D.Double(double, double) constructor Also, you have a typo in your print .另外,您的print中有错字。 It should be something like它应该是这样的

Point2D p1 = new Point2D.Double(x1, y1);
System.out.println("p1 is " + p1);

Point2D is an abstract class present in the package java.awt.geom.Point2D . Point2D 是 package java.awt.geom.Point2D 中的抽象class For abstract classes, you cannot form a object [why?对于抽象类,您不能形成 object [为什么? ] You have to check your import statements that it is not importing the class from the above package and is trying to create the objects of class that you have defined. ] 您必须检查您的导入语句,它没有从上述 package 导入 class 并且正在尝试创建您定义的 class 的对象。

Apart from this, there is compilation error in your println statement as it is missing a ) bracket.除此之外,您的 println 语句中存在编译错误,因为它缺少 ) 括号。 This code does not show any compilation error.此代码不显示任何编译错误。

import java.util.Scanner;
// import java.awt.geom.Point2d // comment this out if it is present in import. 
public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter point1's x-, y-coordinates: ");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        System.out.println("Enter point2's x-, y-coordinates: ");
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();

        Point2D p1 = new Point2D(x1,y1);
        System.out.println("p1 is "+ p1.toString()); // compilator error : missing ) ; 
        
    }
}
class Point2D
{
    double x, y;

    public Point2D(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    } 
    
}

The Point2D class defines a point representing a location in (x,y) coordinate space. Point2D class 定义了一个点,表示 (x,y) 坐标空间中的位置。

This class is only the abstract superclass for all objects that store a 2D coordinate.这个 class 只是所有存储二维坐标的对象的抽象超类。 The actual storage representation of the coordinates is left to the subclass.坐标的实际存储表示留给子类。

These are known subclasses of Point2D: Point, Point2D.Double, Point2D.Float这些是 Point2D 的已知子类:Point、Point2D.Double、Point2D.Float

https://docs.oracle.com/javase/7/docs/api/java/awt/geom/Point2D.html https://docs.oracle.com/javase/7/docs/api/java/awt/geom/Point2D.html

        Point2D p1 = new Point2D().Double(x1,y1);
        System.out.println("p1 is ")+ p1.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM