【发布时间】:2013-04-23 18:31:41
【问题描述】:
好的,所以我有三节课
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 高的蓝色矩形。 我是一个半径为 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);
}
}
【问题讨论】:
-
关于,
"But I am having a problem calling the method draw();"-- 你有什么问题?如果我们能够很好地为您提供帮助,您将希望尽可能具体提出问题。 -
如果您弄清楚哪些成员变量是私有的、受保护的或公共的,也许会有所帮助。请注意,您没有在代码中指定这一点,并且 afaik Java 使用 protected 作为默认值。
-
如果你留意你的代码缩进会很好
标签: java abstract-class static