我的一个朋友也在学习Java,他问我:“Java中的方法规范和方法签名有什么区别?”
我知道什么是方法签名,它是一个方法名及其参数- methodName(int n, String str)。
但是Java中的方法规范到底是什么呢?
我在网上找到的所有东西--只有一个pdf-档案。有关方法规范的信息如下:
因此,正如我所理解的,方法规范包括包名(例如package com.site.progect)、方法签名(methodName(int n, String str))、访问指定符(private、protected、default或public)、返回值的类型、static标识符(如果方法是静态的,并且不属于实例)。
方法规范是否包括类名,我们的方法是在其中定义的?它是否包括抛出异常的指示,该异常位于方法签名(... throws Exception())之后?它是否包括开发人员对该方法所做的评论?
发布于 2019-09-12 10:06:54
对于两个不同的概念,可能会出现混淆。
用法2似乎基本上是指方法签名以及方法的返回类型。
换句话说,虽然methodName(int n, String str)是方法的签名,但根据含义#2,一个可能的“方法规范”是Object methodName(int n, String str)。
发布于 2019-09-12 10:05:50
方法规范可以看作是方法的文档。你可以这样写:
/**
* Method - Description of the method
* @param int n - description of parameter
* @param String str - description of parameter
* @return int - description of the return value
**/
int methodName(int n, String str)
{
...
} 这个语法将用于生成javadoc。您可以看到更多细节这里
https://stackoverflow.com/questions/57904197
复制相似问题