さて、私は3つのクラスを持っています
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
私がやろうとしているのは、次の出力を生成する新しいクラスを作成することです: 私は、幅 20、高さ 10 の青い Rectangle です。私は半径 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);
}
}