一目了然解释getName()、getCanonicalName()和getSimpleName()的异同

(转载)http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667

对于一般的类:
getName()和getCanonicalName()返回结果是一样的,都包括了包名
getSimpleName()只有类名,没有包名

现在来比较getName()和getCanonicalName()的区别
对于数组:比如int[]
getName()返回的是:[I
getCanonicalName()返回的是:int[]

对于内部类:
getName()返回的是:com.getname.pkg.Main$Demo1$Demo2
getCanonicalName()返回的是:com.getname.pkg.Main.Demo1.Demo2

程序结果:

-----三种方式获取普通类的名字-----
 getName()方法:com.getname.pkg.Main
getCanonicalName()方法: com.getname.pkg.Main
getSimpleName()方法: Main

-----对于数组 int[]-----
 getName()方法:[I
getCanonicalName()方法: int[]

-----对于内部类-----
 getName()方法:com.getname.pkg.Main$Demo1$Demo2
getCanonicalName()方法: com.getname.pkg.Main.Demo1.Demo2

代码:

<a target=_blank id="L1" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank id="L29" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank id="L30" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank id="L31" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank id="L32" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
<a target=_blank id="L33" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a>
<a target=_blank id="L34" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a>
<a target=_blank id="L35" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;"> 35</a>
<a target=_blank id="L36" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;"> 36</a>
<a target=_blank id="L37" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;"> 37</a>
<a target=_blank id="L38" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;"> 38</a>
<a target=_blank id="L39" href="http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u012882327/article/details/48650667#L39" rel="#L39" style="color: rgb(102, 102, 102); text-decoration: none;"> 39</a>
             
             
             
             
package com . getname . pkg ;
public class Main {
public class Demo1 {
class Demo2 {
}
}
public static void main ( String [] args ) {
/* 三种方式获取普通类Main的名字 */
System . out . println ( "-----三种方式获取普通类的名字-----" );
String name = Main . class . getName ();
System . out . println ( "\t" + "getName()方法:" + name );
String canonicalname = Main . class . getCanonicalName ();
System . out . println ( "getCanonicalName()方法:" + "\t" + canonicalname );
String simplename = Main . class . getSimpleName ();
System . out . println ( "getSimpleName()方法:" + "\t" + simplename );
/* getName()和getCanonicalName()获取数组名比较 */
int [] aaa = { 1 , 2 , 3 };
System . out . println ( "\r\n" + "-----对于数组 int[]-----" );
name = aaa . getClass (). getName ();
System . out . println ( "\t" + "getName()方法:" + name );
canonicalname = aaa . getClass (). getCanonicalName ();
System . out . println ( "getCanonicalName()方法:" + "\t" + canonicalname );
/* getName()和getCanonicalName()获取内部类名比较 */
System . out . println ( "\r\n" + "-----对于内部类-----" );
name = Demo1 . Demo2 . class . getName ();
System . out . println ( "\t" + "getName()方法:" + name );
canonicalname = Demo1 . Demo2 . class . getCanonicalName ();
System . out . println ( "getCanonicalName()方法:" + "\t" + canonicalname );
}
}
 来自CODE的代码片
getNameDemo.java


你可能感兴趣的:(一目了然解释getName()、getCanonicalName()和getSimpleName()的异同)