Java 我的电脑有问题吗
有人能给我解释一下,当涉及到主方法project5类时,为什么我的编码会出现这些错误Java 我的电脑有问题吗,java,polymorphism,Java,Polymorphism,有人能给我解释一下,当涉及到主方法project5类时,为什么我的编码会出现这些错误thearray[count++]不断地给我错误,我不完全确定我是在哪里出错的。我已经在这上面呆了一段时间了。一些指导会很好 以下是我的主要方法: public class Project5 { private Shape [] thearray = new Shape[100]; public static void main (String [] args) { Project5 tpo = ne
thearray[count++]
不断地给我错误,我不完全确定我是在哪里出错的。我已经在这上面呆了一段时间了。一些指导会很好
以下是我的主要方法:
public class Project5 {
private Shape [] thearray = new Shape[100];
public static void main (String [] args) {
Project5 tpo = new Project5();
tpo.run();
}
public void run () {
int count = 0;
thearray[count++] = new Circle(20, 20, 40);
thearray[count++] = new Triangle(70, 70, 20, 30);
thearray[count++] = new Rectangle(150, 150, 40, 40);
for (int i = 0; i < count; i ++ ) {
thearray[i].display();
}
int offset = 0;
double totalarea = 0.0;
while (thearray[offset] != null) {
totalarea = totalarea + thearray[offset].area();
offset++;
}
System.out.println("The total area for " + offset + " Shape objects is " + totalarea);
}
}
这是我的形体课:
abstract class Shape{
int x = 1;
int y = 1;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Shape(int x, int y) {
this.x = x;
this.y = y;
}
public void display() {
}
public abstract double area();
}
我会发布我的三角形和矩形类,但我想这至少足以解释我把事情搞砸的地方。我查看了你的Circle类,构造函数目前只接受一个参数。但是,创建圆时指定了3个参数 圆应该从形状开始延伸,前两个参数是形状的x和y位置,对吗?如果是这种情况,请按如下方式更改您的类:
public class Circle extends Shape {
private double radius;
public Circle(int x, int y) {
super(x, y);
radius = 1.0;
}
public Circle(int x, int y, double newRadius) {
super(x, y);
setRadius(newRadius);
}
...etc
你得到了什么错误?你正在创建一个在构造函数中有3个参数的圆,但是在Circle类中,构造函数本身只接受一个参数Hi Enrique,其中的参数化构造函数在你的圆、矩形、三角形类中接受3或4个参数。我知道这听起来很愚蠢,但我在这方面还是相当新手。我到底要在哪里修好它,这样它才能正确阅读。只是想弄明白这一点你的代码甚至都不是在编译,也没有以3 int作为参数的循环构造函数,试着一步一步地解决你的问题。首先试着让你的Circle类完美地工作,然后将其他形状添加到你的主程序中。
public class Circle extends Shape {
private double radius;
public Circle(int x, int y) {
super(x, y);
radius = 1.0;
}
public Circle(int x, int y, double newRadius) {
super(x, y);
setRadius(newRadius);
}
...etc