随着时代的发展,java的学习也显得更加重要起来。今天主要为大家介绍,在java中如何实现方法签名,并且通过实际的的代码为大家展现。
首先我们需要知道的是,java方法签名,它的主要作用在于区分两个方法,也就是唯一确定一个方法,用于确定两个方法是否可以重载。
在java中,确定一个方法需要以下三个要素:
①调用者,它是方法所属者,既可以是类,也可以是变量;
②方法名,方法的标识;
③形参列表,当调用方法时,系统将会根据传入的实参列表匹配。
方法重载的要求是两个相同一个不同:同一个类中方法名相同,参数列表不同。总而言之,方法签名就由方法名+形参列表构成。
举个例子给大家看一下,实例如下:
public void test1() {} test1() V public void test2(String str) test2(Ljava / lang / String;) V public int test3() {} test3() I
其实,JVM为我们提供的方法签名实际上是由方法名、形参列表、返回值三部分构成,基本形式就是全类名.方法名(形参数据类型列表)返回值数据类型。其中,签名中的特殊字符/字母含义如下表所示:
可以使用javap命令生成方法签名,代码如下:
$ javap - s java.lang.String
接下来为大家展示String类里的方法签名,代码展示如下:
C: \Users\ T > javap - s java.lang.String Compiled from "String.java" public final class java.lang.String implements java.io.Serializable, java.lang.Comparable < java.lang.String > , java.lang.CharSequence { public static final java.util.Comparator < java.lang.String > CASE_INSENSITIVE_ORDER; descriptor: Ljava / util / Comparator; public java.lang.String(); descriptor: () V public java.lang.String(java.lang.String); descriptor: (Ljava / lang / String;) V public java.lang.String(char[]); descriptor: ([C) V public java.lang.String(char[], int, int); descriptor: ([CII) V public java.lang.String(int[], int, int); descriptor: ([III) V public java.lang.String(byte[], int, int, int); descriptor: ([BIII) V public java.lang.String(byte[], int); descriptor: ([BI) V public java.lang.String(byte[], int, int, java.lang.String) throws java.io.UnsupportedEncodingException; descriptor: ([BIILjava / lang / String;) V public java.lang.String(byte[], int, int, java.nio.charset.Charset); descriptor: ([BIILjava / nio / charset / Charset;) V public java.lang.String(byte[], java.lang.String) throws java.io.UnsupportedEncodingException; descriptor: ([BLjava / lang / String;) V public java.lang.String(byte[], java.nio.charset.Charset); descriptor: ([BLjava / nio / charset / Charset;) V public java.lang.String(byte[], int, int); descriptor: ([BII) V public java.lang.String(byte[]); descriptor: ([B) V public java.lang.String(java.lang.StringBuffer); descriptor: (Ljava / lang / StringBuffer;) V public java.lang.String(java.lang.StringBuilder); descriptor: (Ljava / lang / StringBuilder;) V java.lang.String(char[], boolean); descriptor: ([CZ) V public int length(); descriptor: () I public boolean isEmpty(); descriptor: () Z public char charAt(int); descriptor: (I) C public int codePointAt(int); descriptor: (I) I public int codePointBefore(int); descriptor: (I) I public int codePointCount(int, int); descriptor: (II) I public int offsetByCodePoints(int, int); descriptor: (II) I void getChars(char[], int); descriptor: ([CI) V public void getChars(int, int, char[], int); descriptor: (II[CI) V public void getBytes(int, int, byte[], int); descriptor: (II[BI) V public byte[] getBytes(java.lang.String) throws java.io.UnsupportedEncodingException; descriptor: (Ljava / lang / String;)[B public byte[] getBytes(java.nio.charset.Charset); descriptor: (Ljava / nio / charset / Charset;)[B public byte[] getBytes(); descriptor: ()[B public boolean equals(java.lang.Object); descriptor: (Ljava / lang / Object;) Z public boolean contentEquals(java.lang.StringBuffer); descriptor: (Ljava / lang / StringBuffer;) Z public boolean contentEquals(java.lang.CharSequence); descriptor: (Ljava / lang / CharSequence;) Z public boolean equalsIgnoreCase(java.lang.String); descriptor: (Ljava / lang / String;) Z public int compareTo(java.lang.String); descriptor: (Ljava / lang / String;) I public int compareToIgnoreCase(java.lang.String); descriptor: (Ljava / lang / String;) I public boolean regionMatches(int, java.lang.String, int, int); descriptor: (ILjava / lang / String; II) Z public boolean regionMatches(boolean, int, java.lang.String, int, int); descriptor: (ZILjava / lang / String; II) Z public boolean startsWith(java.lang.String, int); descriptor: (Ljava / lang / String; I) Z public boolean startsWith(java.lang.String); descriptor: (Ljava / lang / String;) Z public boolean endsWith(java.lang.String); descriptor: (Ljava / lang / String;) Z public int hashCode(); descriptor: () I public int indexOf(int); descriptor: (I) I public int indexOf(int, int); descriptor: (II) I public int lastIndexOf(int); descriptor: (I) I public int lastIndexOf(int, int); descriptor: (II) I public int indexOf(java.lang.String); descriptor: (Ljava / lang / String;) I public int indexOf(java.lang.String, int); descriptor: (Ljava / lang / String; I) I static int indexOf(char[], int, int, java.lang.String, int); descriptor: ([CIILjava / lang / String;I) I static int indexOf(char[], int, int, char[], int, int, int); descriptor: ([CII[CIII) I public int lastIndexOf(java.lang.String); descriptor: (Ljava / lang / String;) I public int lastIndexOf(java.lang.String, int); descriptor: (Ljava / lang / String; I) I static int lastIndexOf(char[], int, int, java.lang.String, int); descriptor: ([CIILjava / lang / String;I) I static int lastIndexOf(char[], int, int, char[], int, int, int); descriptor: ([CII[CIII) I public java.lang.String substring(int); descriptor: (I) Ljava / lang / String; public java.lang.String substring(int, int); descriptor: (II) Ljava / lang / String; public java.lang.CharSequence subSequence(int, int); descriptor: (II) Ljava / lang / CharSequence; public java.lang.String concat(java.lang.String); descriptor: (Ljava / lang / String;) Ljava / lang / String; public java.lang.String replace(char, char); descriptor: (CC) Ljava / lang / String; public boolean matches(java.lang.String); descriptor: (Ljava / lang / String;) Z public boolean contains(java.lang.CharSequence); descriptor: (Ljava / lang / CharSequence;) Z public java.lang.String replaceFirst(java.lang.String, java.lang.String); descriptor: (Ljava / lang / String; Ljava / lang / String;) Ljava / lang / String; public java.lang.String replaceAll(java.lang.String, java.lang.String); descriptor: (Ljava / lang / String; Ljava / lang / String;) Ljava / lang / String; public java.lang.String replace(java.lang.CharSequence, java.lang.CharSequence); descriptor: (Ljava / lang / CharSequence; Ljava / lang / CharSequence;) Ljava / lang / String; public java.lang.String[] split(java.lang.String, int); descriptor: (Ljava / lang / String; I)[Ljava / lang / String;
以上就是有关于在java中实现方法签名的具体操作方法,并且为大家列举了方法签名的集合。想要了解更多java基础,敬请关注奇Q工具网。
推荐阅读: