This site requires JavaScript, please enable it in your browser!
Greenfoot back
Entity1037
Entity1037 wrote ...

2013/7/15

Methods with optional values?

Entity1037 Entity1037

2013/7/15

#
I want to make a method where if you don't supply the last value, it assumes it to be false. The only problem is that I have no Idea how to do that! Does someone else know, or is it not even possible?
Zamoht Zamoht

2013/7/15

#
If I get your right (which I'm not sure I do) you can create a method called theTestMethod: public void theTestMethod(boolean trueOrFalse) { //Do stuff. } And then if you don't want to pass the boolean you create a new method: public void theTestMethod() { theTestMethod(false); } theTestMethod without an argument just calls the other theTestMethod with a false argument. Again I'm not sure this is what you are looking for, but if it's not could you please add an example of your method and explain what you want to do with it?
Entity1037 Entity1037

2013/7/15

#
OK that makes sense! I If I don't put anything there, it calls the method that doesn't have that value! Thank you! So I can just do this:
public void block(int x, int y, boolean movable){
        addObject(new Block(movable),x*20+20,getHeight()-y*20-20);
    }
    public void block(int x, int y){
        addObject(new Block(false),x*20+20,getHeight()-y*20-20);
    }
Zamoht Zamoht

2013/7/17

#
Sorry for my late answer. I guess you already found out but yes it is possible and it will work.
You need to login to post a reply.