Sunday, 24 April 2011

Class confusion - part 2

I've decided to do some experiments with classes to see if they behave how I think they do.

First I want to see I can call a method in a different class. I was able to do it, and here's the code, below:

The Program class

The monster class.


Then I made a second method in the monster class to see if I could call either of them when I wanted to, which I was able to. Also, typing the class name brings up a list of its methods (usefully).




This makes an error.
Then I tried creating variables inside of the new class. I tried to do it outside of a method initially, but this generates errors. Is it possible to have a main method for a new class? What would be the purpose of it?





Next I created 2 new 'monsters' in the Program class, monster1 and monster2. I made the Program class define their HP and names from monster class.

The Program class.
The monster class

The question is, now what do I do? Where can the variables 'monsterHP' and 'monsterName' be used? Can they be used outside of the monster class? If so, how do I refer to them? I'm guessing the 'static' part means the variables can't be changed outside of that method, so I need to take 'static' out. Taking it out produces this error:

Well what do I have to type in, to replace 'static'? It's asking for an object reference and I don't even know what an object is yet.

excerpt code from the tutorial
Annoyingly, in the tutorials I'm following the guy seems to get away without replacing the word static. Sometimes he doesn't write 'public' either (which I guess is actually optional).

Also he's able to create variables outside of a method, which generated an error for me.

No comments:

Post a Comment