【问题标题】:How to print a void method from main method如何从 main 方法打印 void 方法
【发布时间】: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


【解决方案1】:

您的代码格式很糟糕,所以这只是一个猜测。我认为你应该改变

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();

【讨论】:

  • 感谢各位的帮助。我知道我的代码不是那么专业,但我是新手。感谢 jlordo 和 rgettman。你们是最有帮助的
【解决方案2】:

您的 draw() 方法是在您的 Shape 类中定义的,而不是在您的 Caller 类中。

myShape[0].draw() 例如打印出矩形。

【讨论】:

    【解决方案3】:

    在您的for 循环中,您需要为您所在的特定Shape 调用draw 方法,并且您不需要调用System.out.println(),除非您想要另一个空行。

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

    删除像call.draw 这样的行。您不使用call 来调用方法。事实上,您甚至不需要 Caller 对象的实例。只需在您已有的 Shape 对象上调用 draw 方法即可。

    正如 jlordo 的回答,如果 Shape 类没有方法,则它不需要是抽象的。因此,您可以将draw 方法设为abstract,也可以从Shape 类中删除abstract

    【讨论】:

      【解决方案4】:

      您的代码有几个问题:

      1) 抽象类中的 draw() 方法应该是抽象的。或者,您可以将其实现为“部分”,就像我在下面发布的解决方案中一样,然后在 Shape 类中不使用 abstract 关键字:

      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
      

      稍后在 main 方法中,您需要执行以下操作:

      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();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-03
        • 2011-10-22
        • 1970-01-01
        相关资源
        最近更新 更多