好的,所以我有三节课
abstract class Shape
{
int width, height;
String color;
public void draw()
{
}
} // end Shape class
``
class Rectangle extends Shape
{
Rectangle(int w, int h, String color)
{
width = w;
height = h;
this.color = new String(color);
}
public void draw()
{
System.out.println("I am a " + color + " Rectangle " + width + " wide and " + height + " high.");
}
}// end Rectangle class
``
class Circle extends Shape
{
Circle (int r, String color)
{
width = 2*r;
height = 2*r;
this.color = new String(color);
}
public void draw()
{
System.out.println("I am a " + color + " Circle with radius " + width + ".");
}
} // end Circle class
`` 我要做的是创建一个新类来产生以下输出:我是一个蓝色 Rectangle 20 宽和 10 高。我是一个半径为 30 的红色圆圈。我是一个 25 宽和 25 高的绿色矩形 但我在调用方法 draw() 时遇到问题;
This is the main class:
public class Caller
{
public static void main(String args[])
{
Caller call= new Caller();
Shape[] myShape = new Shape[3];
myShape[0] = new Rectangle(20,10,"blue");
myShape[1] = new Circle(30, "red");
myShape[2] = new Rectangle(25,25, "green");
for (int i=0; i < 3; i++)
{
System.out.println();
}
call.draw(Rectangle);
call.draw(Circle);
}
}