Computer Science Canada Java Design Question |
Author: | Flikerator [ Wed Aug 15, 2007 7:07 pm ] | ||||||||
Post subject: | Java Design Question | ||||||||
Alright so I'm working through the thought process of designing Java (I'm not actually making anything, just testing and tinkering right now). I have the main file (Stitches.java) with a units class (Unit.java) and an Infantry sublcass of Unit (seperate file, Infantry.java). In infantry is a nested class called "Horse" because someone infantry can have horses. If I created crossbowman they wouldn't be able to use horses. Thats the reason I didn't nest horses in the Unit superclass. So each subclass would nest horses optionally. Would I be able to create a separate file called Horses.java and import it as a nested class? There is also the instance where a unit would require a horse. In that case the subclass of Unit would be horses with a subclass of Lancers (Which you "can't" do explicitly in Java). My thought would be to just make horses forced with classes that require horses, but if there is another method I'm not aware of I'm all ears. Mind the spacing. Tabs and [space] change the look from what I have in my IDE.
Basically I'm wondering if I'm doing this "correctly" or if there are other ways (Better or worse, the more the better) to go about doing the same thing. This is for design ideas not an actual program using Infantry and Horses. Here are the programs I have;
|
Author: | Aziz [ Wed Aug 15, 2007 10:13 pm ] |
Post subject: | RE:Java Design Question |
Have a separate class, Horse. Each Unit can have zero or one horse(s). Inheritance defines an "is-a" relationship. Infantry "is a" Unit. Lancer is NOT a Horse. What's the relationship between Horses and Unit? Well, some units can have horses, some cannot. So we could have a subclass of unit, that is MountedUnit. Our units that have horses would extend that class. MountedUnit would include a member of type Horse. Just remember the Object Orientated principles of inheritance. |