关键字“类”在声明中 [英] Keyword "class" in declaration

查看:90
本文介绍了关键字“类”在声明中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在面试中遇到了一个相当简单的问题。

I faced a rather simple question in an interview.

为什么我们使用用于声明类的关键字?

Why do we use the class keyword for declaring classes?

推荐答案

简答:因为这是在C ++中完成的方式。 Java已经从C ++中采用了大部分语法 - 在我看来,这是一个明智的决定,因为它真的有助于绘制程序员,当它还是新的时候。

Short answer: because that's the way it's done in C++. Java has taken the bulk of its syntax from C++ - a wise decision, in my opinion, as it really helped with drawing programmers when it was still new.

现在,如果你的问题是为什么一个关键字根本就需要 - 也就是为什么编译器不能推断出声明类的位置 - 也许它可以,但是使用关键字可以带来好处

Now, if your question is why a keyword is needed at all - i.e. why can't the compiler just deduce where classes are declared - maybe it can, but using a keyword has the benefits of


  1. 更容易编译。

  2. 比隐式声明对人类更具可读性。

  3. 正如我上面所说 - 类似于C ++语法。

编辑:另外一件事 - 有些事情只是不能由编译器在Java语法中推断 - 例如,空类和空接口(在Java中都是合法的)之间的唯一区别是 / interface keyword。

one other things - some things simply cannot be deduced by the compiler in the Java syntax - for example, the only difference between an empty class and an empty interface (both legal in Java) is the class / interface keyword.

这篇关于关键字“类”在声明中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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