Methods 使用参数调用方法

Methods 使用参数调用方法,methods,Methods,我如何确切地称之为head方法 我试了好几次,但似乎都没能成功 头();window.head();没有为我工作过 对不起,我对这一点还不熟悉。您需要使用 class Robot extends Canvas { public Robot() //constructor method - sets up the class { setSize(800,600); setBackground(Color.WHITE); setVi

我如何确切地称之为head方法

我试了好几次,但似乎都没能成功

头();window.head();没有为我工作过

对不起,我对这一点还不熟悉。

您需要使用

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

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

head(window);
参数的数量、顺序和类型必须与方法声明的形式参数相匹配。在您的例子中,
public void(图形窗口)
表示只有一个

最后,如果只从内部调用此方法,则应将其设置为私有方法(而不是从类本身外部调用)?谢谢!头部(窗口);为我工作,但我能知道你为什么把窗户放在括号里吗?括号里通常是什么?
methodWithNoParameters();
someOtherObject.methodWithNoParameters();
this.methodWithOneParameter("foo");
SomeClass.staticMethodWithFiveParameters(1,two,3,4, "five");