简体   繁体   English

简单Java方法将无法运行。 方法内部方法

[英]Simple Java Method won't run. Method inside method

Here is my code: 这是我的代码:

public class Project1mrybak3
{   
    public static void main (String[] args)   
    {
        System.out.println("Begin Java Exection");  
        System.out.println("");

        // put your Java Program here

        //choose picture

        String  picfile = FileChooser.pickAFile();  
        Picture pic     = new Picture(picfile);

        //Create turtle 1   
        Turtle t1 = new Turtle (pic);   
        t1.setPenWidth(10);

        //Create turtle 2  
        Turtle t2 = new Turtle (pic);

        flower(t1,200);

        //show picture    
        pic.show();

        System.out.println("");     
        System.out.println("End Java Exection");
    }//end of main class


    public static void flower (Turtle tparam, int x )     
    {     
        tparam.forward(x);     
    }

    public static void flowers ()       
    {      
        flower(t1,15);    
    }
} // end of Template class

So as you can tell, it doesn't make much sense but I have just started writing it. 所以你可以说,它没有多大意义,但我刚开始写它。 My question is, for the last method flowers , when I try to run it, it says that it cannot find the symbol variable t1 . 我的问题是,对于最后一个方法flowers ,当我尝试运行它时,它说它找不到符号变量t1 If I take out the parameters, it says that it requires parameters Turtle and int . 如果我取出参数,它说它需要参数Turtleint Can I not put methods inside of methods? 我不能把方法放在方法里面吗?

Ultimately my goal is to create about 4 methods to draw parts of the flower, then put them all inside of one method, then inside my main code, I can call that method with turtle t1 and some x variable. 最终我的目标是创建大约4个方法来绘制花的部分,然后将它们全部放在一个方法中,然后在我的主代码中,我可以用turtle t1和一些x变量调用该方法。

Thank you for any help and your time! 感谢您的帮助和时间!

No, you can't put methods inside of methods. 不,你不能把方法放在方法里面。

If you need to use multiple methods and it doesn't make sense to pass your variables as parameters, you should make the variables you need into fields (or members) of a class (read that link it will help). 如果您需要使用多个方法并且将变量作为参数传递没有意义,那么您应该将所需的变量放入类的字段(或成员)中 (读取它将有用的链接)。 Move their declaration outside of the method they are local to (such as main ) and into the scope of the class itself. 将他们的声明移到他们本地的方法之外(例如main )并进入类本身的范围。 Then, all methods in that class can refer to those variables: 然后,该类中的所有方法都可以引用这些变量:

  • With this. 有了this. prefixed 前缀
  • Or just as they are, as long as there is no local variable with the same name. 或者就像它们一样,只要没有具有相同名称的局部变量。

Like NickC said, you can't put methods inside of methods. 就像NickC所说,你不能把方法放在方法里面。 Something else important to note here, though, is that t1 in your example isn't a method; 但是,这里需要注意的其他重要事项是,示例中的t1不是方法; it's an object ( Method objects from the reflection API being a little beyond the scope of this question...). 它是一个对象(来自反射API的Method对象有点超出了这个问题的范围......)。 I don't mean to presume your design, but it seems like you might want your various flower-drawing methods inside a separate Turtle class, and you can have another method inside that class that calls all the helper methods in turn, like so: 我并不是要设想你的设计,但似乎你可能想要在一个单独的Turtle类中使用各种绘图方法,并且你可以在该类中使用另一个方法依次调用所有辅助方法,如下所示:

public void drawFlower(int xPosition) {
    drawStem(xPosition);
    drawStamen(xPosition);
    drawPetals(xPosition);
    ...
}

That way, all of those helper methods have access to the Turtle that's (I think) doing the drawing (via the this keyword, as NickC pointed out), and they can return a modified xPosition if you want so that you can pass a different starting point into the next method. 这样,所有这些辅助方法都可以访问Turtle (我认为)正在进行绘图(通过this关键字,正如NickC指出的那样),如果你愿意,他们可以返回一个修改过的xPosition,这样你就可以传递一个不同的起点进入下一个方法。

您无法在方法中定义方法,但可以在方法内部调用方法 - 例如在具有可能不同参数的循环中重新运行方法,直到满足条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM