2014-12-05 139 views
0
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();没有为我工作?

对不起,我对此很新。

+0

'head(window);'? – Thilo 2014-12-05 00:49:28

+0

谢谢!头(窗口);为我工作,但我可以知道你为什么把窗口放在括号内?括号内通常会出现什么内容? – prora 2014-12-05 00:51:24

回答

0

您需要使用

head(window); 

这是你刚才怎么调用一个方法在Java参数(和大多数其他语言),你把参数在后面的括号以逗号分隔的列表方法名称。请注意,甚至在没有任何参数的情况下,这些缺口仍然存在。

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

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

最后,如果您只从paint(而不是从课程本身之外)调用此方法,则应该使此方法为private