|
-
Java Super and Sub Classes
Ok so i've got a game, with class Player, and a few extensions of Player
i've greatly over simplified my code for ease of my typing.
Code:
class Player
public PlayerStuff;
class SmartPlayer extends Player
public SmartStuff;
class HumanPlayer extends Player;
Public HumanStuff;
then in my game class i'm trying to define an array of players that is a mix of players, smartplayers and humanplayers.
Code:
Player[NUM_OF_PLAYERS] PlayersInGame;
PlayersInGame[1] = new Player;
PlayersInGame[2] = new SmartPlayer;
and the problem arises that i cant access the methods in SmartPlayer even if instances of the array is pointed at a SmartPlayer... any ideas?
ACK I think i blew it up...
-
The reason for that is that the compiler has no way of guaranteeing that a reference in an array of Players has the methods within the SmartPlayer class. Since you could just as easily store a HumanPlayer there that does not have the method, the compiler forces you to make such a guarantee.
There are two ways around this.
1) I call this the wrong way, but sometimes you can't avoid it. You could force the instance to be a smart player with a cast, so something like:
Code:
((SmartPlayer)PlayersInGame).methodInSmartPlayer();
To be even safer, you could do an instanceof check, so that you never get a ClassCastException if you accidentally do that with the wrong class.
2) Only call methods that are guaranteed to be common among Player - if the method you are trying to call can be made abstract in Player and then implemented in both, then by all means take this route.
Current Rig: Q6600 @2.8GHz | Zalman CNPS 9700 | 6 GB Corsair XM2 DDR2 RAM @ 736 MHz | 2 x 150 GB Raptors Raid 0 | Asus Dark Knight 4870 512 MB | Gigabyte EP45-U3DL Motherboard | ASUS VW266H Panel | Lian Li PC-V1200Bplus
XBL Gamertag: Recursivity
http://xclite.net
Now Playing: Left4Dead, Team Fortress 2, Killing Floor, Dawn of War 2
-
can you typecast as things other then default types?
ACK I think i blew it up...
-
Yes indeed - and that TypeCast will actually work, provided what you are casting actually was instantiated as a SmartPlayer. If you tried the cast on a HumanPlayer, it would throw an exception during execution.
Current Rig: Q6600 @2.8GHz | Zalman CNPS 9700 | 6 GB Corsair XM2 DDR2 RAM @ 736 MHz | 2 x 150 GB Raptors Raid 0 | Asus Dark Knight 4870 512 MB | Gigabyte EP45-U3DL Motherboard | ASUS VW266H Panel | Lian Li PC-V1200Bplus
XBL Gamertag: Recursivity
http://xclite.net
Now Playing: Left4Dead, Team Fortress 2, Killing Floor, Dawn of War 2
-
ACK I think i blew it up...
-
Current Rig: Q6600 @2.8GHz | Zalman CNPS 9700 | 6 GB Corsair XM2 DDR2 RAM @ 736 MHz | 2 x 150 GB Raptors Raid 0 | Asus Dark Knight 4870 512 MB | Gigabyte EP45-U3DL Motherboard | ASUS VW266H Panel | Lian Li PC-V1200Bplus
XBL Gamertag: Recursivity
http://xclite.net
Now Playing: Left4Dead, Team Fortress 2, Killing Floor, Dawn of War 2
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|