为什么MyClass.class存在于java和MyField.field中不存在? [英] why MyClass.class exists in java and MyField.field isn't?

查看:143
本文介绍了为什么MyClass.class存在于java和MyField.field中不存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有:

class A {
    Integer b;
    void c() {}
}

为什么Java有这种语法: A.class ,并且没有这样的语法: b.field c.method

Why does Java have this syntax: A.class, and doesn't have a syntax like this: b.field, c.method?

对于类文字有什么用处吗?

Is there any use that is so common for class literals?

推荐答案

A.class 语法看起来像字段访问,但事实上它是在上下文中的特殊语法规则的结果,其中不允许正常的字段访问;即 A 是一个类名。

The A.class syntax looks like a field access, but in fact it is a result of a special syntax rule in a context where normal field access is simply not allowed; i.e. where A is a class name.

以下是JLS中语法的含义:

Here is what the grammar in the JLS says:

Primary:
  ParExpression
        NonWildcardTypeArguments (
            ExplicitGenericInvocationSuffix | this Arguments)
  this [Arguments]
  super SuperSuffix
  Literal
  new Creator
  Identifier { . Identifier }[ IdentifierSuffix]
  BasicType {[]} .class
  void.class

请注意,字段方法没有等效语法。

Note that there is no equivalent syntax for field or method.

(旁白:语法允许 b.field ,但JLS声明 b .field 表示名为field的字段的内容...如果不存在这样的字段则是编译错误。同上 c.method ,此外还必须存在字段 c 。所以这些结构都不代表你想要的意思......)

(Aside: The grammar allows b.field, but the JLS states that b.field means the contents of a field named "field" ... and it is a compilation error if no such field exists. Ditto for c.method, with the addition that a field c must exist. So neither of these constructs mean what you want them to mean ... )

为什么存在此限制?嗯,我想因为Java语言设计者没有看到需要混淆语言语法/语义来支持方便地访问Field和Method对象。 (请参阅下面的 * 以了解更改Java以解决所需问题的一些问题。)

Why does this limitation exist? Well, I guess because the Java language designers did not see the need to clutter up the language syntax / semantics to support convenient access to the Field and Method objects. (See * below for some of the problems of changing Java to allow what you want.)

Java反射的设计并不容易使用。在Java中,最好在可能的情况下使用静态类型。它更有效,更不易碎。将反射的使用限制在静态输入根本不起作用的少数情况。

Java reflection is not designed to be easy to use. In Java, it is best practice use static typing where possible. It is more efficient, and less fragile. Limit your use of reflection to the few cases where static typing simply won't work.

如果你习惯于编程到一切都是动态的语言,这可能会让你感到烦恼。但你最好还是不打它。

This may irk you if you are used to programming to a language where everything is dynamic. But you are better off not fighting it.


对于类文字有什么用处吗?

Is there any use that is so common for class literals?

我想,他们支持这个类的主要原因是它避免了程序调用 Class.forName(一些可怕的字符串)每次你需要反思性地做某事。你可以把它称为一种折衷/小让步可用性为反射

I guess, the main reason they supported this for classes is that it avoids programs calling Class.forName("some horrible string") each time you need to do something reflectively. You could call it a compromise / small concession to usability for reflection.

我想另一个原因是,在<类型>的.class 语法没有破坏任何东西,因为 class 已经是一个关键字。 (IIRC,Java 1.1中添加了语法。)

I guess the other reason is that the <type>.class syntax didn't break anything, because class was already a keyword. (IIRC, the syntax was added in Java 1.1.)

*如果语言设计者试图改进支持对于这种事情会有各种各样的问题:

* If the language designers tried to retrofit support for this kind of thing there would be all sorts of problems:


  • 这些变化会在语言中引入含糊之处,使编译和其他解析器 - 依赖任务更难。

  • 无论是否方法字段,这些更改无疑会破坏现有代码
  • code>被转换为关键字。
  • 您不能将 b.field 视为隐式对象属性,因为它不会适用于物体。而 b.field 则需要应用于字段/属性标识符。但除非我们将字段保留为单词,否则我们会出现异常情况,即您可以创建一个名为 field 的字段,但是不能在Java源代码中引用它。

  • 对于 c.method ,有一个问题是可以有多个可见的方法叫 C 。第二个问题,如果有一个名为 c 的字段和一个名为 c 的方法,那么 c.method 可以引用一个名为字段方法通过 c 字段。

  • The changes would introduce ambiguities into the language, making compilation and other parser-dependent tasks harder.
  • The changes would undoubtedly break existing code, whether or not method and field were turned into keywords.
  • You cannot treat b.field as an implicit object attribute, because it doesn't apply to objects. Rather b.field would need to apply to field / attribute identifiers. But unless we make field a reserved word, we have the anomalous situation that you can create a field called field but you cannot refer to it in Java sourcecode.
  • For c.method, there is the problem that there can be multiple visible methods called c. A second issue that if there is a field called c and a method called c, then c.method could be a reference to an field called method on the object referred to by the c field.

这篇关于为什么MyClass.class存在于java和MyField.field中不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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