How to structure and organize nested classes
New to OOP, eager to learn good habits.
I want to make a vectorMap
class. A vectorMap
will have a few properties and contain a number of polyLine
objects, which in turn will each will have a few properties and consist of a number of xyPoint
objects.
The user will mostly interact with vectorMap
objects, but may occasionally want to use polyLine
and xyPoint
objects outside the context of vectorMap
.
Does this mean I should create three separate public classes? Would this mean three separate class modules in VBA, and in Java, three separate .java files?
My procedural gut tells me that it would be untidy to have three separate source code files for three small and simple classes with only a fe开发者_Go百科w lines of code each. I'm used to source code files containing packages with many functions. At this rate, a VBA project will contain tens of class modules. But maybe that's just the way it's done in OOP...
The above will be implemented in VBA and Java, so any examples in either/both of these are most welcome.
what do you mean "simple small classes"? My opinion is you should use a fresh file for each class which is testable. if (for instance) XyPoint is just a touple containing 2 elements, it will be a good idea to put it as a subclass of PolyLine.
However, as far as I see it - PolyLine and VectorMap should be in separate files, since you cannot really tell A is important only to B, and both are testable.
also, when using subclasses in java, notice their types (static/non-static,anonymous..) and choose wisely which is preferred.
p.s. a strong convention in Java is that class names start is a capital letters.
p.s.2: I assume this is done for educational purposes, otherwise you should (as @Ingo said) use built in classes, and not to reinvent the wheel...
精彩评论