我遇到了麻烦。我想用测试程序绘制矩形和圆形。但我运行它,它没有打印任何值。我想知道我应该为每个形状设置值。我需要帮助。
这是图类
public abstract class Figure{
private int X, Y; // the center of the object.
private String name;
private static int numberOfShapes=0;
public Figure(){
X = 0;
Y = 0;
name = "none";
}
public Figure(int a, int b, String n){
setX(a);
setY(b);
setName(n);
numberOfShapes++;
}
public void setX(int a){X = a;}
public void setY(int b){Y = b;}
public void setName(String n){name = n;}
public int getX(){return X;}
public int getY(){return Y;}
public String getName(){return name;}
public static int getNumberOfShapes(){return numberOfShapes;}
public abstract void erase();
public abstract void draw();
public void center(){
System.out.println("\nIn Figure. Centering at ("+getX()+","+getY()+")");
}
这是我的矩形类
public class Rectangle extends Figure {
int length, height;
public Rectangle(){
super(0,0,"none");
//setLength(0);
//setHeight(0);
}
public Rectangle(String n,int a, int b, int h,int l){
super(a,b,n);
setLength(l);
setHeight(h);
}
public String toString() {
return "In Rectangle Drawing " + getName() + " centered at (" + getX() + "," + getY() + ") Length " + getLength() + " height " + getHeight();
}
public void erase(){
System.out.println("In Rectangle erasing");
}
public void draw(){
center();
erase();
System.out.println(""+this);
}
public void setLength(int l){length = l;}
public void setHeight(int h){height = h;}
public int getLength(){return length;}
public int getHeight(){return height;}
}
My Circle Class
public class Circle extends Figure {
private double Radius;
public Circle(){
super(0,0,"none");
}
public Circle(String n, int a, int b){
super(a,b,n);
}
public double getArea() {
return Math.PI * Radius * Radius;
}
public void setRadius(double r){
Radius = r;
}
public String toString(){
return "In Circle Drawing "+getName()+" centered at ("+getX()+","+getY()+") Area "+getArea();
}
public void erase(){
System.out.println("In Circle erasing");
}
public void draw(){
erase();
System.out.println(""+this);
}
public void setRadius(int r){Radius = r;}
public double getRadius(){return Radius;}
}
这是我的测试类
import java.util.*;
public class Testshape{
public static void main(String[] args){
//Scanner s = new Scanner(System.in);
Rectangle r = new Rectangle();
//Rectangle ry = new Rectangle();
System.out.println("In Rectangle erasing");
System.out.println(r);
}
}
我的输出格式就像这样:
答案 0 :(得分:0)
调用类的 toString()方法以获取输出。 你没有将任何参数传递给构造函数方法,所以我想知道这是否会编译。矩形的两侧是相同的,相对的宽度和长度相等。
public class Testshape{
public static void main(String[] args){
//Scanner s = new Scanner(System.in);
Rectangle r = new Rectangle("Rectangle",length, width);
System.out.println("In Rectangle erasing");
System.out.println(r.toString());
}
答案 1 :(得分:0)
猜猜你错过了对draw()
方法的调用。
import java.util.*;
public class Testshape{
public static void main(String[] args){
//Scanner s = new Scanner(System.in);
Rectangle r = new Rectangle();
//Rectangle ry = new Rectangle();
r.draw();
System.out.println("In Rectangle erasing");
System.out.println(r);
}
}
答案 2 :(得分:0)
我没有在你的功能上看到任何消息调用。
你可能会为动作调用函数,你只是实例化了类,而你的构造函数没有做任何打印。