Computer Science Canada calling classes |
Author: | Clayton [ Tue May 30, 2006 9:09 am ] |
Post subject: | calling classes |
is it possible to call a class from within a class? for example, i have a game where weapons have stats, but these stats are needed for another class for a character that the user uses, so im just wondering if i can call that class from within my Character class or if i have to do it outside the class (obviously inside would be easier as it would allow me to have many instances of the class) |
Author: | Delos [ Tue May 30, 2006 10:37 am ] | ||
Post subject: | |||
Do you mean calling a method of a class? Something like:
That code is untest! I don't have access to Turing right now, so it's likely buggy here and there...but yes, it is possible to insantiate an object of a Class within another class. I wouldn't be surprised if you could instantiate an object of that same class within a class...though for what reasons, I'm not too sure... |
Author: | Clayton [ Tue May 30, 2006 10:45 am ] |
Post subject: | |
ok thanks thats pretty much exactly what i wanted to know ![]() |
Author: | Cervantes [ Tue May 30, 2006 3:16 pm ] |
Post subject: | |
Delos wrote: I wouldn't be surprised if you could instantiate an object of that same class within a class...though for what reasons, I'm not too sure...
Any sort of structure that links to others like it. A node is the classic example, for the use of creating a linked list. Another example: Say you wanted to make a grid (for something like Minesweeper). You could do this with a 2D array, yes. However, you could also do it with a Cell object that stores an array of neighbour Cells. This approach would be harder to initialize the grid, but it makes working with the grid much easier. SuperFreak82: You really need to work on your terminology. If not for Delos' post, I would have no clue what you're asking. You don't "call a class". Heck, you don't even call a method of that class. You call a method of an instance of that class. |