简体   繁体   中英

Java classification - what is the best approach

I was wondering what is the best approach to make the following classification in java. For example we have a simple app with the following classification:

  1. Air
    -----MotorType
    -----------PlaneObject

    -----NonMotorType
    -----------Deltaplane
  2. Earth
    -----MotorType
    -----------CarOboject

    ----NonMotorType
    ------------Bycicle

my initial idea was to make motor and nonMotor abstract classes and then extend them with the rest of the classes and then make water, air and earth of type enumeration, but I'm not sure if that's the best approach. I'll have ID, model and type(depending on the classification) fields and later will want to search by ID. Any ideas will be highly appreciated :) Thanks!

Maybe you would like to have a Vehicle abstract class. This class can has an attribute of the type Motor , which can be abstract and then extended in the sub-classes depending on your desired design.

This tutorial can help you to determine when you need an abstract class.

Hope it helps.

You can also use Interfaces for earth, water and air. So you can get the main stuff by your Interface.

Another way is to check the inheritance between the classes. So you can outsource the same things to the parent class and extend this step by step

I think a enumeration for the specification earth, water and air is also a god idea, becouse a lot of other variables and functions would be the same for a vehicle on earth, water or air.

If i would develop this stuf, i would do an uml concept.

  • Motor (Maybe Interface)
    • Earth
    • Water
    • Air
  • Non Motor
    • Earth
    • Water
    • Air

In order to maintain versatility, I would make Air and Earth interfaces and MotorType and NonMotorType abstract classes, both extending a VehicleType superclass.

A class like Bicycle would extend NonMotorType and implement Earth.

A class like AmphibianCar would extend MotorType and implement both Earth and Water.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM