在java中使用println打印对象(调用toString)时,将打印NULL,而不是我期望的输出
我在做一个涉及使用抽象类和方法的项目。其思想是我们有一般抽象类Shape和三个子类Circle、Triangle和Rectangle。我必须计算每个形状的面积,因此我在shape类中定义了一个抽象方法:calculateArea(),该方法返回形状的面积。每个子类重写此方法并分别计算其面积。我有一个测试类DrawingApp,它实例化了一个由10个随机形状组成的数组,并计算形状的总面积。现在我查看了我的代码,逻辑似乎是正确的。在DrawingApp类中,我使用for循环实例化了10个不同类型的Shape对象,并生成了一个介于1和3之间的随机数,如果该数为1,则我们实例化一个圆,反之亦然(2为矩形,3为三角形)。现在我认为这似乎是正确的,但在实例化10个随机对象之后,我想打印出每个不同形状的区域。因此,我使用for循环对创建的10个对象进行迭代,并调用:System.out.println(randomShapes[I])。现在我面临的问题是,当迭代10个形状时,它会打印null10次,而不是打印每个形状的区域。我检查了我的代码多次,但无法找出它为什么这样做。这是一个NullPointerException错误吗?我真的很困惑,任何帮助都会很有帮助。非常感谢。下面我还列出了以下类:DrawingApp.java、Shape.java和Circle.java。我没有列出其他类(矩形和三角形),因为它与Circle.java做的事情相同,唯一的区别是公式 DrawingApp.java在java中使用println打印对象(调用toString)时,将打印NULL,而不是我期望的输出,java,oop,nullpointerexception,null,abstract-class,Java,Oop,Nullpointerexception,Null,Abstract Class,我在做一个涉及使用抽象类和方法的项目。其思想是我们有一般抽象类Shape和三个子类Circle、Triangle和Rectangle。我必须计算每个形状的面积,因此我在shape类中定义了一个抽象方法:calculateArea(),该方法返回形状的面积。每个子类重写此方法并分别计算其面积。我有一个测试类DrawingApp,它实例化了一个由10个随机形状组成的数组,并计算形状的总面积。现在我查看了我的代码,逻辑似乎是正确的。在DrawingApp类中,我使用for循环实例化了10个不同类型的S
class DrawingApp {
public static void main(String[] args) {
Shape [] randomShapes = new Shape[10]; // an array of 10 random shapes: Circle, Triangle or Rectangle
// create 10 random shapes
for(int i = 0; i < randomShapes.length; i++) {
double randomNumber = Math.random() * (3 - 1 + 1) + 1; // generates a random between 1 and 3
// circle
if(randomNumber == 1) {
double circleRadius = Math.random() * (10 - 1 + 1) + 1; // generates a random between 1 and 10
randomShapes[i] = new Circle("Circle", circleRadius);
}
// rectangle
else if(randomNumber == 2) {
double length = Math.random() * (10 - 1 + 1) + 1; // generates a random between 1 and 10
double width = Math.random() * (10 - 1 + 1) + 1; // generates a random between 1 and 10
randomShapes[i] = new Rectangle("Rectangle", length, width);
}
// triangle
else if(randomNumber == 3) {
double height = Math.random() * (10 - 1 + 1) + 1; // generates a random between 1 and 10
double base = Math.random() * (10 - 1 + 1) + 1; // generates a random between 1 and 10
randomShapes[i] = new Triangle("Triangle", height, base);
}
}
// prints null instead of area of that shape
for(int i = 0; i < randomShapes.length; i++){
System.out.println(randomShapes[i]); // call toString and display shape name, id, and area
}
}
}
abstract class Shape {
protected static int id = 0; // id for each shape
protected String label; // the shape label
public Shape(String label) {
id++;
this.label = label;
}
public abstract double calculateArea(); // abstract method for calculating the area of a shape
// getters and setters
public static int getId() {
return id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
class Circle extends Shape {
private double radius;
public Circle(String label, double radius) {
super(label);
this.radius = radius;
}
@Override
public double calculateArea() {
double area = Math.PI * Math.pow(radius, 2);
return area;
}
@Override
public String toString() {
return "Id = " + super.getId() + ". Shape = " + super.getLabel() + ". Area = " + this.calculateArea();
}
}
Circle.java
class DrawingApp {
public static void main(String[] args) {
Shape [] randomShapes = new Shape[10]; // an array of 10 random shapes: Circle, Triangle or Rectangle
// create 10 random shapes
for(int i = 0; i < randomShapes.length; i++) {
double randomNumber = Math.random() * (3 - 1 + 1) + 1; // generates a random between 1 and 3
// circle
if(randomNumber == 1) {
double circleRadius = Math.random() * (10 - 1 + 1) + 1; // generates a random between 1 and 10
randomShapes[i] = new Circle("Circle", circleRadius);
}
// rectangle
else if(randomNumber == 2) {
double length = Math.random() * (10 - 1 + 1) + 1; // generates a random between 1 and 10
double width = Math.random() * (10 - 1 + 1) + 1; // generates a random between 1 and 10
randomShapes[i] = new Rectangle("Rectangle", length, width);
}
// triangle
else if(randomNumber == 3) {
double height = Math.random() * (10 - 1 + 1) + 1; // generates a random between 1 and 10
double base = Math.random() * (10 - 1 + 1) + 1; // generates a random between 1 and 10
randomShapes[i] = new Triangle("Triangle", height, base);
}
}
// prints null instead of area of that shape
for(int i = 0; i < randomShapes.length; i++){
System.out.println(randomShapes[i]); // call toString and display shape name, id, and area
}
}
}
abstract class Shape {
protected static int id = 0; // id for each shape
protected String label; // the shape label
public Shape(String label) {
id++;
this.label = label;
}
public abstract double calculateArea(); // abstract method for calculating the area of a shape
// getters and setters
public static int getId() {
return id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
class Circle extends Shape {
private double radius;
public Circle(String label, double radius) {
super(label);
this.radius = radius;
}
@Override
public double calculateArea() {
double area = Math.PI * Math.pow(radius, 2);
return area;
}
@Override
public String toString() {
return "Id = " + super.getId() + ". Shape = " + super.getLabel() + ". Area = " + this.calculateArea();
}
}
输出(免责声明:圆圈点不会被打印。它只是一个StackOverflow列表。
- 空的
- 空的
- 空的
- 空的
- 空的
- 空的
- 空的
- 空的
- 空的
- 空的
双随机数
与一个整数进行比较。很明显,双随机数类似于3.124519
,而不完全是3。将您的随机数设为int
。您是对的。非常感谢您指出这一点。它现在起作用了!!:)我猜:你是在把一个双随机数
和一个整数进行比较。显然,双精度将类似于3.124519
,而不完全是3。把你的随机数设为int
。你说得对。非常感谢你指出这一点。现在可以用了!!:)