java 如何从主方法打印空方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16176637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:06:56  来源:igfitidea点击:

How to print a void method from main method

javaabstract-classstatic

提问by Brit

Okay so I have three class

好的,所以我有三个班级

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

`` What I am trying to do is create a new class to produce the following outputs: I am a blue Rectangle 20 wide and 10 high. I am a red Circle with radius 30. I am a green Rectangle 25 wide and 25 high But I am having a problem calling the method draw();

`` 我想要做的是创建一个新类来产生以下输出:我是一个蓝色的矩形,宽 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);
   } 
   }

回答by jlordo

Your code formatting is horrible, so this is just a guess. I think you should change

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

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

to

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

Also, in the Shapeclass change

此外,在Shape班级变化

public void draw()
{
}

to

public abstract void draw();

回答by Davey Chu

Your draw() method is defined on your Shape classes, not on your Caller class.

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

myShape[0].draw() to print out the rectangle for example.

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

回答by rgettman

Inside your forloop you need to call the drawmethod for the specific Shapethat you're on and you don't need to call System.out.println()unless you want another blank line.

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

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

Remove the lines like call.draw. You don't use callto call a method. In fact you don't even need an instance of your Callerobject. Just call the drawmethod on the Shapeobjects you already have.

删除像call.draw. 你不习惯call调用一个方法。事实上,您甚至不需要Caller对象的实例。只需drawShape您已有的对象上调用该方法。

As in jlordo's answer, there is no need for the Shapeclass to be abstract if it has no methods. So you can either make the drawmethod abstract, or you can remove abstractfrom the Shapeclass.

正如 jlordo 的回答,Shape如果类没有方法,则该类不需要是抽象的。因此,您可以创建draw方法abstract,也可以abstractShape类中删除。

回答by luksch

There are several issues with your code:

您的代码有几个问题:

1) The draw() method in the abstract class should be made abstract. Or, you could implement it as "partial" like in the solution I post further down, then without the abstract keyword at the Shape class:

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

2) new String("color") is not needed. Strings are immutable, so you can just write as below.

2) 不需要 new String("color") 。字符串是不可变的,所以你可以写如下。

3) you miss specifying if your class members are private or public or protected. It is always a good idea to think about visibility of variables.

3)您错过了指定您的班级成员是私有的、公共的还是受保护的。考虑变量的可见性总是一个好主意。

4) You try to print the diameter not the radius

4)您尝试打印直径而不是半径

5) You try to call a method on the class and not on the instance

5) 您尝试在类上而不是在实例上调用方法

Here is my suggestion (untested... there might be more issues that i did not see):

这是我的建议(未经测试......可能还有更多我没有看到的问题):

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

later in the main method you need to do something like:

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