摘要class无法实例化
Abstract class cannot be instantiated
一年前我不得不创建这个积分程序,一年前它运行良好。现在我必须重新访问它并在编译并尝试 运行 它时我 运行 进入了一个抽象 class 无法实例化这一事实的错误。我在网上四处看看,发现使用 Java 进行的一些更新或排序已经使使用 PointClass point1 = new PointClass();
的方法不再有效并且会出错。
在尝试使用 driver 程序实例化 class 时,我还没有找到修复错误的答案。我还看到,为了现在使用抽象 class,必须存在子 class。问题是,由于程序的说明,我不能使用 subclass。只有 driver 和点 class.
程序非常简单,只需声明一些点并从抽象中调用它们class,以便打印到屏幕上。我需要一些帮助来找出更新后的方法,以便在没有实例化错误的情况下再次运行。
PointClass
public abstract class PointClass {
private int pointX;
private int pointY;
//set instance variables
public PointClass() { this.pointX = 10; this.pointY = 10; }
public PointClass(int x, int y){ this.pointX = x; this.pointY = y; }
//make getters and setters
public void setPointX(int x) { this.pointX = x; }
public void setPointY(int y) { this.pointY = y; }
public int getPointX() { return this.pointX; }
public int getPointY() { return this.pointY; }
//make string for format with driver
public String toString() { return "x = " + this.pointX + " y = " + this.pointY; }
}
Driver
public class PointTest {
public static void main(String[] args){
System.out.println();
PointClass point1 = new PointClass(); //set point1 as no argument
PointClass point2 = new PointClass(11, 24); // set point2 as argument with x and y
System.out.println("Point1: " + point1); //display point1 from toString method
System.out.println();
System.out.println("Point2: " + point2); //display point2 from toString method
System.out.println("---------------------");
}
}
最好的办法是删除 abstract
关键字。没有必要。 Point
没有抽象方法。
如果您出于某种原因不能这样做,您可以通过在每次实例化后添加大括号来创建内联匿名 classes:
PointClass point1 = new PointClass() { };
PointClass point2 = new PointClass(11, 24) { };
顺便说一下,您关于这曾经有效的说法是不正确的。从来不可能直接实例化一个抽象class。这实际上是关键字的 整点 ,以防止 class 被实例化。
一年前我不得不创建这个积分程序,一年前它运行良好。现在我必须重新访问它并在编译并尝试 运行 它时我 运行 进入了一个抽象 class 无法实例化这一事实的错误。我在网上四处看看,发现使用 Java 进行的一些更新或排序已经使使用 PointClass point1 = new PointClass();
的方法不再有效并且会出错。
在尝试使用 driver 程序实例化 class 时,我还没有找到修复错误的答案。我还看到,为了现在使用抽象 class,必须存在子 class。问题是,由于程序的说明,我不能使用 subclass。只有 driver 和点 class.
程序非常简单,只需声明一些点并从抽象中调用它们class,以便打印到屏幕上。我需要一些帮助来找出更新后的方法,以便在没有实例化错误的情况下再次运行。
PointClass
public abstract class PointClass {
private int pointX;
private int pointY;
//set instance variables
public PointClass() { this.pointX = 10; this.pointY = 10; }
public PointClass(int x, int y){ this.pointX = x; this.pointY = y; }
//make getters and setters
public void setPointX(int x) { this.pointX = x; }
public void setPointY(int y) { this.pointY = y; }
public int getPointX() { return this.pointX; }
public int getPointY() { return this.pointY; }
//make string for format with driver
public String toString() { return "x = " + this.pointX + " y = " + this.pointY; }
}
Driver
public class PointTest {
public static void main(String[] args){
System.out.println();
PointClass point1 = new PointClass(); //set point1 as no argument
PointClass point2 = new PointClass(11, 24); // set point2 as argument with x and y
System.out.println("Point1: " + point1); //display point1 from toString method
System.out.println();
System.out.println("Point2: " + point2); //display point2 from toString method
System.out.println("---------------------");
}
}
最好的办法是删除 abstract
关键字。没有必要。 Point
没有抽象方法。
如果您出于某种原因不能这样做,您可以通过在每次实例化后添加大括号来创建内联匿名 classes:
PointClass point1 = new PointClass() { };
PointClass point2 = new PointClass(11, 24) { };
顺便说一下,您关于这曾经有效的说法是不正确的。从来不可能直接实例化一个抽象class。这实际上是关键字的 整点 ,以防止 class 被实例化。