-4

さて、私は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);
   } 
   }
4

4 に答える 4

2

あなたのコードのフォーマットはひどいので、これは単なる推測です。私はあなたが変わるべきだと思う

for (int i=0; i < 3; i++)
     {
System.out.println();
     }
    call.draw(Rectangle);
    call.draw(Circle);

for (int i=0; i < myShape.length; i++) {
    myShape[i].draw();
}

また、Shapeクラスチェンジで

public void draw()
{
}

public abstract void draw();
于 2013-04-23T18:35:06.650 に答える
1

draw() メソッドは、Caller クラスではなく、Shape クラスで定義されています。

myShape[0].draw() を使用して、たとえば長方形を印刷します。

于 2013-04-23T18:33:38.337 に答える
0

コードにはいくつかの問題があります。

1) 抽象クラスの draw() メソッドは抽象化する必要があります。または、私がさらに下に投稿したソリューションのように「部分的」として実装し、Shape クラスに抽象キーワードを付けずに実装することもできます。

2) new String("color") は必要ありません。文字列はイミュータブルなので、以下のように書けます。

3) クラス メンバーがプライベートか、パブリックか、または保護されているかを指定できません。変数の可視性について考えるのは常に良い考えです。

4) 半径ではなく直径を印刷しようとしています

5) インスタンスではなく、クラスでメソッドを呼び出そうとします。

これが私の提案です(テストされていません...私が見ていない問題が他にもあるかもしれません):

class Shape  
{  
  protected int width, height;  
  protected String color;

  public void draw()    
  {      
    System.out.print("I am a " + color");
  }
} // end Shape class

class Rectangle extends Shape
{
  Rectangle(int w, int h, String color)
  {
    this.width = w;
    this.height = h;
    this.color = color;
  }

  public void draw()
  {
    super();
    System.out.print(" Rectangle " + width + " wide and " + height + " high.\n");
  }
}// end Rectangle class

class Circle extends Shape
{
  Circle (int r, String color)
  {
    this.width = 2*r;
    this.height = 2*r;
    this.color = new String(color);   
  }
  public void draw()
  {
    super();
    System.out.print(" Circle with radius " + (width/2) + ".\n");
  }
} // end Circle class

後でメインメソッドで次のようなことをする必要があります:

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++)
{
  myShape[i].draw();
}
于 2013-04-23T18:56:48.520 に答える
0

forループ内では、実行中drawの特定のメソッドを呼び出す必要があり、別の空白行が必要でない限りShape呼び出す必要はありません。System.out.println()

for (int i=0; i < 3; i++)
{
    myShape[i].draw();
}

のような行を削除しますcall.drawcallメソッドの呼び出しには使用しません。Caller実際、オブジェクトのインスタンスさえ必要ありません。すでに持っているオブジェクトでdrawメソッドを呼び出すだけです。Shape

jlordoの答えのように、Shapeメソッドがない場合、クラスを抽象化する必要はありません。したがって、drawメソッドを作成するか、クラスからabstract削除できます。abstractShape

于 2013-04-23T18:34:19.090 に答える