用参数调用方法

时间:2014-12-05 00:47:29

标签: methods

class Robot extends Canvas
{
   public Robot()    //constructor method - sets up the class
   {
      setSize(800,600);
      setBackground(Color.WHITE);       
      setVisible(true);
   }

   public void paint( Graphics window )
   {
      window.setColor(Color.BLUE);
      window.drawString("Robot LAB ", 35, 35 );




      //call head method

      //call other methods

   }

   public void head( Graphics window )
   {
      window.setColor(Color.YELLOW);
      window.fillRect(300, 100, 200, 100);


        //add more code here

   }

我究竟如何调用head方法?

我已多次尝试,但我似乎无法得到它。

头(); window.head();对我没用?

对不起,我真的很陌生。

1 个答案:

答案 0 :(得分:0)

您需要使用

head(window);

这就是如何使用Java(以及大多数其他语言)中的参数调用方法,将参数作为逗号分隔列表放在方法名称后面的括号中。请注意,即使没有任何参数,parantheses仍然存在。

methodWithNoParameters();
someOtherObject.methodWithNoParameters();
this.methodWithOneParameter("foo");
SomeClass.staticMethodWithFiveParameters(1,two,3,4, "five");

参数的数量,顺序和类型必须与方法声明对其形式参数的数量,顺序和类型相匹配。在您的情况下,public void(Graphics window)表示只有一个。

最后,如果只从private内部调用它(而不是来自类本身外部),则应该使用此方法paint

相关问题