开发者

setters and getters simple use?

Question the SECOND PART

Thanks for the great help so far.. OK I've made progress but something still isn't right. My SpaceWarz class is as follows:

public class SpaceWarz {
    private boolean deePad; //access to this is restricted
    public boolean getDeePad()
    {       return this.deePad;     }

    public void setDeePad(boolean value)
    {      this.deePad = value;     }
    }

and it is happily sharing values with my Render class where the bulk of my code resides:

SpaceWarz sw = new SpaceWarz();
public void LoadGameSettings(){
    sw.setDeePad(_dPad);    // send value to SpaceWarz class
    _dPad = sw.getDeePad(); // get value from SpaceWarz class
}

BUT values are NOT getting through from my onCreate method in the other class. The game data is loaded in the onCreate method and saved onDestroy so it's a problem if I can't then send that data on:

    SpaceWarz sw = new SpaceWarz(); 
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

// SET SHARED DATA 
            _dPad = true; // debug value to be removed.
            sw.setDeePad(_dPad);
// SET SHARED DATA          
                    }

Any ideas how to put this right?

<--snip-->

Question the FIRST PART

Need some very basic help with setters and getters. I'm trying to move data between a class containing my onCreate method and one containing my main code to load and save game settings when my a开发者_StackOverflow社区pp starts and stops. I was trying to use Bundles but It was suggested using setters and getters would be simpler.

I've created a 3rd class called SpaceWarz:

package com.clockworkrobot.spacewarz;

public class SpaceWarz {

    private boolean deePad; //access to this is restricted

    public boolean getDeePad()
    {       return this.deePad;     }

    public void setDeePad(boolean value)
    {      this.deePad = value;     }

   }

Am I setting that right and can anyone outline exactly how I set and get the value from my other classes as I'm not getting the syntax right causing a crash :(

Thanks for taking a look.


I don't fully understand the question but to use those setters and getters that you wrote, you will have to have a SpaceWarz object created somewhere in your code.

SpaceWarz sw = new SpaceWarz();

Then you can call its setter or getter method.

if (sw.getDeePad())
{
   //do something
}

sw.setDeePad(false);


Having a hard time understanding what you're trying to do. But getters/setters are as simple as they sound. Here's a small example of how it would work.

public class MainClass() {

    public static void main(String [] args) {

         SpaceWarz game = new SpaceWarz();
         game.setDeePee(true);

         if (game.getDeePee()) // if true
             game.setDeePee(false); // turn off
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜