为什么C#和Java需要的一切是在一个班? [英] Why do C# and Java require everything to be in a class?

查看:117
本文介绍了为什么C#和Java需要的一切是在一个班?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是这个问题应该已经问过,但搜索一无所获。

我一直想知道什么点让我们把每一个代码位的类或接口内。我记得好象有一些优势,需要的main()之类的函数C,但没有上课。如Python语言是,在某种程度上,甚至更多的面向对象比Java,因为他们没有原语,但你可以把任何你想要的代码。

I've always wondered what's the point of making us put every bit of code inside a class or interface. I seem to remember that there were some advantages to requiring a main() function like C, but nothing for classes. Languages like Python are, in a way, even more object oriented than Java since they don't have primitives, but you can put code wherever you want.

这是一些排序OOP的误解吗?毕竟,你可以编写程序代码,就像在C会,并把它的类里面,但它不会是面向对象的。

Is this some sort of "misinterpretation" of OOP? After all, you can write procedural code like you would in C and put it inside a class, but it won't be object oriented.

推荐答案

我觉得需要,一切都包含在类的目标是为最小化的概念的数目,你需要处理的语言。在C#或Java,你只需要了解对象模型(这是相当复杂的,虽然)。但是,你只有用类(对象)的成员和实例类。

I think the goal of requiring that everything is enclosed in classes is to minimize the number of concepts that you need to deal with in the language. In C# or Java, you only need to understand the object-model (which is fairly complex, though). However, you only have classes with members and instances of classes (objects).

我想这是大多数语言的尝试以某种方式遵循一个非常重要的目标或其他。如果C#有一些全局代码(例如允许交互式评估和启动代码的规范,而不法),你有一个额外的概念学习(顶级码)。用C#/ Java的所作的选择当然只是一个,以获得简单方式。

I think this is a very important goal that most of the languages try to follow in one way or another. If C# had some global code (for example to allow interactive evaluation and specification of the startup code without Main method), you'd have one additional concept to learn (top-level code). The choice made by C#/Java is of course just one way to get the simplicity.

当然,这是一个问题,这是否是正确的选择。例如:

Of course, it is a question whether this is the right choice. For example:


  • 在函数式编程语言,程序正在使用结构化的类型的(类型声明)和表达式的。该计划的身体仅仅是一个计算的表达式,这比一类简单了很多方法,它也使交互式脚本(如在Python)。

  • In functional languages, programs are structured using types (type declarations) and expressions. The body of the program is simply an expression that is evaluated, which is a lot simpler than a class with Main method and it also enables interactive scripting (as in Python).

在二郎(以及类似的语言),程序的并发结构与启动其他进程一个主流程的执行进程。这是一个完全不同的方法,但它可以使某些类型的应用程序的良好意识。

In Erlang (and similar languages), program is structured as concurrently executing processes with one main process that starts other processes. This is a dramatically different approach, but it makes a good sense for some types of applications.

在一般情况下,每语言具有观察世界和建模它的一些方式,并使用这个角度看的一切的时。这种运作良好,在某些情况下,但我认为,没有一个模型完全普及。这可能是一个原因,今天的混合多范式语言都相当受欢迎。

In general, every language has some way of looking at the world and modelling it and uses this point of view when looking at everything. This works well in some scenarios, but I think that none of the models is fully universal. That may be a reason why languages that mix multiple paradigms are quite popular today.

作为一个侧面说明,我认为使用主要的方法有些值得商榷的选择(可能是从C / C ++语言的继承)。我会假设更加清晰的面向对象的解决办法是通过创造一些类的一个实例来启动程序。

As a side-note, I think that the use of Main method is somewhat arguable choice (probably inheriting from C/C++ languages). I would suppose that more clear object-oriented solution would be to start the program by creating an instance of some Main class.

这篇关于为什么C#和Java需要的一切是在一个班?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆