Why do C# and Java require everything to be in a class?

前端 未结 3 1142
猫巷女王i
猫巷女王i 2020-12-29 04:16

It seemed like this question should have been asked before, but searching found nothing.

I\'ve always wondered what\'s the point of making us put every bit

3条回答
  •  时光说笑
    2020-12-29 04:34

    I think the idea with Java was that a top-level class would represent a single unit of code that would be compiled to a separate .class file. The idea was that these discrete, self-contained units of code could then be easily shared and combined into many projects (just as a carpenter can combine basic parts like nuts, bolts, pieces of wood, etc., to make a variety of items). The class was seen as the smallest, most basic atomic unit, and thus everything should be a part of a class to make it easier to assemble larger programs from these parts.

    One can argue that object-oriented programming's promise of easily composable code didn't work out very well, but back when Java was being designed, the goal of OOP was to create little units (classes) that could easily be combined to make unique programs.

    I imagine C# had some of the same goals in mind.

提交回复
热议问题