What do people use class loading for?

前端 未结 18 1520
暗喜
暗喜 2021-02-02 14:26

So, every Java text book talks about how flexible Java is since it can load classes at run time. Just cobble together a string and give it to Class.forName(), and

18条回答
  •  佛祖请我去吃肉
    2021-02-02 15:12

    "PLUGIN" and that is the big word.

    Basically, you can load a class that you do not know when or does not exist when you write and compile your program.

    For example, if you want a program to do spell check, you can write an interface SpellChecker then load a class from a configuration file that implement the SpellChecker interface. After that, you can write any SpellChecker and set in the configuration file the actual file name. This way, your program does not need to know what class will do the spell checking.

    DB driver, Eclipse's plugin, Script language, Cryptography methods are all done this way as the original writer does not know (and in some case, has no idea) what class will actually be used.

    Hope this helps.

提交回复
热议问题