{"id":194,"date":"2022-11-17T23:50:09","date_gmt":"2022-11-17T15:50:09","guid":{"rendered":"https:\/\/www.muxuetianyin.cn\/?p=194"},"modified":"2024-05-28T16:55:29","modified_gmt":"2024-05-28T08:55:29","slug":"java%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"https:\/\/www.muxuetianyin.cn\/?p=194","title":{"rendered":"Java\u57fa\u7840"},"content":{"rendered":"\n<p>1.\u6570\u636e\u7c7b\u578b\u7684\u5206\u7c7b<\/p>\n\n\n\n<p>*\u5f15\u7528\u6570\u636e\u7c7b\u578b\uff08\u9664\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e4b\u5916\u7684\uff0c\u5982String\uff09<\/p>\n\n\n\n<p>*\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff1a4\u5927\u7c7b8\u79cd<\/p>\n\n\n\n<p><strong>\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\u53d8\u91cf\u7684\u51e0\u4e2a\u8865\u5145\u77e5\u8bc6\u70b9<\/strong><\/p>\n\n\n\n<p>\u26ab \u968f\u4fbf\u5199\u4e00\u4e2a\u6574\u6570\u5b57\u9762\u503c\uff0c\u9ed8\u8ba4\u662fint\u7c7b\u578b\u7684\uff0c\u5982\u679c\u5e0c\u671b\u968f\u4fbf\u5199\u4e00\u4e2a\u6574\u6570\u9ed8\u8ba4\u662flong\u578b\u7684\u5fc5\u987b\u5728\u6570\u636e\u540e\u52a0L\u6216\u8005l\u8868\u793a\u3002<\/p>\n\n\n\n<p>\u26ab \u968f\u4fbf\u5199\u4e00\u4e2a\u5c0f\u6570\u5b57\u9762\u503c\uff0c\u9ed8\u8ba4\u662fdouble\u7c7b\u578b\u7684\uff0c\u5982\u679c\u5e0c\u671b\u8fd9\u4e2a\u5c0f\u6570\u662ffloat\u7c7b\u578b\u7684\uff0c\u5fc5\u987b\u5728\u6570\u636e\u540e\u52a0F\u6216\u8005f\u8868\u793a\u3002<\/p>\n\n\n\n<h1>Java \u57fa\u7840<\/h1>\n\n\n\n<h2>\u5173\u952e\u5b57<\/h2>\n\n\n\n<h3>final<\/h3>\n\n\n\n<p><strong>1. \u6570\u636e<\/strong><\/p>\n\n\n\n<p>\u58f0\u660e\u6570\u636e\u4e3a\u5e38\u91cf\uff0c\u53ef\u4ee5\u662f\u7f16\u8bd1\u65f6\u5e38\u91cf\uff0c\u4e5f\u53ef\u4ee5\u662f\u5728\u8fd0\u884c\u65f6\u88ab\u521d\u59cb\u5316\u540e\u4e0d\u80fd\u88ab\u6539\u53d8\u7684\u5e38\u91cf\u3002<\/p>\n\n\n\n<ul><li>\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\uff0cfinal \u4f7f\u6570\u503c\u4e0d\u53d8\uff1b<\/li><li>\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\uff0cfinal \u4f7f\u5f15\u7528\u4e0d\u53d8\uff0c\u4e5f\u5c31\u4e0d\u80fd\u5f15\u7528\u5176\u5b83\u5bf9\u8c61\uff0c\u4f46\u662f\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u672c\u8eab\u662f\u53ef\u4ee5\u4fee\u6539\u7684\u3002<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;final int x = 1;<br>&nbsp;\/\/ x = 2;  \/\/ cannot assign value to final variable 'x'<br>&nbsp;final A y = new A();<br>&nbsp;y.a = 1;<\/pre>\n\n\n\n<p><strong>2. \u65b9\u6cd5<\/strong><\/p>\n\n\n\n<p>\u58f0\u660e\u65b9\u6cd5\u4e0d\u80fd\u88ab\u5b50\u7c7b\u91cd\u5199\u3002<\/p>\n\n\n\n<p>private \u65b9\u6cd5\u9690\u5f0f\u5730\u88ab\u6307\u5b9a\u4e3a final\uff0c\u5982\u679c\u5728\u5b50\u7c7b\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\u548c\u57fa\u7c7b\u4e2d\u7684\u4e00\u4e2a private \u65b9\u6cd5\u7b7e\u540d\u76f8\u540c\uff0c\u6b64\u65f6\u5b50\u7c7b\u7684\u65b9\u6cd5\u4e0d\u662f\u91cd\u5199\u57fa\u7c7b\u65b9\u6cd5\uff0c\u800c\u662f\u5728\u5b50\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b0\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p><strong>3. \u7c7b<\/strong><\/p>\n\n\n\n<p>\u58f0\u660e\u7c7b\u4e0d\u5141\u8bb8\u88ab\u7ee7\u627f\u3002<\/p>\n\n\n\n<h3>static<\/h3>\n\n\n\n<p><strong>1. \u9759\u6001\u53d8\u91cf<\/strong><\/p>\n\n\n\n<ul><li>\u9759\u6001\u53d8\u91cf\uff1a\u53c8\u79f0\u4e3a\u7c7b\u53d8\u91cf\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd9\u4e2a\u53d8\u91cf\u5c5e\u4e8e\u7c7b\u7684\uff0c\u7c7b\u6240\u6709\u7684\u5b9e\u4f8b\u90fd\u5171\u4eab\u9759\u6001\u53d8\u91cf\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u6765\u8bbf\u95ee\u5b83\u3002\u9759\u6001\u53d8\u91cf\u5728\u5185\u5b58\u4e2d\u53ea\u5b58\u5728\u4e00\u4efd\u3002<\/li><li>\u5b9e\u4f8b\u53d8\u91cf\uff1a\u6bcf\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\u5c31\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5b9e\u4f8b\u53d8\u91cf\uff0c\u5b83\u4e0e\u8be5\u5b9e\u4f8b\u540c\u751f\u5171\u6b7b\u3002<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;public class A {<br>&nbsp;\u200b<br>&nbsp; &nbsp;  private int x; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ \u5b9e\u4f8b\u53d8\u91cf<br>&nbsp; &nbsp;  private static int y;  \/\/ \u9759\u6001\u53d8\u91cf<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public static void main(String&#91;] args) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  \/\/ int x = A.x;  \/\/ Non-static field 'x' cannot be referenced from a static context<br>&nbsp; &nbsp; &nbsp; &nbsp;  A a = new A();<br>&nbsp; &nbsp; &nbsp; &nbsp;  int x = a.x;<br>&nbsp; &nbsp; &nbsp; &nbsp;  int y = A.y;<br>&nbsp; &nbsp;  }<br>&nbsp;}<\/code><\/pre>\n\n\n\n<p><strong>2. \u9759\u6001\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<p>\u9759\u6001\u65b9\u6cd5\u5728\u7c7b\u52a0\u8f7d\u7684\u65f6\u5019\u5c31\u5b58\u5728\u4e86\uff0c\u5b83\u4e0d\u4f9d\u8d56\u4e8e\u4efb\u4f55\u5b9e\u4f8b\u3002\u6240\u4ee5\u9759\u6001\u65b9\u6cd5\u5fc5\u987b\u6709\u5b9e\u73b0\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u4e0d\u80fd\u662f\u62bd\u8c61\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;public abstract class A {<br>&nbsp; &nbsp;  public static void func1(){<br>&nbsp; &nbsp;  }<br>&nbsp; &nbsp;  \/\/ public abstract static void func2();  \/\/ Illegal combination of modifiers: 'abstract' and 'static'<br>&nbsp;}<\/code><\/pre>\n\n\n\n<p>\u53ea\u80fd\u8bbf\u95ee\u6240\u5c5e\u7c7b\u7684\u9759\u6001\u5b57\u6bb5\u548c\u9759\u6001\u65b9\u6cd5\uff0c\u65b9\u6cd5\u4e2d\u4e0d\u80fd\u6709 this \u548c super \u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u8fd9\u4e24\u4e2a\u5173\u952e\u5b57\u4e0e\u5177\u4f53\u5bf9\u8c61\u5173\u8054\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;public class A {<br>&nbsp;\u200b<br>&nbsp; &nbsp;  private static int x;<br>&nbsp; &nbsp;  private int y;<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public static void func1(){<br>&nbsp; &nbsp; &nbsp; &nbsp;  int a = x;<br>&nbsp; &nbsp; &nbsp; &nbsp;  \/\/ int b = y;  \/\/ Non-static field 'y' cannot be referenced from a static context<br>&nbsp; &nbsp; &nbsp; &nbsp;  \/\/ int b = this.y; &nbsp; &nbsp; \/\/ 'A.this' cannot be referenced from a static context<br>&nbsp; &nbsp;  }<br>&nbsp;}<\/code><\/pre>\n\n\n\n<p><strong>3. \u9759\u6001\u8bed\u53e5\u5757<\/strong><\/p>\n\n\n\n<p>\u9759\u6001\u8bed\u53e5\u5757\u5728\u7c7b\u521d\u59cb\u5316\u65f6\u8fd0\u884c\u4e00\u6b21\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u00a0public class A {\n\u00a0 \u00a0  static {\n\u00a0 \u00a0 \u00a0 \u00a0  System.out.println(\"123\");\n\u00a0 \u00a0  }\n\u00a0\u200b\n\u00a0 \u00a0  public static void main(String&#91;] args) {\n\u00a0 \u00a0 \u00a0 \u00a0  A a1 = new A();\n\u00a0 \u00a0 \u00a0 \u00a0  A a2 = new A();\n\u00a0 \u00a0  }\n\u00a0}<\/code><\/pre>\n\n\n\n<p><strong>4. \u9759\u6001\u5185\u90e8\u7c7b<\/strong><\/p>\n\n\n\n<p>\u975e\u9759\u6001\u5185\u90e8\u7c7b\u4f9d\u8d56\u4e8e\u5916\u90e8\u7c7b\u7684\u5b9e\u4f8b\uff0c\u4e5f\u5c31\u662f\u8bf4\u9700\u8981\u5148\u521b\u5efa\u5916\u90e8\u7c7b\u5b9e\u4f8b\uff0c\u624d\u80fd\u7528\u8fd9\u4e2a\u5b9e\u4f8b\u53bb\u521b\u5efa\u975e\u9759\u6001\u5185\u90e8\u7c7b\u3002\u800c\u9759\u6001\u5185\u90e8\u7c7b\u4e0d\u9700\u8981\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;public class OuterClass {<br>&nbsp;\u200b<br>&nbsp; &nbsp;  class InnerClass {<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  static class StaticInnerClass {<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public static void main(String&#91;] args) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  \/\/ InnerClass innerClass = new InnerClass(); \/\/ 'OuterClass.this' cannot be referenced from a static context<br>&nbsp; &nbsp; &nbsp; &nbsp;  OuterClass outerClass = new OuterClass();<br>&nbsp; &nbsp; &nbsp; &nbsp;  InnerClass innerClass = outerClass.new InnerClass();<br>&nbsp; &nbsp; &nbsp; &nbsp;  StaticInnerClass staticInnerClass = new StaticInnerClass();<br>&nbsp; &nbsp;  }<br>&nbsp;}<\/code><\/pre>\n\n\n\n<p>\u9759\u6001\u5185\u90e8\u7c7b\u4e0d\u80fd\u8bbf\u95ee\u5916\u90e8\u7c7b\u7684\u975e\u9759\u6001\u7684\u53d8\u91cf\u548c\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p><strong>5. \u9759\u6001\u5bfc\u5305<\/strong><\/p>\n\n\n\n<p>\u5728\u4f7f\u7528\u9759\u6001\u53d8\u91cf\u548c\u65b9\u6cd5\u65f6\u4e0d\u7528\u518d\u6307\u660e ClassName\uff0c\u4ece\u800c\u7b80\u5316\u4ee3\u7801\uff0c\u4f46\u53ef\u8bfb\u6027\u5927\u5927\u964d\u4f4e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;import static com.xxx.ClassName.*<\/pre>\n\n\n\n<p><strong>6. \u521d\u59cb\u5316\u987a\u5e8f<\/strong><\/p>\n\n\n\n<p>\u9759\u6001\u53d8\u91cf\u548c\u9759\u6001\u8bed\u53e5\u5757\u4f18\u5148\u4e8e\u5b9e\u4f8b\u53d8\u91cf\u548c\u666e\u901a\u8bed\u53e5\u5757\uff0c\u9759\u6001\u53d8\u91cf\u548c\u9759\u6001\u8bed\u53e5\u5757\u7684\u521d\u59cb\u5316\u987a\u5e8f\u53d6\u51b3\u4e8e\u5b83\u4eec\u5728\u4ee3\u7801\u4e2d\u7684\u987a\u5e8f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;public static String staticField = \"\u9759\u6001\u53d8\u91cf\";<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;static {<br>&nbsp; &nbsp;  System.out.println(\"\u9759\u6001\u8bed\u53e5\u5757\");<br>&nbsp;}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;public String field = \"\u5b9e\u4f8b\u53d8\u91cf\";<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;{<br>&nbsp; &nbsp;  System.out.println(\"\u666e\u901a\u8bed\u53e5\u5757\");<br>&nbsp;}<\/pre>\n\n\n\n<p>\u6700\u540e\u624d\u662f\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;public InitialOrderTest() {<br>&nbsp; &nbsp;  System.out.println(\"\u6784\u9020\u51fd\u6570\");<br>&nbsp;}<\/code><\/pre>\n\n\n\n<p>\u5b58\u5728\u7ee7\u627f\u7684\u60c5\u51b5\u4e0b\uff0c\u521d\u59cb\u5316\u987a\u5e8f\u4e3a\uff1a<\/p>\n\n\n\n<ul><li>\u7236\u7c7b\uff08\u9759\u6001\u53d8\u91cf\u3001\u9759\u6001\u8bed\u53e5\u5757\uff09<\/li><li>\u5b50\u7c7b\uff08\u9759\u6001\u53d8\u91cf\u3001\u9759\u6001\u8bed\u53e5\u5757\uff09<\/li><li>\u7236\u7c7b\uff08\u5b9e\u4f8b\u53d8\u91cf\u3001\u666e\u901a\u8bed\u53e5\u5757\uff09<\/li><li>\u7236\u7c7b\uff08\u6784\u9020\u51fd\u6570\uff09<\/li><li>\u5b50\u7c7b\uff08\u5b9e\u4f8b\u53d8\u91cf\u3001\u666e\u901a\u8bed\u53e5\u5757\uff09<\/li><li>\u5b50\u7c7b\uff08\u6784\u9020\u51fd\u6570\uff09<\/li><\/ul>\n\n\n\n<h2>Object \u901a\u7528\u65b9\u6cd5<\/h2>\n\n\n\n<h3>\u6982\u89c8<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;\u200b<br>&nbsp;public native int hashCode()<br>&nbsp;\u200b<br>&nbsp;public boolean equals(Object obj)<br>&nbsp;\u200b<br>&nbsp;protected native Object clone() throws CloneNotSupportedException<br>&nbsp;\u200b<br>&nbsp;public String toString()<br>&nbsp;\u200b<br>&nbsp;public final native Class&lt;?&gt; getClass()<br>&nbsp;\u200b<br>&nbsp;protected void finalize() throws Throwable {}<br>&nbsp;\u200b<br>&nbsp;public final native void notify()<br>&nbsp;\u200b<br>&nbsp;public final native void notifyAll()<br>&nbsp;\u200b<br>&nbsp;public final native void wait(long timeout) throws InterruptedException<br>&nbsp;\u200b<br>&nbsp;public final void wait(long timeout, int nanos) throws InterruptedException<br>&nbsp;\u200b<br>&nbsp;public final void wait() throws InterruptedException<\/code><\/pre>\n\n\n\n<h3>equals()<\/h3>\n\n\n\n<p><strong>1. \u7b49\u4ef7\u5173\u7cfb<\/strong><\/p>\n\n\n\n<p>\u4e24\u4e2a\u5bf9\u8c61\u5177\u6709\u7b49\u4ef7\u5173\u7cfb\uff0c\u9700\u8981\u6ee1\u8db3\u4ee5\u4e0b\u4e94\u4e2a\u6761\u4ef6\uff1a<\/p>\n\n\n\n<p>\u2160 \u81ea\u53cd\u6027<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;x.equals(x); \/\/ true<\/pre>\n\n\n\n<p>\u2161 \u5bf9\u79f0\u6027<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;x.equals(y) == y.equals(x); \/\/ true<\/pre>\n\n\n\n<p>\u2162 \u4f20\u9012\u6027<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;if (x.equals(y) &amp;&amp; y.equals(z))<br>&nbsp; &nbsp;  x.equals(z); \/\/ true;<\/pre>\n\n\n\n<p>\u2163 \u4e00\u81f4\u6027<\/p>\n\n\n\n<p>\u591a\u6b21\u8c03\u7528 equals() \u65b9\u6cd5\u7ed3\u679c\u4e0d\u53d8<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;x.equals(y) == x.equals(y); \/\/ true<\/pre>\n\n\n\n<p>\u2164 \u4e0e null \u7684\u6bd4\u8f83<\/p>\n\n\n\n<p>\u5bf9\u4efb\u4f55\u4e0d\u662f null \u7684\u5bf9\u8c61 x \u8c03\u7528 x.equals(null) \u7ed3\u679c\u90fd\u4e3a false<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;x.equals(null); \/\/ false;<\/pre>\n\n\n\n<p><strong>2. \u7b49\u4ef7\u4e0e\u76f8\u7b49<\/strong><\/p>\n\n\n\n<ul><li>\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\uff0c== \u5224\u65ad\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49\uff0c\u57fa\u672c\u7c7b\u578b\u6ca1\u6709 equals() \u65b9\u6cd5\u3002<\/li><li>\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\uff0c== \u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c equals() \u5224\u65ad\u5f15\u7528\u7684\u5bf9\u8c61\u662f\u5426\u7b49\u4ef7\u3002<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;Integer x = new Integer(1);<br>&nbsp;Integer y = new Integer(1);<br>&nbsp;System.out.println(x.equals(y)); \/\/ true<br>&nbsp;System.out.println(x == y); &nbsp; &nbsp;  \/\/ false<\/pre>\n\n\n\n<p><strong>3. \u5b9e\u73b0<\/strong><\/p>\n\n\n\n<ul><li>\u68c0\u67e5\u662f\u5426\u4e3a\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u5982\u679c\u662f\u76f4\u63a5\u8fd4\u56de true\uff1b<\/li><li>\u68c0\u67e5\u662f\u5426\u662f\u540c\u4e00\u4e2a\u7c7b\u578b\uff0c\u5982\u679c\u4e0d\u662f\uff0c\u76f4\u63a5\u8fd4\u56de false\uff1b<\/li><li>\u5c06 Object \u5bf9\u8c61\u8fdb\u884c\u8f6c\u578b\uff1b<\/li><li>\u5224\u65ad\u6bcf\u4e2a\u5173\u952e\u57df\u662f\u5426\u76f8\u7b49\u3002<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;public class EqualExample {<br>&nbsp;\u200b<br>&nbsp; &nbsp;  private int x;<br>&nbsp; &nbsp;  private int y;<br>&nbsp; &nbsp;  private int z;<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public EqualExample(int x, int y, int z) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  this.x = x;<br>&nbsp; &nbsp; &nbsp; &nbsp;  this.y = y;<br>&nbsp; &nbsp; &nbsp; &nbsp;  this.z = z;<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  @Override<br>&nbsp; &nbsp;  public boolean equals(Object o) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  if (this == o) return true;<br>&nbsp; &nbsp; &nbsp; &nbsp;  if (o == null || getClass() != o.getClass()) return false;<br>&nbsp;\u200b<br>&nbsp; &nbsp; &nbsp; &nbsp;  EqualExample that = (EqualExample) o;<br>&nbsp;\u200b<br>&nbsp; &nbsp; &nbsp; &nbsp;  if (x != that.x) return false;<br>&nbsp; &nbsp; &nbsp; &nbsp;  if (y != that.y) return false;<br>&nbsp; &nbsp; &nbsp; &nbsp;  return z == that.z;<br>&nbsp; &nbsp;  }<br>&nbsp;}<\/code><\/pre>\n\n\n\n<h3>ashCode()<\/h3>\n\n\n\n<p>hashCode() \u8fd4\u56de\u54c8\u5e0c\u503c\uff0c\u800c equals() \u662f\u7528\u6765\u5224\u65ad\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u7b49\u4ef7\u3002\u7b49\u4ef7\u7684\u4e24\u4e2a\u5bf9\u8c61\u6563\u5217\u503c\u4e00\u5b9a\u76f8\u540c\uff0c\u4f46\u662f\u6563\u5217\u503c\u76f8\u540c\u7684\u4e24\u4e2a\u5bf9\u8c61\u4e0d\u4e00\u5b9a\u7b49\u4ef7\uff0c\u8fd9\u662f\u56e0\u4e3a\u8ba1\u7b97\u54c8\u5e0c\u503c\u5177\u6709\u968f\u673a\u6027\uff0c\u4e24\u4e2a\u503c\u4e0d\u540c\u7684\u5bf9\u8c61\u53ef\u80fd\u8ba1\u7b97\u51fa\u76f8\u540c\u7684\u54c8\u5e0c\u503c\u3002<\/p>\n\n\n\n<p>\u5728\u8986\u76d6 equals() \u65b9\u6cd5\u65f6\u5e94\u5f53\u603b\u662f\u8986\u76d6 hashCode() \u65b9\u6cd5\uff0c\u4fdd\u8bc1\u7b49\u4ef7\u7684\u4e24\u4e2a\u5bf9\u8c61\u54c8\u5e0c\u503c\u4e5f\u76f8\u7b49\u3002<\/p>\n\n\n\n<p>HashSet \u548c HashMap \u7b49\u96c6\u5408\u7c7b\u4f7f\u7528\u4e86 hashCode() \u65b9\u6cd5\u6765\u8ba1\u7b97\u5bf9\u8c61\u5e94\u8be5\u5b58\u50a8\u7684\u4f4d\u7f6e\uff0c\u56e0\u6b64\u8981\u5c06\u5bf9\u8c61\u6dfb\u52a0\u5230\u8fd9\u4e9b\u96c6\u5408\u7c7b\u4e2d\uff0c\u9700\u8981\u8ba9\u5bf9\u5e94\u7684\u7c7b\u5b9e\u73b0 hashCode() \u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u65b0\u5efa\u4e86\u4e24\u4e2a\u7b49\u4ef7\u7684\u5bf9\u8c61\uff0c\u5e76\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230 HashSet \u4e2d\u3002\u6211\u4eec\u5e0c\u671b\u5c06\u8fd9\u4e24\u4e2a\u5bf9\u8c61\u5f53\u6210\u4e00\u6837\u7684\uff0c\u53ea\u5728\u96c6\u5408\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5bf9\u8c61\u3002\u4f46\u662f EqualExample \u6ca1\u6709\u5b9e\u73b0 hashCode() \u65b9\u6cd5\uff0c\u56e0\u6b64\u8fd9\u4e24\u4e2a\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u662f\u4e0d\u540c\u7684\uff0c\u6700\u7ec8\u5bfc\u81f4\u96c6\u5408\u6dfb\u52a0\u4e86\u4e24\u4e2a\u7b49\u4ef7\u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;EqualExample e1 = new EqualExample(1, 1, 1);<br>&nbsp;EqualExample e2 = new EqualExample(1, 1, 1);<br>&nbsp;System.out.println(e1.equals(e2)); \/\/ true<br>&nbsp;HashSet&lt;EqualExample&gt; set = new HashSet&lt;&gt;();<br>&nbsp;set.add(e1);<br>&nbsp;set.add(e2);<br>&nbsp;System.out.println(set.size()); &nbsp; \/\/ 2<\/code><\/pre>\n\n\n\n<p>\u7406\u60f3\u7684\u54c8\u5e0c\u51fd\u6570\u5e94\u5f53\u5177\u6709\u5747\u5300\u6027\uff0c\u5373\u4e0d\u76f8\u7b49\u7684\u5bf9\u8c61\u5e94\u5f53\u5747\u5300\u5206\u5e03\u5230\u6240\u6709\u53ef\u80fd\u7684\u54c8\u5e0c\u503c\u4e0a\u3002\u8fd9\u5c31\u8981\u6c42\u4e86\u54c8\u5e0c\u51fd\u6570\u8981\u628a\u6240\u6709\u57df\u7684\u503c\u90fd\u8003\u8651\u8fdb\u6765\u3002\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u57df\u90fd\u5f53\u6210 R \u8fdb\u5236\u7684\u67d0\u4e00\u4f4d\uff0c\u7136\u540e\u7ec4\u6210\u4e00\u4e2a R \u8fdb\u5236\u7684\u6574\u6570\u3002<\/p>\n\n\n\n<p>R \u4e00\u822c\u53d6 31\uff0c\u56e0\u4e3a\u5b83\u662f\u4e00\u4e2a\u5947\u7d20\u6570\uff0c\u5982\u679c\u662f\u5076\u6570\u7684\u8bdd\uff0c\u5f53\u51fa\u73b0\u4e58\u6cd5\u6ea2\u51fa\uff0c\u4fe1\u606f\u5c31\u4f1a\u4e22\u5931\uff0c\u56e0\u4e3a\u4e0e 2 \u76f8\u4e58\u76f8\u5f53\u4e8e\u5411\u5de6\u79fb\u4e00\u4f4d\uff0c\u6700\u5de6\u8fb9\u7684\u4f4d\u4e22\u5931\u3002\u5e76\u4e14\u4e00\u4e2a\u6570\u4e0e 31 \u76f8\u4e58\u53ef\u4ee5\u8f6c\u6362\u6210\u79fb\u4f4d\u548c\u51cf\u6cd5\uff1a<code>31*x == (x&lt;&lt;5)-x<\/code>\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u8fdb\u884c\u8fd9\u4e2a\u4f18\u5316\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;@Override<br>&nbsp;public int hashCode() {<br>&nbsp; &nbsp;  int result = 17;<br>&nbsp; &nbsp;  result = 31 * result + x;<br>&nbsp; &nbsp;  result = 31 * result + y;<br>&nbsp; &nbsp;  result = 31 * result + z;<br>&nbsp; &nbsp;  return result;<br>&nbsp;}<\/code><\/pre>\n\n\n\n<h3>toString()<\/h3>\n\n\n\n<p>\u9ed8\u8ba4\u8fd4\u56de ToStringExample@4554617c \u8fd9\u79cd\u5f62\u5f0f\uff0c\u5176\u4e2d @ \u540e\u9762\u7684\u6570\u503c\u4e3a\u6563\u5217\u7801\u7684\u65e0\u7b26\u53f7\u5341\u516d\u8fdb\u5236\u8868\u793a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u00a0public class ToStringExample {\n\u00a0\u200b\n\u00a0 \u00a0  private int number;\n\u00a0\u200b\n\u00a0 \u00a0  public ToStringExample(int number) {\n\u00a0 \u00a0 \u00a0 \u00a0  this.number = number;\n\u00a0 \u00a0  }\n\u00a0}\n ToStringExample example = new ToStringExample(123);\n System.out.println(example.toString());\n ToStringExample@4554617c<\/code><\/pre>\n\n\n\n<h3>clone()<\/h3>\n\n\n\n<p><strong>1. cloneable<\/strong><\/p>\n\n\n\n<p>clone() \u662f Object \u7684 protected \u65b9\u6cd5\uff0c\u5b83\u4e0d\u662f public\uff0c\u4e00\u4e2a\u7c7b\u4e0d\u663e\u5f0f\u53bb\u91cd\u5199 clone()\uff0c\u5176\u5b83\u7c7b\u5c31\u4e0d\u80fd\u76f4\u63a5\u53bb\u8c03\u7528\u8be5\u7c7b\u5b9e\u4f8b\u7684 clone() \u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;public class CloneExample {<br>&nbsp; &nbsp;  private int a;<br>&nbsp; &nbsp;  private int b;<br>&nbsp;}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;CloneExample e1 = new CloneExample();<br>&nbsp;\/\/ CloneExample e2 = e1.clone(); \/\/ 'clone()' has protected access in 'java.lang.Object'<\/pre>\n\n\n\n<p>\u91cd\u5199 clone() \u5f97\u5230\u4ee5\u4e0b\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u629b\u51fa\u4e86 CloneNotSupportedException\uff0c\u8fd9\u662f\u56e0\u4e3a CloneExample \u6ca1\u6709\u5b9e\u73b0 Cloneable \u63a5\u53e3\u3002<\/p>\n\n\n\n<p>\u5e94\u8be5\u6ce8\u610f\u7684\u662f\uff0cclone() \u65b9\u6cd5\u5e76\u4e0d\u662f Cloneable \u63a5\u53e3\u7684\u65b9\u6cd5\uff0c\u800c\u662f Object \u7684\u4e00\u4e2a protected \u65b9\u6cd5\u3002Cloneable \u63a5\u53e3\u53ea\u662f\u89c4\u5b9a\uff0c\u5982\u679c\u4e00\u4e2a\u7c7b\u6ca1\u6709\u5b9e\u73b0 Cloneable \u63a5\u53e3\u53c8\u8c03\u7528\u4e86 clone() \u65b9\u6cd5\uff0c\u5c31\u4f1a\u629b\u51fa CloneNotSupportedException\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;public class CloneExample implements Cloneable {<br>&nbsp; &nbsp;  private int a;<br>&nbsp; &nbsp;  private int b;<br>&nbsp;\u200b<br>&nbsp; &nbsp;  @Override<br>&nbsp; &nbsp;  public Object clone() throws CloneNotSupportedException {<br>&nbsp; &nbsp; &nbsp; &nbsp;  return super.clone();<br>&nbsp; &nbsp;  }<br>&nbsp;}<\/pre>\n\n\n\n<p><strong>2. \u6d45\u62f7\u8d1d<\/strong><\/p>\n\n\n\n<p>\u62f7\u8d1d\u5bf9\u8c61\u548c\u539f\u59cb\u5bf9\u8c61\u7684\u5f15\u7528\u7c7b\u578b\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;public class ShallowCloneExample implements Cloneable {<br>&nbsp;\u200b<br>&nbsp; &nbsp;  private int&#91;] arr;<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public ShallowCloneExample() {<br>&nbsp; &nbsp; &nbsp; &nbsp;  arr = new int&#91;10];<br>&nbsp; &nbsp; &nbsp; &nbsp;  for (int i = 0; i &lt; arr.length; i++) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  arr&#91;i] = i;<br>&nbsp; &nbsp; &nbsp; &nbsp;  }<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public void set(int index, int value) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  arr&#91;index] = value;<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public int get(int index) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  return arr&#91;index];<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  @Override<br>&nbsp; &nbsp;  protected ShallowCloneExample clone() throws CloneNotSupportedException {<br>&nbsp; &nbsp; &nbsp; &nbsp;  return (ShallowCloneExample) super.clone();<br>&nbsp; &nbsp;  }<br>&nbsp;}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;ShallowCloneExample e1 = new ShallowCloneExample();<br>&nbsp;ShallowCloneExample e2 = null;<br>&nbsp;try {<br>&nbsp; &nbsp;  e2 = e1.clone();<br>&nbsp;} catch (CloneNotSupportedException e) {<br>&nbsp; &nbsp;  e.printStackTrace();<br>&nbsp;}<br>&nbsp;e1.set(2, 222);<br>&nbsp;System.out.println(e2.get(2)); \/\/ 222<\/code><\/pre>\n\n\n\n<p><strong>3. \u6df1\u62f7\u8d1d<\/strong><\/p>\n\n\n\n<p>\u62f7\u8d1d\u5bf9\u8c61\u548c\u539f\u59cb\u5bf9\u8c61\u7684\u5f15\u7528\u7c7b\u578b\u5f15\u7528\u4e0d\u540c\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;public class DeepCloneExample implements Cloneable {<br>&nbsp;\u200b<br>&nbsp; &nbsp;  private int&#91;] arr;<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public DeepCloneExample() {<br>&nbsp; &nbsp; &nbsp; &nbsp;  arr = new int&#91;10];<br>&nbsp; &nbsp; &nbsp; &nbsp;  for (int i = 0; i &lt; arr.length; i++) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  arr&#91;i] = i;<br>&nbsp; &nbsp; &nbsp; &nbsp;  }<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public void set(int index, int value) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  arr&#91;index] = value;<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public int get(int index) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  return arr&#91;index];<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  @Override<br>&nbsp; &nbsp;  protected DeepCloneExample clone() throws CloneNotSupportedException {<br>&nbsp; &nbsp; &nbsp; &nbsp;  DeepCloneExample result = (DeepCloneExample) super.clone();<br>&nbsp; &nbsp; &nbsp; &nbsp;  result.arr = new int&#91;arr.length];<br>&nbsp; &nbsp; &nbsp; &nbsp;  for (int i = 0; i &lt; arr.length; i++) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  result.arr&#91;i] = arr&#91;i];<br>&nbsp; &nbsp; &nbsp; &nbsp;  }<br>&nbsp; &nbsp; &nbsp; &nbsp;  return result;<br>&nbsp; &nbsp;  }<br>&nbsp;}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;DeepCloneExample e1 = new DeepCloneExample();<br>&nbsp;DeepCloneExample e2 = null;<br>&nbsp;try {<br>&nbsp; &nbsp;  e2 = e1.clone();<br>&nbsp;} catch (CloneNotSupportedException e) {<br>&nbsp; &nbsp;  e.printStackTrace();<br>&nbsp;}<br>&nbsp;e1.set(2, 222);<br>&nbsp;System.out.println(e2.get(2)); \/\/ 2<\/code><\/pre>\n\n\n\n<p><strong>4. clone() \u7684\u66ff\u4ee3\u65b9\u6848<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528 clone() \u65b9\u6cd5\u6765\u62f7\u8d1d\u4e00\u4e2a\u5bf9\u8c61\u5373\u590d\u6742\u53c8\u6709\u98ce\u9669\uff0c\u5b83\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u5e76\u4e14\u8fd8\u9700\u8981\u7c7b\u578b\u8f6c\u6362\u3002Effective Java \u4e66\u4e0a\u8bb2\u5230\uff0c\u6700\u597d\u4e0d\u8981\u53bb\u4f7f\u7528 clone()\uff0c\u53ef\u4ee5\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6216\u8005\u62f7\u8d1d\u5de5\u5382\u6765\u62f7\u8d1d\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;public class CloneConstructorExample {<br>&nbsp;\u200b<br>&nbsp; &nbsp;  private int&#91;] arr;<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public CloneConstructorExample() {<br>&nbsp; &nbsp; &nbsp; &nbsp;  arr = new int&#91;10];<br>&nbsp; &nbsp; &nbsp; &nbsp;  for (int i = 0; i &lt; arr.length; i++) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  arr&#91;i] = i;<br>&nbsp; &nbsp; &nbsp; &nbsp;  }<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public CloneConstructorExample(CloneConstructorExample original) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  arr = new int&#91;original.arr.length];<br>&nbsp; &nbsp; &nbsp; &nbsp;  for (int i = 0; i &lt; original.arr.length; i++) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  arr&#91;i] = original.arr&#91;i];<br>&nbsp; &nbsp; &nbsp; &nbsp;  }<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public void set(int index, int value) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  arr&#91;index] = value;<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public int get(int index) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  return arr&#91;index];<br>&nbsp; &nbsp;  }<br>&nbsp;}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;CloneConstructorExample e1 = new CloneConstructorExample();<br>&nbsp;CloneConstructorExample e2 = new CloneConstructorExample(e1);<br>&nbsp;e1.set(2, 222);<br>&nbsp;System.out.println(e2.get(2)); \/\/ 2<\/code><\/pre>\n\n\n\n<h2>\u516d\u3001\u7ee7\u627f<\/h2>\n\n\n\n<h3>\u8bbf\u95ee\u6743\u9650<\/h3>\n\n\n\n<p>Java \u4e2d\u6709\u4e09\u4e2a\u8bbf\u95ee\u6743\u9650\u4fee\u9970\u7b26\uff1aprivate\u3001protected \u4ee5\u53ca public\uff0c\u5982\u679c\u4e0d\u52a0\u8bbf\u95ee\u4fee\u9970\u7b26\uff0c\u8868\u793a\u5305\u7ea7\u53ef\u89c1\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u5bf9\u7c7b\u6216\u7c7b\u4e2d\u7684\u6210\u5458\uff08\u5b57\u6bb5\u548c\u65b9\u6cd5\uff09\u52a0\u4e0a\u8bbf\u95ee\u4fee\u9970\u7b26\u3002<\/p>\n\n\n\n<ul><li>\u7c7b\u53ef\u89c1\u8868\u793a\u5176\u5b83\u7c7b\u53ef\u4ee5\u7528\u8fd9\u4e2a\u7c7b\u521b\u5efa\u5b9e\u4f8b\u5bf9\u8c61\u3002<\/li><li>\u6210\u5458\u53ef\u89c1\u8868\u793a\u5176\u5b83\u7c7b\u53ef\u4ee5\u7528\u8fd9\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u5bf9\u8c61\u8bbf\u95ee\u5230\u8be5\u6210\u5458\uff1b<\/li><\/ul>\n\n\n\n<p>protected \u7528\u4e8e\u4fee\u9970\u6210\u5458\uff0c\u8868\u793a\u5728\u7ee7\u627f\u4f53\u7cfb\u4e2d\u6210\u5458\u5bf9\u4e8e\u5b50\u7c7b\u53ef\u89c1\uff0c\u4f46\u662f\u8fd9\u4e2a\u8bbf\u95ee\u4fee\u9970\u7b26\u5bf9\u4e8e\u7c7b\u6ca1\u6709\u610f\u4e49\u3002<\/p>\n\n\n\n<p>\u8bbe\u8ba1\u826f\u597d\u7684\u6a21\u5757\u4f1a\u9690\u85cf\u6240\u6709\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u628a\u5b83\u7684 API \u4e0e\u5b83\u7684\u5b9e\u73b0\u6e05\u6670\u5730\u9694\u79bb\u5f00\u6765\u3002\u6a21\u5757\u4e4b\u95f4\u53ea\u901a\u8fc7\u5b83\u4eec\u7684 API \u8fdb\u884c\u901a\u4fe1\uff0c\u4e00\u4e2a\u6a21\u5757\u4e0d\u9700\u8981\u77e5\u9053\u5176\u4ed6\u6a21\u5757\u7684\u5185\u90e8\u5de5\u4f5c\u60c5\u51b5\uff0c\u8fd9\u4e2a\u6982\u5ff5\u88ab\u79f0\u4e3a\u4fe1\u606f\u9690\u85cf\u6216\u5c01\u88c5\u3002\u56e0\u6b64\u8bbf\u95ee\u6743\u9650\u5e94\u5f53\u5c3d\u53ef\u80fd\u5730\u4f7f\u6bcf\u4e2a\u7c7b\u6216\u8005\u6210\u5458\u4e0d\u88ab\u5916\u754c\u8bbf\u95ee\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u5b50\u7c7b\u7684\u65b9\u6cd5\u91cd\u5199\u4e86\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u90a3\u4e48\u5b50\u7c7b\u4e2d\u8be5\u65b9\u6cd5\u7684\u8bbf\u95ee\u7ea7\u522b\u4e0d\u5141\u8bb8\u4f4e\u4e8e\u7236\u7c7b\u7684\u8bbf\u95ee\u7ea7\u522b\u3002\u8fd9\u662f\u4e3a\u4e86\u786e\u4fdd\u53ef\u4ee5\u4f7f\u7528\u7236\u7c7b\u5b9e\u4f8b\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u4f7f\u7528\u5b50\u7c7b\u5b9e\u4f8b\u53bb\u4ee3\u66ff\uff0c\u4e5f\u5c31\u662f\u786e\u4fdd\u6ee1\u8db3\u91cc\u6c0f\u66ff\u6362\u539f\u5219\u3002<\/p>\n\n\n\n<p>\u5b57\u6bb5\u51b3\u4e0d\u80fd\u662f\u516c\u6709\u7684\uff0c\u56e0\u4e3a\u8fd9\u4e48\u505a\u7684\u8bdd\u5c31\u5931\u53bb\u4e86\u5bf9\u8fd9\u4e2a\u5b57\u6bb5\u4fee\u6539\u884c\u4e3a\u7684\u63a7\u5236\uff0c\u5ba2\u6237\u7aef\u53ef\u4ee5\u5bf9\u5176\u968f\u610f\u4fee\u6539\u3002\u4f8b\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cAccessExample \u62e5\u6709 id \u516c\u6709\u5b57\u6bb5\uff0c\u5982\u679c\u5728\u67d0\u4e2a\u65f6\u523b\uff0c\u6211\u4eec\u60f3\u8981\u4f7f\u7528 int \u5b58\u50a8 id \u5b57\u6bb5\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u4fee\u6539\u6240\u6709\u7684\u5ba2\u6237\u7aef\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;public class AccessExample {<br>&nbsp; &nbsp;  public String id;<br>&nbsp;}<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u516c\u6709\u7684 getter \u548c setter \u65b9\u6cd5\u6765\u66ff\u6362\u516c\u6709\u5b57\u6bb5\uff0c\u8fd9\u6837\u7684\u8bdd\u5c31\u53ef\u4ee5\u63a7\u5236\u5bf9\u5b57\u6bb5\u7684\u4fee\u6539\u884c\u4e3a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;public class AccessExample {<br>&nbsp;\u200b<br>&nbsp; &nbsp;  private int id;<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public String getId() {<br>&nbsp; &nbsp; &nbsp; &nbsp;  return id + \"\";<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public void setId(String id) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  this.id = Integer.valueOf(id);<br>&nbsp; &nbsp;  }<br>&nbsp;}<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\u4e5f\u6709\u4f8b\u5916\uff0c\u5982\u679c\u662f\u5305\u7ea7\u79c1\u6709\u7684\u7c7b\u6216\u8005\u79c1\u6709\u7684\u5d4c\u5957\u7c7b\uff0c\u90a3\u4e48\u76f4\u63a5\u66b4\u9732\u6210\u5458\u4e0d\u4f1a\u6709\u7279\u522b\u5927\u7684\u5f71\u54cd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;public class AccessWithInnerClassExample {<br>&nbsp;\u200b<br>&nbsp; &nbsp;  private class InnerClass {<br>&nbsp; &nbsp; &nbsp; &nbsp;  int x;<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  private InnerClass innerClass;<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public AccessWithInnerClassExample() {<br>&nbsp; &nbsp; &nbsp; &nbsp;  innerClass = new InnerClass();<br>&nbsp; &nbsp;  }<br>&nbsp;\u200b<br>&nbsp; &nbsp;  public int getValue() {<br>&nbsp; &nbsp; &nbsp; &nbsp;  return innerClass.x;  \/\/ \u76f4\u63a5\u8bbf\u95ee<br>&nbsp; &nbsp;  }<br>&nbsp;}<\/code><\/pre>\n\n\n\n<h3>\u62bd\u8c61\u7c7b\u4e0e\u63a5\u53e3<\/h3>\n\n\n\n<p><strong>1. \u62bd\u8c61\u7c7b<\/strong><\/p>\n\n\n\n<p>\u62bd\u8c61\u7c7b\u548c\u62bd\u8c61\u65b9\u6cd5\u90fd\u4f7f\u7528 abstract \u5173\u952e\u5b57\u8fdb\u884c\u58f0\u660e\u3002\u5982\u679c\u4e00\u4e2a\u7c7b\u4e2d\u5305\u542b\u62bd\u8c61\u65b9\u6cd5\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7c7b\u5fc5\u987b\u58f0\u660e\u4e3a\u62bd\u8c61\u7c7b\u3002<\/p>\n\n\n\n<p>\u62bd\u8c61\u7c7b\u548c\u666e\u901a\u7c7b\u6700\u5927\u7684\u533a\u522b\u662f\uff0c\u62bd\u8c61\u7c7b\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\uff0c\u53ea\u80fd\u88ab\u7ee7\u627f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u00a0public abstract class AbstractClassExample {\n\u00a0\u200b\n\u00a0 \u00a0  protected int x;\n\u00a0 \u00a0  private int y;\n\u00a0\u200b\n\u00a0 \u00a0  public abstract void func1();\n\u00a0\u200b\n\u00a0 \u00a0  public void func2() {\n\u00a0 \u00a0 \u00a0 \u00a0  System.out.println(\"func2\");\n\u00a0 \u00a0  }\n\u00a0}\n public class AbstractExtendClassExample extends AbstractClassExample {\r\n     @Override\r\n     public void func1() {\r\n         System.out.println(\"func1\");\r\n     }\r\n }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;\/\/ AbstractClassExample ac1 = new AbstractClassExample(); \/\/ 'AbstractClassExample' is abstract; cannot be instantiated<br>&nbsp;AbstractClassExample ac2 = new AbstractExtendClassExample();<br>&nbsp;ac2.func1();<\/code><\/pre>\n\n\n\n<p><strong>2. \u63a5\u53e3<\/strong><\/p>\n\n\n\n<p>\u63a5\u53e3\u662f\u62bd\u8c61\u7c7b\u7684\u5ef6\u4f38\uff0c\u5728 Java 8 \u4e4b\u524d\uff0c\u5b83\u53ef\u4ee5\u770b\u6210\u662f\u4e00\u4e2a\u5b8c\u5168\u62bd\u8c61\u7684\u7c7b\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u4e0d\u80fd\u6709\u4efb\u4f55\u7684\u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u4ece Java 8 \u5f00\u59cb\uff0c\u63a5\u53e3\u4e5f\u53ef\u4ee5\u62e5\u6709\u9ed8\u8ba4\u7684\u65b9\u6cd5\u5b9e\u73b0\uff0c\u8fd9\u662f\u56e0\u4e3a\u4e0d\u652f\u6301\u9ed8\u8ba4\u65b9\u6cd5\u7684\u63a5\u53e3\u7684\u7ef4\u62a4\u6210\u672c\u592a\u9ad8\u4e86\u3002\u5728 Java 8 \u4e4b\u524d\uff0c\u5982\u679c\u4e00\u4e2a\u63a5\u53e3\u60f3\u8981\u6dfb\u52a0\u65b0\u7684\u65b9\u6cd5\uff0c\u90a3\u4e48\u8981\u4fee\u6539\u6240\u6709\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u7684\u7c7b\uff0c\u8ba9\u5b83\u4eec\u90fd\u5b9e\u73b0\u65b0\u589e\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u63a5\u53e3\u7684\u6210\u5458\uff08\u5b57\u6bb5 + \u65b9\u6cd5\uff09\u9ed8\u8ba4\u90fd\u662f public \u7684\uff0c\u5e76\u4e14\u4e0d\u5141\u8bb8\u5b9a\u4e49\u4e3a private \u6216\u8005 protected\u3002\u4ece Java 9 \u5f00\u59cb\uff0c\u5141\u8bb8\u5c06\u65b9\u6cd5\u5b9a\u4e49\u4e3a private\uff0c\u8fd9\u6837\u5c31\u80fd\u5b9a\u4e49\u67d0\u4e9b\u590d\u7528\u7684\u4ee3\u7801\u53c8\u4e0d\u4f1a\u628a\u65b9\u6cd5\u66b4\u9732\u51fa\u53bb\u3002<\/p>\n\n\n\n<p>\u63a5\u53e3\u7684\u5b57\u6bb5\u9ed8\u8ba4\u90fd\u662f static \u548c final \u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u00a0public interface InterfaceExample {\n\u00a0\u200b\n\u00a0 \u00a0  void func1();\n\u00a0\u200b\n\u00a0 \u00a0  default void func2(){\n\u00a0 \u00a0 \u00a0 \u00a0  System.out.println(\"func2\");\n\u00a0 \u00a0  }\n\u00a0\u200b\n\u00a0 \u00a0  int x = 123;\n\u00a0 \u00a0  \/\/ int y; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Variable 'y' might not have been initialized\n\u00a0 \u00a0  public int z = 0; \u00a0 \u00a0 \u00a0 \/\/ Modifier 'public' is redundant for interface fields\n\u00a0 \u00a0  \/\/ private int k = 0; \u00a0 \/\/ Modifier 'private' not allowed here\n\u00a0 \u00a0  \/\/ protected int l = 0; \/\/ Modifier 'protected' not allowed here\n\u00a0 \u00a0  \/\/ private void fun3(); \/\/ Modifier 'private' not allowed here\n\u00a0}\n public class InterfaceImplementExample implements InterfaceExample {\r\n     @Override\r\n     public void func1() {\r\n         System.out.println(\"func1\");\r\n     }\r\n }\n \/\/ InterfaceExample ie1 = new InterfaceExample(); \/\/ 'InterfaceExample' is abstract; cannot be instantiated\r\n InterfaceExample ie2 = new InterfaceImplementExample();\r\n ie2.func1();\r\n System.out.println(InterfaceExample.x);<\/code><\/pre>\n\n\n\n<p><strong>3. \u6bd4\u8f83<\/strong><\/p>\n\n\n\n<ul><li>\u4ece\u8bbe\u8ba1\u5c42\u9762\u4e0a\u770b\uff0c\u62bd\u8c61\u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd IS-A \u5173\u7cfb\uff0c\u9700\u8981\u6ee1\u8db3\u91cc\u5f0f\u66ff\u6362\u539f\u5219\uff0c\u5373\u5b50\u7c7b\u5bf9\u8c61\u5fc5\u987b\u80fd\u591f\u66ff\u6362\u6389\u6240\u6709\u7236\u7c7b\u5bf9\u8c61\u3002\u800c\u63a5\u53e3\u66f4\u50cf\u662f\u4e00\u79cd LIKE-A \u5173\u7cfb\uff0c\u5b83\u53ea\u662f\u63d0\u4f9b\u4e00\u79cd\u65b9\u6cd5\u5b9e\u73b0\u5951\u7ea6\uff0c\u5e76\u4e0d\u8981\u6c42\u63a5\u53e3\u548c\u5b9e\u73b0\u63a5\u53e3\u7684\u7c7b\u5177\u6709 IS-A \u5173\u7cfb\u3002<\/li><li>\u4ece\u4f7f\u7528\u4e0a\u6765\u770b\uff0c\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u5b9e\u73b0\u591a\u4e2a\u63a5\u53e3\uff0c\u4f46\u662f\u4e0d\u80fd\u7ee7\u627f\u591a\u4e2a\u62bd\u8c61\u7c7b\u3002<\/li><li>\u63a5\u53e3\u7684\u5b57\u6bb5\u53ea\u80fd\u662f static \u548c final \u7c7b\u578b\u7684\uff0c\u800c\u62bd\u8c61\u7c7b\u7684\u5b57\u6bb5\u6ca1\u6709\u8fd9\u79cd\u9650\u5236\u3002<\/li><li>\u63a5\u53e3\u7684\u6210\u5458\u53ea\u80fd\u662f public \u7684\uff0c\u800c\u62bd\u8c61\u7c7b\u7684\u6210\u5458\u53ef\u4ee5\u6709\u591a\u79cd\u8bbf\u95ee\u6743\u9650\u3002<\/li><\/ul>\n\n\n\n<p><strong>4. \u4f7f\u7528\u9009\u62e9<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528\u63a5\u53e3\uff1a<\/p>\n\n\n\n<ul><li>\u9700\u8981\u8ba9\u4e0d\u76f8\u5173\u7684\u7c7b\u90fd\u5b9e\u73b0\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4f8b\u5982\u4e0d\u76f8\u5173\u7684\u7c7b\u90fd\u53ef\u4ee5\u5b9e\u73b0 Comparable \u63a5\u53e3\u4e2d\u7684 compareTo() \u65b9\u6cd5\uff1b<\/li><li>\u9700\u8981\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\u3002<\/li><\/ul>\n\n\n\n<p>\u4f7f\u7528\u62bd\u8c61\u7c7b\uff1a<\/p>\n\n\n\n<ul><li>\u9700\u8981\u5728\u51e0\u4e2a\u76f8\u5173\u7684\u7c7b\u4e2d\u5171\u4eab\u4ee3\u7801\u3002<\/li><li>\u9700\u8981\u80fd\u63a7\u5236\u7ee7\u627f\u6765\u7684\u6210\u5458\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u800c\u4e0d\u662f\u90fd\u4e3a public\u3002<\/li><li>\u9700\u8981\u7ee7\u627f\u975e\u9759\u6001\u548c\u975e\u5e38\u91cf\u5b57\u6bb5\u3002<\/li><\/ul>\n\n\n\n<p>\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u63a5\u53e3\u4f18\u5148\u4e8e\u62bd\u8c61\u7c7b\u3002\u56e0\u4e3a\u63a5\u53e3\u6ca1\u6709\u62bd\u8c61\u7c7b\u4e25\u683c\u7684\u7c7b\u5c42\u6b21\u7ed3\u6784\u8981\u6c42\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u4e3a\u4e00\u4e2a\u7c7b\u6dfb\u52a0\u884c\u4e3a\u3002\u5e76\u4e14\u4ece Java 8 \u5f00\u59cb\uff0c\u63a5\u53e3\u4e5f\u53ef\u4ee5\u6709\u9ed8\u8ba4\u7684\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4f7f\u5f97\u4fee\u6539\u63a5\u53e3\u7684\u6210\u672c\u4e5f\u53d8\u7684\u5f88\u4f4e\u3002<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/IandI\/abstract.html\">Abstract Methods and Classes<\/a><\/li><li><a href=\"https:\/\/www.ibm.com\/developerworks\/cn\/java\/l-javainterface-abstract\/\">\u6df1\u5165\u7406\u89e3 abstract class \u548c interface<\/a><\/li><li><a href=\"https:\/\/dzone.com\/articles\/when-to-use-abstract-class-and-intreface\">When to Use Abstract Class and Interface<\/a><\/li><li><a href=\"https:\/\/www.journaldev.com\/12850\/java-9-private-methods-interfaces\">Java 9 Private Methods in Interfaces<\/a><\/li><\/ul>\n\n\n\n<h3>super<\/h3>\n\n\n\n<ul><li>\u8bbf\u95ee\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff1a\u53ef\u4ee5\u4f7f\u7528 super() \u51fd\u6570\u8bbf\u95ee\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u4ece\u800c\u59d4\u6258\u7236\u7c7b\u5b8c\u6210\u4e00\u4e9b\u521d\u59cb\u5316\u7684\u5de5\u4f5c\u3002\u5e94\u8be5\u6ce8\u610f\u5230\uff0c\u5b50\u7c7b\u4e00\u5b9a\u4f1a\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u6765\u5b8c\u6210\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u4e00\u822c\u662f\u8c03\u7528\u7236\u7c7b\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5982\u679c\u5b50\u7c7b\u9700\u8981\u8c03\u7528\u7236\u7c7b\u5176\u5b83\u6784\u9020\u51fd\u6570\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u4f7f\u7528 super() \u51fd\u6570\u3002<\/li><li>\u8bbf\u95ee\u7236\u7c7b\u7684\u6210\u5458\uff1a\u5982\u679c\u5b50\u7c7b\u91cd\u5199\u4e86\u7236\u7c7b\u7684\u67d0\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 super \u5173\u952e\u5b57\u6765\u5f15\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u5b9e\u73b0\u3002<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;public class SuperExample {\n&nbsp;\u200b\n&nbsp; &nbsp;  protected int x;\n&nbsp; &nbsp;  protected int y;\n&nbsp;\u200b\n&nbsp; &nbsp;  public SuperExample(int x, int y) {\n&nbsp; &nbsp; &nbsp; &nbsp;  this.x = x;\n&nbsp; &nbsp; &nbsp; &nbsp;  this.y = y;\n&nbsp; &nbsp;  }\n&nbsp;\u200b\n&nbsp; &nbsp;  public void func() {\n&nbsp; &nbsp; &nbsp; &nbsp;  System.out.println(\"SuperExample.func()\");\n&nbsp; &nbsp;  }\n&nbsp;}\n public class SuperExtendExample extends SuperExample { \u200b\n     private int z;\n \u200b\n     public SuperExtendExample(int x, int y, int z) {\n         super(x, y);\n         this.z = z;\n     }\n \u200b\n     @Override\n     public void func() {\n         super.func();\n         System.out.println(\"SuperExtendExample.func()\");\n     }\n }\n     private int z;\n \u200b\n     public SuperExtendExample(int x, int y, int z) {\n         super(x, y);\n         this.z = z;\n     }\n \u200b\n     @Override\n     public void func() {\n         super.func();\n         System.out.println(\"SuperExtendExample.func()\");\n     }\n }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;SuperExample e = new SuperExtendExample(1, 2, 3);<br>&nbsp;e.func();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;SuperExample.func()<br>&nbsp;SuperExtendExample.func()<\/pre>\n\n\n\n<p><a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/IandI\/super.html\">Using the Keyword super<\/a><\/p>\n\n\n\n<h3>\u91cd\u5199\u4e0e\u91cd\u8f7d<\/h3>\n\n\n\n<p><strong>1. \u91cd\u5199\uff08Override\uff09<\/strong><\/p>\n\n\n\n<p>\u5b58\u5728\u4e8e\u7ee7\u627f\u4f53\u7cfb\u4e2d\uff0c\u6307\u5b50\u7c7b\u5b9e\u73b0\u4e86\u4e00\u4e2a\u4e0e\u7236\u7c7b\u5728\u65b9\u6cd5\u58f0\u660e\u4e0a\u5b8c\u5168\u76f8\u540c\u7684\u4e00\u4e2a\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u6ee1\u8db3\u91cc\u5f0f\u66ff\u6362\u539f\u5219\uff0c\u91cd\u5199\u6709\u4ee5\u4e0b\u4e09\u4e2a\u9650\u5236\uff1a<\/p>\n\n\n\n<ul><li>\u5b50\u7c7b\u65b9\u6cd5\u7684\u8bbf\u95ee\u6743\u9650\u5fc5\u987b\u5927\u4e8e\u7b49\u4e8e\u7236\u7c7b\u65b9\u6cd5\uff1b<\/li><li>\u5b50\u7c7b\u65b9\u6cd5\u7684\u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u662f\u7236\u7c7b\u65b9\u6cd5\u8fd4\u56de\u7c7b\u578b\u6216\u4e3a\u5176\u5b50\u7c7b\u578b\u3002<\/li><li>\u5b50\u7c7b\u65b9\u6cd5\u629b\u51fa\u7684\u5f02\u5e38\u7c7b\u578b\u5fc5\u987b\u662f\u7236\u7c7b\u629b\u51fa\u5f02\u5e38\u7c7b\u578b\u6216\u4e3a\u5176\u5b50\u7c7b\u578b\u3002<\/li><\/ul>\n\n\n\n<p>\u4f7f\u7528 @Override \u6ce8\u89e3\uff0c\u53ef\u4ee5\u8ba9\u7f16\u8bd1\u5668\u5e2e\u5fd9\u68c0\u67e5\u662f\u5426\u6ee1\u8db3\u4e0a\u9762\u7684\u4e09\u4e2a\u9650\u5236\u6761\u4ef6\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0cSubClass \u4e3a SuperClass \u7684\u5b50\u7c7b\uff0cSubClass \u91cd\u5199\u4e86 SuperClass \u7684 func() \u65b9\u6cd5\u3002\u5176\u4e2d\uff1a<\/p>\n\n\n\n<ul><li>\u5b50\u7c7b\u65b9\u6cd5\u8bbf\u95ee\u6743\u9650\u4e3a public\uff0c\u5927\u4e8e\u7236\u7c7b\u7684 protected\u3002<\/li><li>\u5b50\u7c7b\u7684\u8fd4\u56de\u7c7b\u578b\u4e3a ArrayList&lt;Integer&gt;\uff0c\u662f\u7236\u7c7b\u8fd4\u56de\u7c7b\u578b List&lt;Integer&gt; \u7684\u5b50\u7c7b\u3002<\/li><li>\u5b50\u7c7b\u629b\u51fa\u7684\u5f02\u5e38\u7c7b\u578b\u4e3a Exception\uff0c\u662f\u7236\u7c7b\u629b\u51fa\u5f02\u5e38 Throwable \u7684\u5b50\u7c7b\u3002<\/li><li>\u5b50\u7c7b\u91cd\u5199\u65b9\u6cd5\u4f7f\u7528 @Override \u6ce8\u89e3\uff0c\u4ece\u800c\u8ba9\u7f16\u8bd1\u5668\u81ea\u52a8\u68c0\u67e5\u662f\u5426\u6ee1\u8db3\u9650\u5236\u6761\u4ef6\u3002<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;class SuperClass {<br>&nbsp; &nbsp;  protected List&lt;Integer&gt; func() throws Throwable {<br>&nbsp; &nbsp; &nbsp; &nbsp;  return new ArrayList&lt;&gt;();<br>&nbsp; &nbsp;  }<br>&nbsp;}<br>&nbsp;\u200b<br>&nbsp;class SubClass extends SuperClass {<br>&nbsp; &nbsp;  @Override<br>&nbsp; &nbsp;  public ArrayList&lt;Integer&gt; func() throws Exception {<br>&nbsp; &nbsp; &nbsp; &nbsp;  return new ArrayList&lt;&gt;();<br>&nbsp; &nbsp;  }<br>&nbsp;}<\/code><\/pre>\n\n\n\n<p>\u5728\u8c03\u7528\u4e00\u4e2a\u65b9\u6cd5\u65f6\uff0c\u5148\u4ece\u672c\u7c7b\u4e2d\u67e5\u627e\u770b\u662f\u5426\u6709\u5bf9\u5e94\u7684\u65b9\u6cd5\uff0c\u5982\u679c\u6ca1\u6709\u518d\u5230\u7236\u7c7b\u4e2d\u67e5\u770b\uff0c\u770b\u662f\u5426\u4ece\u7236\u7c7b\u7ee7\u627f\u6765\u3002\u5426\u5219\u5c31\u8981\u5bf9\u53c2\u6570\u8fdb\u884c\u8f6c\u578b\uff0c\u8f6c\u6210\u7236\u7c7b\u4e4b\u540e\u770b\u662f\u5426\u6709\u5bf9\u5e94\u7684\u65b9\u6cd5\u3002\u603b\u7684\u6765\u8bf4\uff0c\u65b9\u6cd5\u8c03\u7528\u7684\u4f18\u5148\u7ea7\u4e3a\uff1a<\/p>\n\n\n\n<ul><li>this.func(this)<\/li><li>super.func(this)<\/li><li>this.func(super)<\/li><li>super.func(super)<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class A {\n\n    public void show(A obj) {\n        System.out.println(\"A.show(A)\");\n    }\n\n    public void show(C obj) {\n        System.out.println(\"A.show(C)\");\n    }\n}\n\nclass B extends A {\n\n    @Override\n    public void show(A obj) {\n        System.out.println(\"B.show(A)\");\n    }\n}\n\nclass C extends B {\n}\n\nclass D extends C {\n}\npublic static void main(String&#91;] args) {\n\n    A a = new A();\n    B b = new B();\n    C c = new C();\n    D d = new D();\n\n    \/\/ \u5728 A \u4e2d\u5b58\u5728 show(A obj)\uff0c\u76f4\u63a5\u8c03\u7528\n    a.show(a); \/\/ A.show(A)\n    \/\/ \u5728 A \u4e2d\u4e0d\u5b58\u5728 show(B obj)\uff0c\u5c06 B \u8f6c\u578b\u6210\u5176\u7236\u7c7b A\n    a.show(b); \/\/ A.show(A)\n    \/\/ \u5728 B \u4e2d\u5b58\u5728\u4ece A \u7ee7\u627f\u6765\u7684 show(C obj)\uff0c\u76f4\u63a5\u8c03\u7528\n    b.show(c); \/\/ A.show(C)\n    \/\/ \u5728 B \u4e2d\u4e0d\u5b58\u5728 show(D obj)\uff0c\u4f46\u662f\u5b58\u5728\u4ece A \u7ee7\u627f\u6765\u7684 show(C obj)\uff0c\u5c06 D \u8f6c\u578b\u6210\u5176\u7236\u7c7b C\n    b.show(d); \/\/ A.show(C)\n\n    \/\/ \u5f15\u7528\u7684\u8fd8\u662f B \u5bf9\u8c61\uff0c\u6240\u4ee5 ba \u548c b \u7684\u8c03\u7528\u7ed3\u679c\u4e00\u6837\n    A ba = new B();\n    ba.show(c); \/\/ A.show(C)\n    ba.show(d); \/\/ A.show(C)\n}<\/code><\/pre>\n\n\n\n<p><strong>2. \u91cd\u8f7d\uff08Overload\uff09<\/strong><\/p>\n\n\n\n<p>\u5b58\u5728\u4e8e\u540c\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u6307\u4e00\u4e2a\u65b9\u6cd5\u4e0e\u5df2\u7ecf\u5b58\u5728\u7684\u65b9\u6cd5\u540d\u79f0\u4e0a\u76f8\u540c\uff0c\u4f46\u662f\u53c2\u6570\u7c7b\u578b\u3001\u4e2a\u6570\u3001\u987a\u5e8f\u81f3\u5c11\u6709\u4e00\u4e2a\u4e0d\u540c\u3002<\/p>\n\n\n\n<p>\u5e94\u8be5\u6ce8\u610f\u7684\u662f\uff0c\u8fd4\u56de\u503c\u4e0d\u540c\uff0c\u5176\u5b83\u90fd\u76f8\u540c\u4e0d\u7b97\u662f\u91cd\u8f7d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;class OverloadingExample {\n&nbsp; &nbsp;  public void show(int x) {\n&nbsp; &nbsp; &nbsp; &nbsp;  System.out.println(x);\n&nbsp; &nbsp;  }\n&nbsp;\u200b\n&nbsp; &nbsp;  public void show(int x, String y) {\n&nbsp; &nbsp; &nbsp; &nbsp;  System.out.println(x + \" \" + y);\n&nbsp; &nbsp;  }\n&nbsp;}\n public static void main(String&#91;] args) {\n     OverloadingExample example = new OverloadingExample();\n     example.show(1);\n     example.show(1, \"2\");\n }<\/code><\/pre>\n\n\n\n<h2>\u53cd\u5c04<\/h2>\n\n\n\n<p>\u6bcf\u4e2a\u7c7b\u90fd\u6709\u4e00\u4e2a <strong>Class<\/strong> \u5bf9\u8c61\uff0c\u5305\u542b\u4e86\u4e0e\u7c7b\u6709\u5173\u7684\u4fe1\u606f\u3002\u5f53\u7f16\u8bd1\u4e00\u4e2a\u65b0\u7c7b\u65f6\uff0c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u540c\u540d\u7684 .class \u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u5185\u5bb9\u4fdd\u5b58\u7740 Class \u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u7c7b\u52a0\u8f7d\u76f8\u5f53\u4e8e Class \u5bf9\u8c61\u7684\u52a0\u8f7d\uff0c\u7c7b\u5728\u7b2c\u4e00\u6b21\u4f7f\u7528\u65f6\u624d\u52a8\u6001\u52a0\u8f7d\u5230 JVM \u4e2d\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>Class.forName(\"com.mysql.jdbc.Driver\")<\/code> \u8fd9\u79cd\u65b9\u5f0f\u6765\u63a7\u5236\u7c7b\u7684\u52a0\u8f7d\uff0c\u8be5\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a Class \u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u53cd\u5c04\u53ef\u4ee5\u63d0\u4f9b\u8fd0\u884c\u65f6\u7684\u7c7b\u4fe1\u606f\uff0c\u5e76\u4e14\u8fd9\u4e2a\u7c7b\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u624d\u52a0\u8f7d\u8fdb\u6765\uff0c\u751a\u81f3\u5728\u7f16\u8bd1\u65f6\u671f\u8be5\u7c7b\u7684 .class \u4e0d\u5b58\u5728\u4e5f\u53ef\u4ee5\u52a0\u8f7d\u8fdb\u6765\u3002<\/p>\n\n\n\n<p>Class \u548c java.lang.reflect \u4e00\u8d77\u5bf9\u53cd\u5c04\u63d0\u4f9b\u4e86\u652f\u6301\uff0cjava.lang.reflect \u7c7b\u5e93\u4e3b\u8981\u5305\u542b\u4e86\u4ee5\u4e0b\u4e09\u4e2a\u7c7b\uff1a<\/p>\n\n\n\n<ul><li><strong>Field<\/strong> \uff1a\u53ef\u4ee5\u4f7f\u7528 get() \u548c set() \u65b9\u6cd5\u8bfb\u53d6\u548c\u4fee\u6539 Field \u5bf9\u8c61\u5173\u8054\u7684\u5b57\u6bb5\uff1b<\/li><li><strong>Method<\/strong> \uff1a\u53ef\u4ee5\u4f7f\u7528 invoke() \u65b9\u6cd5\u8c03\u7528\u4e0e Method \u5bf9\u8c61\u5173\u8054\u7684\u65b9\u6cd5\uff1b<\/li><li><strong>Constructor<\/strong> \uff1a\u53ef\u4ee5\u7528 Constructor \u7684 newInstance() \u521b\u5efa\u65b0\u7684\u5bf9\u8c61\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u53cd\u5c04\u7684\u4f18\u70b9\uff1a<\/strong><\/p>\n\n\n\n<ul><li><strong>\u53ef\u6269\u5c55\u6027<\/strong> \uff1a\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u5229\u7528\u5168\u9650\u5b9a\u540d\u521b\u5efa\u53ef\u6269\u5c55\u5bf9\u8c61\u7684\u5b9e\u4f8b\uff0c\u6765\u4f7f\u7528\u6765\u81ea\u5916\u90e8\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u3002<\/li><li><strong>\u7c7b\u6d4f\u89c8\u5668\u548c\u53ef\u89c6\u5316\u5f00\u53d1\u73af\u5883<\/strong> \uff1a\u4e00\u4e2a\u7c7b\u6d4f\u89c8\u5668\u9700\u8981\u53ef\u4ee5\u679a\u4e3e\u7c7b\u7684\u6210\u5458\u3002\u53ef\u89c6\u5316\u5f00\u53d1\u73af\u5883\uff08\u5982 IDE\uff09\u53ef\u4ee5\u4ece\u5229\u7528\u53cd\u5c04\u4e2d\u53ef\u7528\u7684\u7c7b\u578b\u4fe1\u606f\u4e2d\u53d7\u76ca\uff0c\u4ee5\u5e2e\u52a9\u7a0b\u5e8f\u5458\u7f16\u5199\u6b63\u786e\u7684\u4ee3\u7801\u3002<\/li><li><strong>\u8c03\u8bd5\u5668\u548c\u6d4b\u8bd5\u5de5\u5177<\/strong> \uff1a \u8c03\u8bd5\u5668\u9700\u8981\u80fd\u591f\u68c0\u67e5\u4e00\u4e2a\u7c7b\u91cc\u7684\u79c1\u6709\u6210\u5458\u3002\u6d4b\u8bd5\u5de5\u5177\u53ef\u4ee5\u5229\u7528\u53cd\u5c04\u6765\u81ea\u52a8\u5730\u8c03\u7528\u7c7b\u91cc\u5b9a\u4e49\u7684\u53ef\u88ab\u53d1\u73b0\u7684 API \u5b9a\u4e49\uff0c\u4ee5\u786e\u4fdd\u4e00\u7ec4\u6d4b\u8bd5\u4e2d\u6709\u8f83\u9ad8\u7684\u4ee3\u7801\u8986\u76d6\u7387\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u53cd\u5c04\u7684\u7f3a\u70b9\uff1a<\/strong><\/p>\n\n\n\n<p>\u5c3d\u7ba1\u53cd\u5c04\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u4e5f\u4e0d\u80fd\u6ee5\u7528\u3002\u5982\u679c\u4e00\u4e2a\u529f\u80fd\u53ef\u4ee5\u4e0d\u7528\u53cd\u5c04\u5b8c\u6210\uff0c\u90a3\u4e48\u6700\u597d\u5c31\u4e0d\u7528\u3002\u5728\u6211\u4eec\u4f7f\u7528\u53cd\u5c04\u6280\u672f\u65f6\uff0c\u4e0b\u9762\u51e0\u6761\u5185\u5bb9\u5e94\u8be5\u7262\u8bb0\u4e8e\u5fc3\u3002<\/p>\n\n\n\n<ul><li><strong>\u6027\u80fd\u5f00\u9500<\/strong> \uff1a\u53cd\u5c04\u6d89\u53ca\u4e86\u52a8\u6001\u7c7b\u578b\u7684\u89e3\u6790\uff0c\u6240\u4ee5 JVM \u65e0\u6cd5\u5bf9\u8fd9\u4e9b\u4ee3\u7801\u8fdb\u884c\u4f18\u5316\u3002\u56e0\u6b64\uff0c\u53cd\u5c04\u64cd\u4f5c\u7684\u6548\u7387\u8981\u6bd4\u90a3\u4e9b\u975e\u53cd\u5c04\u64cd\u4f5c\u4f4e\u5f97\u591a\u3002\u6211\u4eec\u5e94\u8be5\u907f\u514d\u5728\u7ecf\u5e38\u88ab\u6267\u884c\u7684\u4ee3\u7801\u6216\u5bf9\u6027\u80fd\u8981\u6c42\u5f88\u9ad8\u7684\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u53cd\u5c04\u3002<\/li><li><strong>\u5b89\u5168\u9650\u5236<\/strong> \uff1a\u4f7f\u7528\u53cd\u5c04\u6280\u672f\u8981\u6c42\u7a0b\u5e8f\u5fc5\u987b\u5728\u4e00\u4e2a\u6ca1\u6709\u5b89\u5168\u9650\u5236\u7684\u73af\u5883\u4e2d\u8fd0\u884c\u3002\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u5fc5\u987b\u5728\u6709\u5b89\u5168\u9650\u5236\u7684\u73af\u5883\u4e2d\u8fd0\u884c\uff0c\u5982 Applet\uff0c\u90a3\u4e48\u8fd9\u5c31\u662f\u4e2a\u95ee\u9898\u4e86\u3002<\/li><li><strong>\u5185\u90e8\u66b4\u9732<\/strong> \uff1a\u7531\u4e8e\u53cd\u5c04\u5141\u8bb8\u4ee3\u7801\u6267\u884c\u4e00\u4e9b\u5728\u6b63\u5e38\u60c5\u51b5\u4e0b\u4e0d\u88ab\u5141\u8bb8\u7684\u64cd\u4f5c\uff08\u6bd4\u5982\u8bbf\u95ee\u79c1\u6709\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff09\uff0c\u6240\u4ee5\u4f7f\u7528\u53cd\u5c04\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u6599\u4e4b\u5916\u7684\u526f\u4f5c\u7528\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u529f\u80fd\u5931\u8c03\u5e76\u7834\u574f\u53ef\u79fb\u690d\u6027\u3002\u53cd\u5c04\u4ee3\u7801\u7834\u574f\u4e86\u62bd\u8c61\u6027\uff0c\u56e0\u6b64\u5f53\u5e73\u53f0\u53d1\u751f\u6539\u53d8\u7684\u65f6\u5019\uff0c\u4ee3\u7801\u7684\u884c\u4e3a\u5c31\u6709\u53ef\u80fd\u4e5f\u968f\u7740\u53d8\u5316\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1.\u6570\u636e\u7c7b\u578b\u7684\u5206\u7c7b *\u5f15\u7528\u6570\u636e\u7c7b\u578b\uff08\u9664\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e4b\u5916\u7684\uff0c\u5982String\uff09 *\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff1a4\u5927\u7c7b8\u79cd \u6570\u636e\u7c7b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[18,13],"tags":[20],"_links":{"self":[{"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/posts\/194"}],"collection":[{"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=194"}],"version-history":[{"count":4,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/posts\/194\/revisions"}],"predecessor-version":[{"id":643,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/posts\/194\/revisions\/643"}],"wp:attachment":[{"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}