简体   繁体   English

MyType#myMethod()中“#”的含义是什么,与MyType.myMethod()中的“。”的区别是什么

[英]What is the meaning of “#” in MyType#myMethod() and the difference with “.” as in MyType.myMethod()

Well, all is in the title... 好吧,一切都在标题中......

I understand that both forms refer to the method, but I don't see what # adds to the other form. 我知道两种形式都是指方法,但我没有看到#添加到另一种形式。

The hash character (#) is not part of the Java language. 哈希字符(#)不是Java语言的一部分。

It has a special use in javadoc. 它在javadoc中有特殊用途。 The specification states 规范说明

package.class#member is any valid program element name that is referenced -- a package, class, interface, constructor, method or field name -- except that the character ahead of the member name should be a hash character (#). package.class #member是引用的任何有效程序元素名称 - 包,类,接口,构造函数,方法或字段名称 - 除了成员名称前面的字符应该是哈希字符(#)。

and

As stated, the hash character (#), rather than a dot (.) separates a member from its class. 如上所述, 散列字符(#)而不是点(。)将成员与其类别分开。 This enables the Javadoc tool to resolve ambiguities, since the dot also separates classes, nested classes, packages, and subpackages. 这使得Javadoc工具能够解决歧义,因为该点还分隔了类,嵌套类,包和子包。 However, the Javadoc tool is generally lenient and will properly parse a dot if you know there is no ambiguity, though it will print a warning. 但是,Javadoc工具通常是宽松的,如果你知道没有歧义,它会正确地解析一个点,尽管它会打印一个警告。

The notation doesn't only apply to methods, it applies to any type member. 该表示法不仅适用于方法,它适用于任何类型的成员。 It helps remove ambiguity from fully qualified type names. 它有助于消除完全限定类型名称的歧义。

The . . version is the formal notation used in the Java programming language, more concretely, consider the following code: version是Java编程语言中使用的正式表示法,更具体地说,请考虑以下代码:

class Animal {
    void fly() { 
    }
}

The usethe method fly() we need to do the following: 使用方法fly()我们需要做以下事情:

Animal animal = new Animal();
animal.fly();

What we truly want to notate in the documentation however, is an invocation of the instance method fly() , hence we denote Animal#fly() , a seemingly alternative would be Animal.fly() , but that would denote a static method of the Animal class in Java code. 然而,我们真正想在文档中注意的是实例方法 fly()的调用,因此我们表示Animal#fly() ,看似替代方案是Animal.fly() ,但这表示静态方法 Java代码中的Animal类。

Hence Animal#fly() is being used over all other forms, to summarize, alternatives would be: 因此, Animal#fly()被用于所有其他形式,总结一下,替代方案将是:

  • Animal.fly() , however this can mean a static method. Animal.fly() ,但这可能意味着静态方法。
  • animal.fly() , however there is no class called animal . animal.fly() ,但是没有一个叫做animal类。

As a sidenote, it is worth to note that since Java 8, we can actually do these things in Java code directly, by using: 作为旁注,值得注意的是,自Java 8以来,我们实际上可以通过以下方式直接在Java代码中执行这些操作:

Runnable animalFly = Animal::fly;

Here we also reference the Animal#fly() method, however javadoc predates Java 8 by a long time. 这里我们也引用了Animal#fly()方法,但javadoc早在Java 8之前。

MyType#myMethod() is a syntax for JavaDoc so that your documentation can link to a different method's documentation. MyType#myMethod()JavaDoc的语法,因此您的文档可以链接到不同方法的文档。

For example Object.equals()'s javadoc contains text to link to the Object.hashCode() method since they are related. 例如,Object.equals()的javadoc包含链接到Object.hashCode()方法的文本,因为它们是相关的。

/**
 * ...
 *
 * @see Object#hashCode()
 */
  public boolean equals(Object obj)

Where as MyType.myMethod() is the syntx in actual syntax usd in Java code to call the method. 其中MyType.myMethod()是Java代码中用于调用方法的实际语法中的合成器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM