变量与方法


一、变量详解

1.1 变量分类

<mxfile>
  <diagram id="variable-classification" name="变量分类">
    <mxGraphModel dx="1337" dy="572" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="680" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="650" width="820" x="40" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=28;fontStyle=1;fontColor=#303f9f;" value="变量分类" vertex="1">
          <mxGeometry height="50" width="200" x="350" y="40" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3f51b5;strokeColor=#303f9f;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="60" width="140" x="380" y="110" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 18px;&quot;&gt;&lt;b&gt;变量&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="140" x="380" y="110" as="geometry" />
        </mxCell>
        <mxCell id="6" edge="1" parent="1" style="endArrow=none;html=1;strokeColor=#5c6bc0;strokeWidth=2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="450" y="170" as="sourcePoint" />
            <mxPoint x="450" y="210" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="7" edge="1" parent="1" style="endArrow=none;html=1;strokeColor=#5c6bc0;strokeWidth=2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="150" y="210" as="sourcePoint" />
            <mxPoint x="750" y="210" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="8" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#5c6bc0;strokeWidth=2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="150" y="210" as="sourcePoint" />
            <mxPoint x="150" y="260" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="9" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#5c6bc0;strokeWidth=2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="450" y="210" as="sourcePoint" />
            <mxPoint x="450" y="260" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="10" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#5c6bc0;strokeWidth=2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="750" y="210" as="sourcePoint" />
            <mxPoint x="750" y="260" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="70" width="180" x="60" y="260" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font style=&quot;font-size: 17px;&quot; color=&quot;#1565c0&quot;&gt;&lt;b&gt;局部变量&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="70" width="180" x="60" y="260" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="290" width="180" x="60" y="350" as="geometry" />
        </mxCell>
        <mxCell id="14" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#1976d2;strokeWidth=2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="150" y="330" as="sourcePoint" />
            <mxPoint x="150" y="350" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="30" width="180" x="60" y="350" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;📍 定义位置&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="60" y="350" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="方法内部&lt;br&gt;代码块内部&lt;br&gt;构造器内部" vertex="1">
          <mxGeometry height="55" width="180" x="60" y="380" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="30" width="180" x="60" y="440" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;🏷️ 修饰符&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="60" y="440" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;无修饰符&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 11px;&quot;&gt;(不能用访问修饰符)&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="180" x="60" y="470" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="30" width="180" x="60" y="515" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;⏱️ 生命周期&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="60" y="515" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="方法调用时创建&lt;br&gt;方法结束时销毁" vertex="1">
          <mxGeometry height="40" width="180" x="60" y="545" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="30" width="180" x="60" y="590" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;💾 存储位置&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="60" y="590" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;栈内存&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="180" x="60" y="620" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ff9800;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="70" width="180" x="360" y="260" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font style=&quot;font-size: 17px;&quot; color=&quot;#e65100&quot;&gt;&lt;b&gt;成员变量&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 12px;&quot;&gt;(实例变量)&lt;/font&gt;" vertex="1">
          <mxGeometry height="70" width="180" x="360" y="260" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ff9800;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="290" width="180" x="360" y="350" as="geometry" />
        </mxCell>
        <mxCell id="30" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#ff9800;strokeWidth=2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="450" y="330" as="sourcePoint" />
            <mxPoint x="450" y="350" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="30" width="180" x="360" y="350" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;📍 定义位置&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="360" y="350" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="类中&lt;br&gt;方法外" vertex="1">
          <mxGeometry height="55" width="180" x="360" y="380" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="30" width="180" x="360" y="440" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;🏷️ 修饰符&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="360" y="440" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;无 static&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 11px;&quot;&gt;(可用访问修饰符)&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="180" x="360" y="470" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="30" width="180" x="360" y="515" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;⏱️ 生命周期&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="360" y="515" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="对象创建时创建&lt;br&gt;对象销毁时销毁" vertex="1">
          <mxGeometry height="40" width="180" x="360" y="545" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="30" width="180" x="360" y="590" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;💾 存储位置&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="360" y="590" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#e65100&quot;&gt;&lt;b&gt;堆内存&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="180" x="360" y="620" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="70" width="180" x="660" y="260" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font style=&quot;font-size: 17px;&quot; color=&quot;#2e7d32&quot;&gt;&lt;b&gt;类变量&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 12px;&quot;&gt;(静态变量)&lt;/font&gt;" vertex="1">
          <mxGeometry height="70" width="180" x="660" y="260" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="290" width="180" x="660" y="350" as="geometry" />
        </mxCell>
        <mxCell id="46" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#4caf50;strokeWidth=2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="750" y="330" as="sourcePoint" />
            <mxPoint x="750" y="350" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="47" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="30" width="180" x="660" y="350" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;📍 定义位置&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="660" y="350" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="类中&lt;br&gt;方法外" vertex="1">
          <mxGeometry height="55" width="180" x="660" y="380" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="30" width="180" x="660" y="440" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;🏷️ 修饰符&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="660" y="440" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;有 static&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 11px;&quot;&gt;(可用访问修饰符)&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="180" x="660" y="470" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="30" width="180" x="660" y="515" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;⏱️ 生命周期&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="660" y="515" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="类加载时创建&lt;br&gt;类卸载时销毁" vertex="1">
          <mxGeometry height="40" width="180" x="660" y="545" as="geometry" />
        </mxCell>
        <mxCell id="56" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="30" width="180" x="660" y="590" as="geometry" />
        </mxCell>
        <mxCell id="57" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;💾 存储位置&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="660" y="590" as="geometry" />
        </mxCell>
        <mxCell id="58" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;方法区/元空间&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="180" x="660" y="620" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

Java

public class VariableDemo {

    // ═══════════════════════════════════════════════════════════════════
    // 类变量(静态变量):属于类,所有实例共享
    // ═══════════════════════════════════════════════════════════════════
    private static int classCount = 0;      // 类变量
    public static final String APP_NAME = "MyApp";  // 静态常量

    // ═══════════════════════════════════════════════════════════════════
    // 成员变量(实例变量):属于对象,每个实例独立
    // ═══════════════════════════════════════════════════════════════════
    private String name;     // 实例变量
    private int age;         // 实例变量

    public void method(int param) {  // param 是局部变量
        // ═══════════════════════════════════════════════════════════════
        // 局部变量:定义在方法内部
        // ═══════════════════════════════════════════════════════════════
        int localVar = 10;   // 局部变量
        String message;      // 局部变量(未初始化,使用前必须赋值)

        for (int i = 0; i < 10; i++) {  // i 也是局部变量
            // ...
        }

        // 代码块内的局部变量
        {
            int blockVar = 20;  // 作用域仅限于这个代码块
        }
        // blockVar 在这里不可见
    }
}

1.2 三种变量详细对比

<mxfile>
  <diagram id="variable-comparison" name="变量对比表">
    <mxGraphModel dx="1614" dy="690" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="750" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="600" width="750" x="30" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;fontStyle=1;fontColor=#303f9f;" value="局部变量 vs 成员变量 vs 静态变量" vertex="1">
          <mxGeometry height="40" width="600" x="105" y="35" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#3f51b5;strokeColor=#303f9f;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="120" x="50" y="90" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;对比项&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="120" x="50" y="90" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=#1565c0;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="170" y="90" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;局部变量&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="200" x="170" y="90" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=#f57c00;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="370" y="90" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;成员变量&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="200" x="370" y="90" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=#388e3c;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="570" y="90" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;静态变量&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="200" x="570" y="90" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="120" x="50" y="140" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#3f51b5;" value="&lt;b&gt;📍 定义位置&lt;/b&gt;" vertex="1">
          <mxGeometry height="50" width="120" x="50" y="140" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="170" y="140" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="方法内/代码块内" vertex="1">
          <mxGeometry height="50" width="200" x="170" y="140" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="370" y="140" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="类中,方法外" vertex="1">
          <mxGeometry height="50" width="200" x="370" y="140" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="570" y="140" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="类中,方法外" vertex="1">
          <mxGeometry height="50" width="200" x="570" y="140" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="70" width="120" x="50" y="190" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#3f51b5;" value="&lt;b&gt;🏷️ 修饰符&lt;/b&gt;" vertex="1">
          <mxGeometry height="70" width="120" x="50" y="190" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="70" width="200" x="170" y="190" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#424242;" value="&lt;font color=&quot;#c62828&quot;&gt;不能用访问修饰符&lt;/font&gt;&lt;br&gt;可用 &lt;font face=&quot;Courier New&quot; color=&quot;#1565c0&quot;&gt;&lt;b&gt;final&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="70" width="200" x="170" y="190" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="70" width="200" x="370" y="190" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#424242;" value="可用&lt;br&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#e65100&quot;&gt;&lt;b&gt;private/public&lt;/b&gt;&lt;/font&gt; 等" vertex="1">
          <mxGeometry height="70" width="200" x="370" y="190" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="70" width="200" x="570" y="190" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#424242;" value="必须有 &lt;font face=&quot;Courier New&quot; color=&quot;#2e7d32&quot;&gt;&lt;b&gt;static&lt;/b&gt;&lt;/font&gt;&lt;br&gt;可有其他修饰符" vertex="1">
          <mxGeometry height="70" width="200" x="570" y="190" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="120" x="50" y="260" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#3f51b5;" value="&lt;b&gt;💾 存储位置&lt;/b&gt;" vertex="1">
          <mxGeometry height="50" width="120" x="50" y="260" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="170" y="260" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;栈&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="200" x="170" y="260" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="370" y="260" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#e65100&quot;&gt;&lt;b&gt;堆&lt;/b&gt;&lt;/font&gt;(对象内)" vertex="1">
          <mxGeometry height="50" width="200" x="370" y="260" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="570" y="260" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;方法区/元空间&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="200" x="570" y="260" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="120" x="50" y="310" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#3f51b5;" value="&lt;b&gt;⏱️ 生命周期&lt;/b&gt;" vertex="1">
          <mxGeometry height="50" width="120" x="50" y="310" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="170" y="310" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="方法调用 → 结束" vertex="1">
          <mxGeometry height="50" width="200" x="170" y="310" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="370" y="310" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="对象创建 → 销毁" vertex="1">
          <mxGeometry height="50" width="200" x="370" y="310" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="570" y="310" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="类加载 → 卸载" vertex="1">
          <mxGeometry height="50" width="200" x="570" y="310" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="120" x="50" y="360" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#3f51b5;" value="&lt;b&gt;📋 默认值&lt;/b&gt;" vertex="1">
          <mxGeometry height="50" width="120" x="50" y="360" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="170" y="360" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;无&lt;/b&gt;&lt;/font&gt;,必须初始化" vertex="1">
          <mxGeometry height="50" width="200" x="170" y="360" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="370" y="360" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="&lt;font color=&quot;#4caf50&quot;&gt;&lt;b&gt;有&lt;/b&gt;&lt;/font&gt;默认值" vertex="1">
          <mxGeometry height="50" width="200" x="370" y="360" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="570" y="360" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="&lt;font color=&quot;#4caf50&quot;&gt;&lt;b&gt;有&lt;/b&gt;&lt;/font&gt;默认值" vertex="1">
          <mxGeometry height="50" width="200" x="570" y="360" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="120" x="50" y="410" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#3f51b5;" value="&lt;b&gt;🎯 作用范围&lt;/b&gt;" vertex="1">
          <mxGeometry height="50" width="120" x="50" y="410" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="170" y="410" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="方法/代码块内" vertex="1">
          <mxGeometry height="50" width="200" x="170" y="410" as="geometry" />
        </mxCell>
        <mxCell id="56" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="370" y="410" as="geometry" />
        </mxCell>
        <mxCell id="57" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="整个类" vertex="1">
          <mxGeometry height="50" width="200" x="370" y="410" as="geometry" />
        </mxCell>
        <mxCell id="58" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="570" y="410" as="geometry" />
        </mxCell>
        <mxCell id="59" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="整个类" vertex="1">
          <mxGeometry height="50" width="200" x="570" y="410" as="geometry" />
        </mxCell>
        <mxCell id="60" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="70" width="120" x="50" y="460" as="geometry" />
        </mxCell>
        <mxCell id="61" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#3f51b5;" value="&lt;b&gt;🔑 访问方式&lt;/b&gt;" vertex="1">
          <mxGeometry height="70" width="120" x="50" y="460" as="geometry" />
        </mxCell>
        <mxCell id="62" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="70" width="200" x="170" y="460" as="geometry" />
        </mxCell>
        <mxCell id="63" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="直接使用" vertex="1">
          <mxGeometry height="70" width="200" x="170" y="460" as="geometry" />
        </mxCell>
        <mxCell id="64" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="70" width="200" x="370" y="460" as="geometry" />
        </mxCell>
        <mxCell id="65" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#424242;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#e65100&quot;&gt;&lt;b&gt;this.&lt;/b&gt;&lt;/font&gt;变量名&lt;br&gt;或直接使用" vertex="1">
          <mxGeometry height="70" width="200" x="370" y="460" as="geometry" />
        </mxCell>
        <mxCell id="66" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="70" width="200" x="570" y="460" as="geometry" />
        </mxCell>
        <mxCell id="67" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#424242;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#2e7d32&quot;&gt;&lt;b&gt;类名.&lt;/b&gt;&lt;/font&gt;变量名&lt;br&gt;或直接使用" vertex="1">
          <mxGeometry height="70" width="200" x="570" y="460" as="geometry" />
        </mxCell>
        <mxCell id="68" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="120" x="50" y="530" as="geometry" />
        </mxCell>
        <mxCell id="69" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#3f51b5;" value="&lt;b&gt;🔒 线程安全&lt;/b&gt;" vertex="1">
          <mxGeometry height="50" width="120" x="50" y="530" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

1.3 变量存储位置图解

<mxfile>
  <diagram id="jvm-variable-storage" name="变量存储位置">
    <mxGraphModel dx="883" dy="401" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="750" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#1E293B;" value="变量存储位置" vertex="1">
          <mxGeometry height="40" width="300" x="300" y="15" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;" value="JVM 内存结构与变量分布" vertex="1">
          <mxGeometry height="24" width="230" x="335" y="55" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#1E293B;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="635" width="820" x="40" y="95" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="JVM 内存结构" vertex="1">
          <mxGeometry height="32" width="150" x="75" y="120" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#8B5CF6;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="165" width="780" x="60" y="160" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="方法区 / 元空间" vertex="1">
          <mxGeometry height="30" width="140" x="80" y="175" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#8B5CF6;fontStyle=2;" value="Metaspace" vertex="1">
          <mxGeometry height="22" width="80" x="225" y="179" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=#A78BFA;align=center;verticalAlign=middle;fontSize=10;fontColor=#FFFFFF;rounded=1;" value="线程共享" vertex="1">
          <mxGeometry height="20" width="60" x="760" y="175" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#C4B5FD;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="95" width="740" x="80" y="215" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=#F3E8FF;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#6D28D9;rounded=1;" value="类信息、常量池、静态变量" vertex="1">
          <mxGeometry height="24" width="180" x="100" y="225" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#E9D5FF;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="42" width="700" x="100" y="258" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=left;verticalAlign=middle;fontSize=12;fontFamily=Courier New;fontStyle=1;fontColor=#FFFFFF;rounded=1;spacingLeft=8;spacingRight=8;" value="static int classCount = 0;" vertex="1">
          <mxGeometry height="26" width="220" x="120" y="266" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6D28D9;fontStyle=1;" value="← 静态变量" vertex="1">
          <mxGeometry height="22" width="80" x="350" y="268" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=#A78BFA;align=left;verticalAlign=middle;fontSize=12;fontFamily=Courier New;fontStyle=1;fontColor=#FFFFFF;rounded=1;spacingLeft=8;spacingRight=8;" value="static final String APP_NAME;" vertex="1">
          <mxGeometry height="26" width="250" x="450" y="266" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6D28D9;fontStyle=1;" value="← 静态常量" vertex="1">
          <mxGeometry height="22" width="80" x="710" y="268" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="175" width="780" x="60" y="340" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="堆" vertex="1">
          <mxGeometry height="30" width="50" x="80" y="355" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#22C55E;fontStyle=2;" value="Heap" vertex="1">
          <mxGeometry height="22" width="50" x="135" y="359" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=#4ADE80;align=center;verticalAlign=middle;fontSize=10;fontColor=#FFFFFF;rounded=1;" value="线程共享" vertex="1">
          <mxGeometry height="20" width="60" x="760" y="355" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BBF7D0;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="105" width="740" x="80" y="395" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#166534;rounded=1;" value="对象实例" vertex="1">
          <mxGeometry height="24" width="80" x="100" y="405" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#86EFAC;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="52" width="700" x="100" y="438" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="VariableDemo 对象" vertex="1">
          <mxGeometry height="26" width="140" x="120" y="448" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=left;verticalAlign=middle;fontSize=12;fontFamily=Courier New;fontStyle=1;fontColor=#166534;rounded=1;spacingLeft=8;" value="├── name = &quot;Tom&quot;" vertex="1">
          <mxGeometry height="26" width="160" x="280" y="448" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#166534;fontStyle=1;" value="← 成员变量" vertex="1">
          <mxGeometry height="22" width="80" x="445" y="450" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=left;verticalAlign=middle;fontSize=12;fontFamily=Courier New;fontStyle=1;fontColor=#166534;rounded=1;spacingLeft=8;" value="└── age = 20" vertex="1">
          <mxGeometry height="26" width="130" x="540" y="448" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#166534;fontStyle=1;" value="← 成员变量" vertex="1">
          <mxGeometry height="22" width="80" x="675" y="450" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="170" width="780" x="60" y="530" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="虚拟机栈" vertex="1">
          <mxGeometry height="30" width="90" x="80" y="545" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#3B82F6;fontStyle=2;" value="VM Stack" vertex="1">
          <mxGeometry height="22" width="70" x="175" y="549" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#60A5FA;align=center;verticalAlign=middle;fontSize=10;fontColor=#FFFFFF;rounded=1;" value="线程私有" vertex="1">
          <mxGeometry height="20" width="60" x="760" y="545" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#93C5FD;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="100" width="740" x="80" y="585" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="方法栈帧" vertex="1">
          <mxGeometry height="24" width="80" x="100" y="595" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="47" width="700" x="100" y="628" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="局部变量表" vertex="1">
          <mxGeometry height="24" width="85" x="115" y="638" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="param = 10" vertex="1">
          <mxGeometry height="22" width="100" x="215" y="640" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#1E40AF;fontStyle=1;" value="← 方法参数" vertex="1">
          <mxGeometry height="22" width="70" x="320" y="640" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="localVar = 10" vertex="1">
          <mxGeometry height="22" width="110" x="400" y="640" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#1E40AF;fontStyle=1;" value="← 局部变量" vertex="1">
          <mxGeometry height="22" width="70" x="515" y="640" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="i = 0" vertex="1">
          <mxGeometry height="22" width="60" x="600" y="640" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#1E40AF;fontStyle=1;" value="← 循环变量" vertex="1">
          <mxGeometry height="22" width="70" x="665" y="640" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=#166534;align=center;verticalAlign=middle;fontSize=9;fontColor=#FFFFFF;rounded=1;" value="GC 管理" vertex="1">
          <mxGeometry height="18" width="55" x="200" y="355" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E40AF;align=center;verticalAlign=middle;fontSize=9;fontColor=#FFFFFF;rounded=1;" value="方法结束自动释放" vertex="1">
          <mxGeometry height="18" width="110" x="260" y="549" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=#6D28D9;align=center;verticalAlign=middle;fontSize=9;fontColor=#FFFFFF;rounded=1;" value="类卸载时释放" vertex="1">
          <mxGeometry height="18" width="85" x="310" y="179" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

1.4 为什么成员变量有默认值

<mxfile>
  <diagram id="default-value-comparison-fixed" name="成员变量默认值vs局部变量-修复版">
    <mxGraphModel dx="2340" dy="1001" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1100" pageHeight="880" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#7c4dff;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="880" width="1040" x="30" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=26;fontStyle=1;fontColor=#512da8;" value="成员变量默认值 vs 局部变量无默认值" vertex="1">
          <mxGeometry height="45" width="640" x="230" y="35" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ede7f6;strokeColor=#7c4dff;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="60" width="980" x="60" y="95" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontColor=#4527a0;" value="&lt;font style=&quot;font-size: 16px;&quot;&gt;&lt;b&gt;问题:&lt;/b&gt;为什么局部变量必须初始化,而成员变量可以不初始化?&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="980" x="60" y="95" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="310" width="480" x="60" y="175" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="45" width="480" x="60" y="175" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 16px;&quot;&gt;&lt;b&gt;📋 原因 1:编译器的控制能力&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="480" x="60" y="175" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#64b5f6;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="110" width="450" x="75" y="235" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;局部变量&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="100" x="90" y="240" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=top;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;spacingLeft=10;" value="• 生命周期短,作用域小&lt;br&gt;• 编译器可以&lt;b&gt;轻松追踪&lt;/b&gt;是否被初始化&lt;br&gt;• 如果使用前未初始化,&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;编译器直接报错&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="70" width="430" x="85" y="268" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffb74d;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="115" width="450" x="75" y="355" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#e65100&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;成员变量&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="100" x="90" y="360" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=top;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;spacingLeft=10;" value="• 生命周期长,可能在&lt;b&gt;多个方法&lt;/b&gt;中被使用&lt;br&gt;• 初始化可能发生在构造器、初始化块、或任意方法中&lt;br&gt;• 编译器&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;难以判断&lt;/b&gt;&lt;/font&gt;使用时是否已初始化" vertex="1">
          <mxGeometry height="75" width="430" x="85" y="388" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fce4ec;strokeColor=#e91e63;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="310" width="480" x="560" y="175" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e91e63;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="45" width="480" x="560" y="175" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 16px;&quot;&gt;&lt;b&gt;🔒 原因 2:安全性考虑&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="480" x="560" y="175" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#ef9a9a;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="70" width="450" x="575" y="235" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="&lt;font color=&quot;#c62828&quot;&gt;⚠️ &lt;b&gt;如果成员变量不给默认值:&lt;/b&gt;&lt;/font&gt;&lt;br&gt;会读取到内存中的&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;&quot;垃圾值&quot;&lt;/b&gt;&lt;/font&gt;,导致程序行为不可预测" vertex="1">
          <mxGeometry height="70" width="450" x="575" y="235" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#81c784;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="150" width="450" x="575" y="320" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;spacingLeft=15;" value="&lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;给默认值可以保证:&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="200" x="575" y="325" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#81c784;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="95" width="130" x="590" y="360" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;数值类型&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font style=&quot;font-size: 20px;&quot; color=&quot;#1976d2&quot;&gt;&lt;b&gt;0&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 11px;&quot;&gt;安全的起始值&lt;/font&gt;" vertex="1">
          <mxGeometry height="95" width="130" x="590" y="360" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#81c784;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="95" width="130" x="735" y="360" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#e65100&quot;&gt;&lt;b&gt;引用类型&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font style=&quot;font-size: 20px;&quot; color=&quot;#ff9800&quot;&gt;&lt;b&gt;null&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 11px;&quot;&gt;明确未指向对象&lt;/font&gt;" vertex="1">
          <mxGeometry height="95" width="130" x="735" y="360" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#81c784;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="95" width="130" x="880" y="360" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#7b1fa2&quot;&gt;&lt;b&gt;布尔类型&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font style=&quot;font-size: 20px;&quot; color=&quot;#9c27b0&quot;&gt;&lt;b&gt;false&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 11px;&quot;&gt;安全的初始状态&lt;/font&gt;" vertex="1">
          <mxGeometry height="95" width="130" x="880" y="360" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="365" width="980" x="60" y="505" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="45" width="980" x="60" y="505" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 16px;&quot;&gt;&lt;b&gt;📊 默认值一览表&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="980" x="60" y="505" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#81c784;strokeColor=#4caf50;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="460" x="80" y="565" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;类型&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="460" x="80" y="565" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#81c784;strokeColor=#4caf50;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="480" x="540" y="565" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;默认值&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="480" x="540" y="565" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="36" width="460" x="80" y="605" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#2e7d32&quot;&gt;&lt;b&gt;byte / short / int&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="36" width="460" x="80" y="605" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="36" width="480" x="540" y="605" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; style=&quot;font-size: 16px;&quot; color=&quot;#1565c0&quot;&gt;&lt;b&gt;0&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="36" width="480" x="540" y="605" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="36" width="460" x="80" y="641" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#2e7d32&quot;&gt;&lt;b&gt;long&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="36" width="460" x="80" y="641" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="36" width="480" x="540" y="641" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; style=&quot;font-size: 16px;&quot; color=&quot;#1565c0&quot;&gt;&lt;b&gt;0L&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="36" width="480" x="540" y="641" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="36" width="460" x="80" y="677" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#2e7d32&quot;&gt;&lt;b&gt;float&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="36" width="460" x="80" y="677" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="36" width="480" x="540" y="677" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; style=&quot;font-size: 16px;&quot; color=&quot;#1565c0&quot;&gt;&lt;b&gt;0.0f&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="36" width="480" x="540" y="677" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="36" width="460" x="80" y="713" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#2e7d32&quot;&gt;&lt;b&gt;double&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="36" width="460" x="80" y="713" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="36" width="480" x="540" y="713" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; style=&quot;font-size: 16px;&quot; color=&quot;#1565c0&quot;&gt;&lt;b&gt;0.0d&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="36" width="480" x="540" y="713" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="36" width="460" x="80" y="749" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#2e7d32&quot;&gt;&lt;b&gt;char&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="36" width="460" x="80" y="749" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="36" width="480" x="540" y="749" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; style=&quot;font-size: 16px;&quot; color=&quot;#1565c0&quot;&gt;&lt;b&gt;&#39;\u0000&#39;&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="36" width="480" x="540" y="749" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="36" width="460" x="80" y="785" as="geometry" />
        </mxCell>
        <mxCell id="56" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#2e7d32&quot;&gt;&lt;b&gt;boolean&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="36" width="460" x="80" y="785" as="geometry" />
        </mxCell>
        <mxCell id="57" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="36" width="480" x="540" y="785" as="geometry" />
        </mxCell>
        <mxCell id="58" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; style=&quot;font-size: 16px;&quot; color=&quot;#9c27b0&quot;&gt;&lt;b&gt;false&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="36" width="480" x="540" y="785" as="geometry" />
        </mxCell>
        <mxCell id="59" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe0b2;strokeColor=#ffb74d;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="36" width="460" x="80" y="821" as="geometry" />
        </mxCell>
        <mxCell id="60" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#e65100&quot;&gt;&lt;b&gt;引用类型 (String, Object...)&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="36" width="460" x="80" y="821" as="geometry" />
        </mxCell>
        <mxCell id="61" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffb74d;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="36" width="480" x="540" y="821" as="geometry" />
        </mxCell>
        <mxCell id="62" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; style=&quot;font-size: 16px;&quot; color=&quot;#e65100&quot;&gt;&lt;b&gt;null&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="36" width="480" x="540" y="821" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
public class DefaultValueDemo {

    // 成员变量有默认值
    private int count;           // 默认 0
    private String name;         // 默认 null
    private boolean active;      // 默认 false
    private int[] array;         // 默认 null

    public void showDefaults() {
        System.out.println("count: " + count);     // 0
        System.out.println("name: " + name);       // null
        System.out.println("active: " + active);   // false
        System.out.println("array: " + array);     // null
    }

    public void localVariableDemo() {
        int localVar;
        // System.out.println(localVar);  // ❌ 编译错误:变量未初始化

        localVar = 10;  // 必须先赋值
        System.out.println(localVar);  // ✅ 正确
    }
}

1.5 静态变量的作用

Java

public class Counter {

    // 静态变量:所有实例共享
    private static int totalCount = 0;

    // 实例变量:每个实例独立
    private int instanceId;

    public Counter() {
        totalCount++;
        instanceId = totalCount;
    }

    public static void main(String[] args) {
        Counter c1 = new Counter();
        Counter c2 = new Counter();
        Counter c3 = new Counter();

        System.out.println("c1.instanceId: " + c1.instanceId);  // 1
        System.out.println("c2.instanceId: " + c2.instanceId);  // 2
        System.out.println("c3.instanceId: " + c3.instanceId);  // 3

        // 静态变量通过类名访问
        System.out.println("totalCount: " + Counter.totalCount);  // 3
    }
}
<mxfile>
  <diagram id="static-variable-sharing" name="静态变量共享图解">
    <mxGraphModel dx="900" dy="680" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="680">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" value="静态变量共享图解" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#1E293B;" vertex="1" parent="1">
          <mxGeometry x="275" y="20" width="350" height="40" as="geometry" />
        </mxCell>
        <mxCell id="3" value="Static Variable Sharing Mechanism" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="305" y="60" width="290" height="24" as="geometry" />
        </mxCell>
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#8B5CF6;strokeWidth=3;" vertex="1" parent="1">
          <mxGeometry x="80" y="110" width="740" height="180" as="geometry" />
        </mxCell>
        <mxCell id="5" value="方法区" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="125" width="100" height="35" as="geometry" />
        </mxCell>
        <mxCell id="6" value="Metaspace" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#8B5CF6;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="205" y="131" width="80" height="24" as="geometry" />
        </mxCell>
        <mxCell id="7" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#C4B5FD;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="100" y="175" width="700" height="100" as="geometry" />
        </mxCell>
        <mxCell id="8" value="Counter 类信息" style="text;html=1;strokeColor=none;fillColor=#A78BFA;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="120" y="190" width="130" height="28" as="geometry" />
        </mxCell>
        <mxCell id="9" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#E9D5FF;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="120" y="230" width="660" height="32" as="geometry" />
        </mxCell>
        <mxCell id="10" value="static int totalCount = 3;" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=14;fontFamily=Courier New;fontStyle=1;fontColor=#FFFFFF;rounded=1;spacingLeft=10;spacingRight=10;" vertex="1" parent="1">
          <mxGeometry x="140" y="235" width="230" height="24" as="geometry" />
        </mxCell>
        <mxCell id="11" value="← 所有实例共享这一个变量" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#92400E;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="390" y="235" width="200" height="24" as="geometry" />
        </mxCell>
        <mxCell id="12" value="唯一副本" style="text;html=1;strokeColor=none;fillColor=#6D28D9;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="610" y="237" width="65" height="20" as="geometry" />
        </mxCell>
        <mxCell id="13" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;" vertex="1" parent="1">
          <mxGeometry x="80" y="420" width="740" height="160" as="geometry" />
        </mxCell>
        <mxCell id="14" value="堆" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="435" width="50" height="35" as="geometry" />
        </mxCell>
        <mxCell id="15" value="Heap" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#22C55E;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="155" y="441" width="50" height="24" as="geometry" />
        </mxCell>
        <mxCell id="16" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#86EFAC;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="100" y="485" width="220" height="80" as="geometry" />
        </mxCell>
        <mxCell id="17" value="Counter 对象 c1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="115" y="495" width="130" height="26" as="geometry" />
        </mxCell>
        <mxCell id="18" value="instanceId = 1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontFamily=Courier New;fontStyle=1;fontColor=#166534;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="115" y="528" width="120" height="24" as="geometry" />
        </mxCell>
        <mxCell id="19" value="(各自独立)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#166534;" vertex="1" parent="1">
          <mxGeometry x="240" y="530" width="70" height="20" as="geometry" />
        </mxCell>
        <mxCell id="20" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#86EFAC;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="340" y="485" width="220" height="80" as="geometry" />
        </mxCell>
        <mxCell id="21" value="Counter 对象 c2" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="355" y="495" width="130" height="26" as="geometry" />
        </mxCell>
        <mxCell id="22" value="instanceId = 2" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontFamily=Courier New;fontStyle=1;fontColor=#166534;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="355" y="528" width="120" height="24" as="geometry" />
        </mxCell>
        <mxCell id="23" value="(各自独立)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#166534;" vertex="1" parent="1">
          <mxGeometry x="480" y="530" width="70" height="20" as="geometry" />
        </mxCell>
        <mxCell id="24" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#86EFAC;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="580" y="485" width="220" height="80" as="geometry" />
        </mxCell>
        <mxCell id="25" value="Counter 对象 c3" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="595" y="495" width="130" height="26" as="geometry" />
        </mxCell>
        <mxCell id="26" value="instanceId = 3" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontFamily=Courier New;fontStyle=1;fontColor=#166534;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="595" y="528" width="120" height="24" as="geometry" />
        </mxCell>
        <mxCell id="27" value="(各自独立)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#166534;" vertex="1" parent="1">
          <mxGeometry x="720" y="530" width="70" height="20" as="geometry" />
        </mxCell>
        <mxCell id="28" value="" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#8B5CF6;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.35;exitY=1;exitDx=0;exitDy=0;" edge="1" parent="1" source="9" target="16">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="250" y="280" as="sourcePoint" />
            <mxPoint x="210" y="485" as="targetPoint" /><Array as="points">
              <mxPoint x="210" y="350" />
            </Array>
          </mxGeometry>
        </mxCell>
        <mxCell id="29" value="" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#8B5CF6;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" edge="1" parent="1" source="9" target="20">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="450" y="280" as="sourcePoint" />
            <mxPoint x="450" y="485" as="targetPoint" />
            <Array as="points">
              <mxPoint x="450" y="350" />
            </Array>
          </mxGeometry>
        </mxCell>
        <mxCell id="30" value="" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#8B5CF6;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.65;exitY=1;exitDx=0;exitDy=0;" edge="1" parent="1" source="9" target="24">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="690" y="280" as="sourcePoint" />
            <mxPoint x="690" y="485" as="targetPoint" />
            <Array as="points">
              <mxPoint x="690" y="350" />
            </Array>
          </mxGeometry>
        </mxCell>
        <mxCell id="31" value="共享访问" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="415" y="335" width="70" height="22" as="geometry" />
        </mxCell>
        <mxCell id="32" value="引用" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#8B5CF6;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="170" y="370" width="40" height="20" as="geometry" />
        </mxCell>
        <mxCell id="33" value="引用" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#8B5CF6;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="455" y="370" width="40" height="20" as="geometry" />
        </mxCell>
        <mxCell id="34" value="引用" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#8B5CF6;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="695" y="370" width="40" height="20" as="geometry" />
        </mxCell>
        <mxCell id="35" value="💡 静态变量存储在方法区,所有对象实例共享同一份数据" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#92400E;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="230" y="600" width="440" height="28" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>


二、方法详解

2.1 方法的分类

<mxfile>
  <diagram id="method-classification" name="方法分类">
    <mxGraphModel dx="900" dy="700" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        
        <!-- 主容器 -->
        <mxCell id="2" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="30" y="20" width="840" height="660" as="geometry" />
        </mxCell>
        
        <!-- 标题 -->
        <mxCell id="3" value="方法分类" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=28;fontStyle=1;fontColor=#3949ab;" vertex="1" parent="1">
          <mxGeometry x="330" y="35" width="240" height="45" as="geometry" />
        </mxCell>
        
        <!-- ==================== 第一部分:按参数和返回值分类 ==================== -->
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="50" y="95" width="800" height="270" as="geometry" />
        </mxCell>
        
        <!-- 第一部分标题 -->
        <mxCell id="5" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="50" y="95" width="800" height="45" as="geometry" />
        </mxCell>
        <mxCell id="6" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 17px;&quot;&gt;&lt;b&gt;📋 按参数和返回值分类&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="50" y="95" width="800" height="45" as="geometry" />
        </mxCell>
        
        <!-- 表头 -->
        <mxCell id="7" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#64b5f6;strokeColor=#42a5f5;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="70" y="155" width="180" height="40" as="geometry" />
        </mxCell>
        <mxCell id="8" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;类型&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="70" y="155" width="180" height="40" as="geometry" />
        </mxCell>
        <mxCell id="9" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#64b5f6;strokeColor=#42a5f5;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="250" y="155" width="580" height="40" as="geometry" />
        </mxCell>
        <mxCell id="10" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;示例&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="250" y="155" width="580" height="40" as="geometry" />
        </mxCell>
        
        <!-- 第1行:无参无返回值 -->
        <mxCell id="11" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#90caf9;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="70" y="195" width="180" height="40" as="geometry" />
        </mxCell>
        <mxCell id="12" value="&lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;无参无返回值&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="70" y="195" width="180" height="40" as="geometry" />
        </mxCell>
        <mxCell id="13" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="250" y="195" width="580" height="40" as="geometry" />
        </mxCell>
        <mxCell id="14" value="&lt;font face=&quot;Courier New&quot; color=&quot;#424242&quot;&gt;&lt;font color=&quot;#7b1fa2&quot;&gt;void&lt;/font&gt; &lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;printHello&lt;/b&gt;&lt;/font&gt;() { ... }&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="250" y="195" width="580" height="40" as="geometry" />
        </mxCell>
        
        <!-- 第2行:有参无返回值 -->
        <mxCell id="15" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#90caf9;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="70" y="235" width="180" height="40" as="geometry" />
        </mxCell>
        <mxCell id="16" value="&lt;font color=&quot;#e65100&quot;&gt;&lt;b&gt;有参无返回值&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="70" y="235" width="180" height="40" as="geometry" />
        </mxCell>
        <mxCell id="17" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="250" y="235" width="580" height="40" as="geometry" />
        </mxCell>
        <mxCell id="18" value="&lt;font face=&quot;Courier New&quot; color=&quot;#424242&quot;&gt;&lt;font color=&quot;#7b1fa2&quot;&gt;void&lt;/font&gt; &lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;printMessage&lt;/b&gt;&lt;/font&gt;(&lt;font color=&quot;#2e7d32&quot;&gt;String msg&lt;/font&gt;) { ... }&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="250" y="235" width="580" height="40" as="geometry" />
        </mxCell>
        
        <!-- 第3行:无参有返回值 -->
        <mxCell id="19" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#90caf9;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="70" y="275" width="180" height="40" as="geometry" />
        </mxCell>
        <mxCell id="20" value="&lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;无参有返回值&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="70" y="275" width="180" height="40" as="geometry" />
        </mxCell>
        <mxCell id="21" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="250" y="275" width="580" height="40" as="geometry" />
        </mxCell>
        <mxCell id="22" value="&lt;font face=&quot;Courier New&quot; color=&quot;#424242&quot;&gt;&lt;font color=&quot;#7b1fa2&quot;&gt;int&lt;/font&gt; &lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;getCount&lt;/b&gt;&lt;/font&gt;() { ... }&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="250" y="275" width="580" height="40" as="geometry" />
        </mxCell>
        
        <!-- 第4行:有参有返回值 -->
        <mxCell id="23" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#90caf9;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="70" y="315" width="180" height="40" as="geometry" />
        </mxCell>
        <mxCell id="24" value="&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;有参有返回值&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="70" y="315" width="180" height="40" as="geometry" />
        </mxCell>
        <mxCell id="25" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="250" y="315" width="580" height="40" as="geometry" />
        </mxCell>
        <mxCell id="26" value="&lt;font face=&quot;Courier New&quot; color=&quot;#424242&quot;&gt;&lt;font color=&quot;#7b1fa2&quot;&gt;int&lt;/font&gt; &lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;add&lt;/b&gt;&lt;/font&gt;(&lt;font color=&quot;#2e7d32&quot;&gt;int a, int b&lt;/font&gt;) { ... }&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="250" y="315" width="580" height="40" as="geometry" />
        </mxCell>
        
        <!-- ==================== 第二部分:按是否静态分类 ==================== -->
        <mxCell id="27" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ff9800;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="50" y="385" width="800" height="275" as="geometry" />
        </mxCell>
        
        <!-- 第二部分标题 -->
        <mxCell id="28" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="50" y="385" width="800" height="45" as="geometry" />
        </mxCell>
        <mxCell id="29" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 17px;&quot;&gt;&lt;b&gt;⚡ 按是否静态分类&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="50" y="385" width="800" height="45" as="geometry" />
        </mxCell>
        
        <!-- 表头 -->
        <mxCell id="30" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffb74d;strokeColor=#ffa726;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="70" y="445" width="180" height="40" as="geometry" />
        </mxCell>
        <mxCell id="31" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;类型&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="70" y="445" width="180" height="40" as="geometry" />
        </mxCell>
        
        <mxCell id="32" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffb74d;strokeColor=#ffa726;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="250" y="445" width="580" height="40" as="geometry" />
        </mxCell>
        <mxCell id="33" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;特点&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="250" y="445" width="580" height="40" as="geometry" />
        </mxCell>
        
        <!-- 第1行:静态方法 -->
        <mxCell id="34" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe0b2;strokeColor=#ffcc80;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="70" y="485" width="180" height="50" as="geometry" />
        </mxCell>
        <mxCell id="35" value="&lt;font color=&quot;#e65100&quot;&gt;&lt;b&gt;静态方法&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#7b1fa2&quot; style=&quot;font-size: 12px;&quot;&gt;static&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="70" y="485" width="180" height="50" as="geometry" />
        </mxCell>
        <mxCell id="36" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff8e1;strokeColor=#ffcc80;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="250" y="485" width="580" height="50" as="geometry" />
        </mxCell>
        <mxCell id="37" value="属于&lt;b&gt;&lt;/b&gt;,通过&lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;类名&lt;/b&gt;&lt;/font&gt;调用 &amp;nbsp; &lt;font face=&quot;Courier New&quot; color=&quot;#757575&quot;&gt;ClassName.method()&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#424242;" vertex="1" parent="1">
          <mxGeometry x="250" y="485" width="580" height="50" as="geometry" />
        </mxCell>
        
        <!-- 第2行:实例方法 -->
        <mxCell id="38" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe0b2;strokeColor=#ffcc80;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="70" y="535" width="180" height="50" as="geometry" />
        </mxCell>
        <mxCell id="39" value="&lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;实例方法&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="70" y="535" width="180" height="50" as="geometry" />
        </mxCell>
        <mxCell id="40" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff8e1;strokeColor=#ffcc80;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="250" y="535" width="580" height="50" as="geometry" />
        </mxCell>
        <mxCell id="41" value="属于&lt;b&gt;对象&lt;/b&gt;,通过&lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;实例&lt;/b&gt;&lt;/font&gt;调用 &amp;nbsp; &lt;font face=&quot;Courier New&quot; color=&quot;#757575&quot;&gt;object.method()&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#424242;" vertex="1" parent="1">
          <mxGeometry x="250" y="535" width="580" height="50" as="geometry" />
        </mxCell>
        
        <!-- 第3行:构造方法 -->
        <mxCell id="42" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe0b2;strokeColor=#ffcc80;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="70" y="585" width="180" height="55" as="geometry" />
        </mxCell>
        <mxCell id="43" value="&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;构造方法&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#757575&quot; style=&quot;font-size: 12px;&quot;&gt;Constructor&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="70" y="585" width="180" height="55" as="geometry" />
        </mxCell>
        <mxCell id="44" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff8e1;strokeColor=#ffcc80;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="250" y="585" width="580" height="55" as="geometry" />
        </mxCell>
        <mxCell id="45" value="创建对象时调用,&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;无返回值&lt;/b&gt;&lt;/font&gt; &amp;nbsp; &lt;font face=&quot;Courier New&quot; color=&quot;#757575&quot;&gt;new ClassName()&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#424242;" vertex="1" parent="1">
          <mxGeometry x="250" y="585" width="580" height="55" as="geometry" />
        </mxCell>
        
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
public class MethodDemo {

    private int count = 0;

    // ═══════════════════════════════════════════════════════════════════
    // 1. 无参无返回值
    // ═══════════════════════════════════════════════════════════════════
    public void printHello() {
        System.out.println("Hello");
    }

    // ═══════════════════════════════════════════════════════════════════
    // 2. 有参无返回值
    // ═══════════════════════════════════════════════════════════════════
    public void printMessage(String message) {
        System.out.println(message);
    }

    // ═══════════════════════════════════════════════════════════════════
    // 3. 无参有返回值
    // ═══════════════════════════════════════════════════════════════════
    public int getCount() {
        return count;
    }

    // ═══════════════════════════════════════════════════════════════════
    // 4. 有参有返回值
    // ═══════════════════════════════════════════════════════════════════
    public int add(int a, int b) {
        return a + b;
    }

    // ═══════════════════════════════════════════════════════════════════
    // 5. 静态方法
    // ═══════════════════════════════════════════════════════════════════
    public static int max(int a, int b) {
        return a > b ? a : b;
    }
}

2.2 静态方法 vs 实例方法

<mxfile>
  <diagram id="static-vs-instance-method" name="静态方法vs实例方法">
    <mxGraphModel dx="900" dy="750" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="750">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" value="静态方法 vs 实例方法" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#1E293B;" vertex="1" parent="1">
          <mxGeometry x="250" y="20" width="400" height="40" as="geometry" />
        </mxCell>
        <mxCell id="3" value="Static Method vs Instance Method" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="305" y="60" width="290" height="24" as="geometry" />
        </mxCell>
        <mxCell id="4" value="对比项" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#1E293B;strokeWidth=2;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="110" width="140" height="45" as="geometry" />
        </mxCell>
        <mxCell id="5" value="静态方法" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#8B5CF6;strokeColor=#8B5CF6;strokeWidth=2;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="210" y="110" width="280" height="45" as="geometry" />
        </mxCell>
        <mxCell id="6" value="实例方法" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#22C55E;strokeWidth=2;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="500" y="110" width="280" height="45" as="geometry" />
        </mxCell>
        <mxCell id="7" value="关键字" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontColor=#1E293B;fontSize=13;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="165" width="140" height="50" as="geometry" />
        </mxCell>
        <mxCell id="8" value="static" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#C4B5FD;strokeWidth=1;fontColor=#6D28D9;fontSize=13;fontFamily=Courier New;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="210" y="165" width="280" height="50" as="geometry" />
        </mxCell>
        <mxCell id="9" value="无" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;fontColor=#166534;fontSize=13;" vertex="1" parent="1">
          <mxGeometry x="500" y="165" width="280" height="50" as="geometry" />
        </mxCell>
        <mxCell id="10" value="归属" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontColor=#1E293B;fontSize=13;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="225" width="140" height="50" as="geometry" />
        </mxCell>
        <mxCell id="11" value="属于类" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#C4B5FD;strokeWidth=1;fontColor=#6D28D9;fontSize=13;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="210" y="225" width="280" height="50" as="geometry" />
        </mxCell>
        <mxCell id="12" value="属于对象" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;fontColor=#166534;fontSize=13;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="500" y="225" width="280" height="50" as="geometry" />
        </mxCell>
        <mxCell id="13" value="调用方式" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontColor=#1E293B;fontSize=13;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="285" width="140" height="60" as="geometry" />
        </mxCell>
        <mxCell id="14" value="&lt;font face=&quot;Courier New&quot;&gt;类名.方法名()&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#9ca3af&quot; style=&quot;font-size: 11px;&quot;&gt;或 对象.方法名()&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#C4B5FD;strokeWidth=1;fontColor=#6D28D9;fontSize=13;" vertex="1" parent="1">
          <mxGeometry x="210" y="285" width="280" height="60" as="geometry" />
        </mxCell>
        <mxCell id="15" value="&lt;font face=&quot;Courier New&quot;&gt;对象.方法名()&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;fontColor=#166534;fontSize=13;" vertex="1" parent="1">
          <mxGeometry x="500" y="285" width="280" height="60" as="geometry" />
        </mxCell>
        <mxCell id="16" value="访问成员变量" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontColor=#1E293B;fontSize=13;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="355" width="140" height="70" as="geometry" />
        </mxCell>
        <mxCell id="17" value="❌ 不能访问实例变量&lt;br&gt;✅ 可以访问静态变量" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#C4B5FD;strokeWidth=1;fontColor=#6D28D9;fontSize=12;align=center;" vertex="1" parent="1">
          <mxGeometry x="210" y="355" width="280" height="70" as="geometry" />
        </mxCell>
        <mxCell id="18" value="✅ 可以访问实例变量&lt;br&gt;✅ 可以访问静态变量" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;fontColor=#166534;fontSize=12;align=center;" vertex="1" parent="1">
          <mxGeometry x="500" y="355" width="280" height="70" as="geometry" />
        </mxCell>
        <mxCell id="19" value="调用其他方法" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontColor=#1E293B;fontSize=13;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="435" width="140" height="70" as="geometry" />
        </mxCell>
        <mxCell id="20" value="❌ 不能调用实例方法&lt;br&gt;✅ 可以调用静态方法" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#C4B5FD;strokeWidth=1;fontColor=#6D28D9;fontSize=12;align=center;" vertex="1" parent="1">
          <mxGeometry x="210" y="435" width="280" height="70" as="geometry" />
        </mxCell>
        <mxCell id="21" value="✅ 可以调用实例方法&lt;br&gt;✅ 可以调用静态方法" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;fontColor=#166534;fontSize=12;align=center;" vertex="1" parent="1">
          <mxGeometry x="500" y="435" width="280" height="70" as="geometry" />
        </mxCell>
        <mxCell id="22" value="this / super" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontColor=#1E293B;fontSize=13;fontStyle=1;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="60" y="515" width="140" height="50" as="geometry" />
        </mxCell>
        <mxCell id="23" value="❌ 不能使用" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#C4B5FD;strokeWidth=1;fontColor=#6D28D9;fontSize=13;" vertex="1" parent="1">
          <mxGeometry x="210" y="515" width="280" height="50" as="geometry" />
        </mxCell>
        <mxCell id="24" value="✅ 可以使用" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;fontColor=#166534;fontSize=13;" vertex="1" parent="1">
          <mxGeometry x="500" y="515" width="280" height="50" as="geometry" />
        </mxCell>
        <mxCell id="25" value="典型场景" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontColor=#1E293B;fontSize=13;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="575" width="140" height="55" as="geometry" />
        </mxCell>
        <mxCell id="26" value="工具方法、工厂方法" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#C4B5FD;strokeWidth=1;fontColor=#6D28D9;fontSize=13;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="210" y="575" width="280" height="55" as="geometry" />
        </mxCell>
        <mxCell id="27" value="操作对象状态的方法" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;fontColor=#166534;fontSize=13;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="500" y="575" width="280" height="55" as="geometry" />
        </mxCell>
        <mxCell id="28" value="💡 静态方法在类加载时就可用,无需创建对象;实例方法必须通过对象调用" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#92400E;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="170" y="655" width="560" height="30" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
public class MethodAccessDemo {

    private int instanceVar = 10;         // 实例变量
    private static int staticVar = 20;    // 静态变量

    // ═══════════════════════════════════════════════════════════════════
    // 静态方法
    // ═══════════════════════════════════════════════════════════════════
    public static void staticMethod() {
        System.out.println(staticVar);     // ✅ 可以访问静态变量
        // System.out.println(instanceVar); // ❌ 不能访问实例变量
        // System.out.println(this.instanceVar); // ❌ 不能使用 this

        anotherStaticMethod();             // ✅ 可以调用静态方法
        // instanceMethod();               // ❌ 不能调用实例方法
    }

    public static void anotherStaticMethod() {
        System.out.println("另一个静态方法");
    }

    // ═══════════════════════════════════════════════════════════════════
    // 实例方法
    // ═══════════════════════════════════════════════════════════════════
    public void instanceMethod() {
        System.out.println(instanceVar);   // ✅ 可以访问实例变量
        System.out.println(staticVar);     // ✅ 可以访问静态变量
        System.out.println(this.instanceVar); // ✅ 可以使用 this

        staticMethod();                    // ✅ 可以调用静态方法
        anotherInstanceMethod();           // ✅ 可以调用实例方法
    }

    public void anotherInstanceMethod() {
        System.out.println("另一个实例方法");
    }

    public static void main(String[] args) {
        // 静态方法调用
        MethodAccessDemo.staticMethod();    // 推荐:类名.方法名
        staticMethod();                     // 同一个类中可省略类名

        // 实例方法调用
        MethodAccessDemo demo = new MethodAccessDemo();
        demo.instanceMethod();              // 必须通过对象调用
    }
}

为什么静态方法不能访问非静态成员?

<mxfile>
  <diagram id="static-method-limitation" name="静态方法不能访问非静态成员的原因">
    <mxGraphModel dx="900" dy="750" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="750" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        
        <!-- 主容器 -->
        <mxCell id="2" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="25" y="15" width="850" height="720" as="geometry" />
        </mxCell>
        
        <!-- 标题 -->
        <mxCell id="3" value="静态方法不能访问非静态成员的原因" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;fontStyle=1;fontColor=#3949ab;" vertex="1" parent="1">
          <mxGeometry x="175" y="30" width="550" height="40" as="geometry" />
        </mxCell>
        
        <!-- ==================== 时间线部分 ==================== -->
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="45" y="85" width="810" height="280" as="geometry" />
        </mxCell>
        
        <!-- 时间线标题 -->
        <mxCell id="5" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="45" y="85" width="810" height="40" as="geometry" />
        </mxCell>
        <mxCell id="6" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 16px;&quot;&gt;&lt;b&gt;⏱️ 时间线:成员的生命周期&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="45" y="85" width="810" height="40" as="geometry" />
        </mxCell>
        
        <!-- 时间线主轴 -->
        <mxCell id="7" value="" style="endArrow=classic;html=1;strokeWidth=4;strokeColor=#2e7d32;flowAnimation=1;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="80" y="185" as="sourcePoint" />
            <mxPoint x="820" y="185" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        
        <!-- 阶段1:类加载 -->
        <mxCell id="8" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=#1565c0;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="145" width="160" height="35" as="geometry" />
        </mxCell>
        <mxCell id="9" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;① 类加载&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="80" y="145" width="160" height="35" as="geometry" />
        </mxCell>
        
        <!-- 阶段1说明 -->
        <mxCell id="10" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#64b5f6;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="65" y="210" width="190" height="85" as="geometry" />
        </mxCell>
        <mxCell id="11" value="&lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;静态成员就绪&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#424242&quot;&gt;• 静态变量赋值&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#424242&quot;&gt;• 静态方法可用&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" vertex="1" parent="1">
          <mxGeometry x="65" y="210" width="190" height="85" as="geometry" />
        </mxCell>
        
        <!-- 连接线1 -->
        <mxCell id="12" value="" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#1976d2;dashed=1;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="160" y="185" as="sourcePoint" />
            <mxPoint x="160" y="210" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        
        <!-- 阶段2:创建对象 -->
        <mxCell id="13" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=#f57c00;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="340" y="145" width="160" height="35" as="geometry" />
        </mxCell>
        <mxCell id="14" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;② 创建对象&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="340" y="145" width="160" height="35" as="geometry" />
        </mxCell>
        
        <!-- 阶段2说明 -->
        <mxCell id="15" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffe0b2;strokeColor=#ffb74d;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="325" y="210" width="190" height="85" as="geometry" />
        </mxCell>
        <mxCell id="16" value="&lt;font color=&quot;#e65100&quot;&gt;&lt;b&gt;实例成员就绪&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#424242&quot;&gt;• 实例变量赋值&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#424242&quot;&gt;&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#7b1fa2&quot;&gt;this&lt;/font&gt;&lt;font color=&quot;#424242&quot;&gt; 可用&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" vertex="1" parent="1">
          <mxGeometry x="325" y="210" width="190" height="85" as="geometry" />
        </mxCell>
        
        <!-- 连接线2 -->
        <mxCell id="17" value="" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#ff9800;dashed=1;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="420" y="185" as="sourcePoint" />
            <mxPoint x="420" y="210" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        
        <!-- 阶段3:调用实例方法 -->
        <mxCell id="18" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#9c27b0;strokeColor=#7b1fa2;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="600" y="145" width="180" height="35" as="geometry" />
        </mxCell>
        <mxCell id="19" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;③ 调用实例方法&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="600" y="145" width="180" height="35" as="geometry" />
        </mxCell>
        
        <!-- 阶段3说明 -->
        <mxCell id="20" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1bee7;strokeColor=#ba68c8;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="585" y="210" width="210" height="85" as="geometry" />
        </mxCell>
        <mxCell id="21" value="&lt;font color=&quot;#7b1fa2&quot;&gt;&lt;b&gt;所有成员可用&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#424242&quot;&gt;• 静态成员 ✅&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#424242&quot;&gt;• 实例成员 ✅&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" vertex="1" parent="1">
          <mxGeometry x="585" y="210" width="210" height="85" as="geometry" />
        </mxCell>
        
        <!-- 连接线3 -->
        <mxCell id="22" value="" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#9c27b0;dashed=1;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="690" y="185" as="sourcePoint" />
            <mxPoint x="690" y="210" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        
        <!-- 箭头标签 -->
        <mxCell id="23" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 12px;&quot;&gt;时间&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="790" y="165" width="40" height="20" as="geometry" />
        </mxCell>
        
        <!-- 时间线节点 -->
        <mxCell id="24" value="" style="ellipse;whiteSpace=wrap;html=1;fillColor=#2e7d32;strokeColor=#1b5e20;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="152" y="177" width="16" height="16" as="geometry" />
        </mxCell>
        <mxCell id="25" value="" style="ellipse;whiteSpace=wrap;html=1;fillColor=#2e7d32;strokeColor=#1b5e20;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="412" y="177" width="16" height="16" as="geometry" />
        </mxCell>
        <mxCell id="26" value="" style="ellipse;whiteSpace=wrap;html=1;fillColor=#2e7d32;strokeColor=#1b5e20;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="682" y="177" width="16" height="16" as="geometry" />
        </mxCell>
        
        <!-- ==================== 问题部分 ==================== -->
        <mxCell id="27" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffebee;strokeColor=#e53935;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="45" y="385" width="810" height="330" as="geometry" />
        </mxCell>
        
        <!-- 问题标题 -->
        <mxCell id="28" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e53935;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="45" y="385" width="810" height="40" as="geometry" />
        </mxCell>
        <mxCell id="29" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 16px;&quot;&gt;&lt;b&gt;❓ 问题:为什么静态方法不能访问非静态成员?&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="45" y="385" width="810" height="40" as="geometry" />
        </mxCell>
        
        <!-- 核心问题说明 -->
        <mxCell id="30" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#ef9a9a;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="65" y="440" width="770" height="55" as="geometry" />
        </mxCell>
        <mxCell id="31" value="&lt;font style=&quot;font-size: 15px;&quot; color=&quot;#c62828&quot;&gt;⚠️ &lt;b&gt;当调用静态方法时,可能还没有创建任何对象!&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="65" y="440" width="770" height="55" as="geometry" />
        </mxCell>
        
        <!-- 代码示例 -->
        <mxCell id="32" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#263238;strokeColor=#37474f;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="65" y="510" width="770" height="50" as="geometry" />
        </mxCell>
        <mxCell id="33" value="&lt;font face=&quot;Courier New&quot; color=&quot;#80cbc4&quot; style=&quot;font-size: 14px;&quot;&gt;ClassName.&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ffcc80&quot; style=&quot;font-size: 14px;&quot;&gt;staticMethod&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ffffff&quot; style=&quot;font-size: 14px;&quot;&gt;();&lt;/font&gt;&lt;font color=&quot;#78909c&quot; style=&quot;font-size: 13px;&quot;&gt;  // 不需要对象就能调用&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="65" y="510" width="770" height="50" as="geometry" />
        </mxCell>
        
        <!-- 此时说明标题 -->
        <mxCell id="34" value="&lt;font color=&quot;#c62828&quot; style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;此时的状态:&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="65" y="570" width="150" height="30" as="geometry" />
        </mxCell>
        
        <!-- 三个状态卡片 -->
        <!-- 静态变量已存在 -->
        <mxCell id="35" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#81c784;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="65" y="605" width="240" height="90" as="geometry" />
        </mxCell>
        <mxCell id="36" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 22px;&quot;&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="65" y="610" width="240" height="30" as="geometry" />
        </mxCell>
        <mxCell id="37" value="&lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;静态变量已存在&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#424242&quot; style=&quot;font-size: 12px;&quot;&gt;类加载时就已初始化&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="65" y="640" width="240" height="50" as="geometry" />
        </mxCell>
        
        <!-- 实例变量不存在 -->
        <mxCell id="38" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#ef9a9a;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="325" y="605" width="240" height="90" as="geometry" />
        </mxCell>
        <mxCell id="39" value="&lt;font color=&quot;#c62828&quot; style=&quot;font-size: 22px;&quot;&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="325" y="610" width="240" height="30" as="geometry" />
        </mxCell>
        <mxCell id="40" value="&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;实例变量不存在&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#424242&quot; style=&quot;font-size: 12px;&quot;&gt;没有对象,哪来的实例变量?&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="325" y="640" width="240" height="50" as="geometry" />
        </mxCell>
        
        <!-- this 不存在 -->
        <mxCell id="41" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#ef9a9a;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="585" y="605" width="240" height="90" as="geometry" />
        </mxCell>
        <mxCell id="42" value="&lt;font color=&quot;#c62828&quot; style=&quot;font-size: 22px;&quot;&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="585" y="610" width="240" height="30" as="geometry" />
        </mxCell>
        <mxCell id="43" value="&lt;font face=&quot;Courier New&quot; color=&quot;#c62828&quot;&gt;&lt;b&gt;this&lt;/b&gt;&lt;/font&gt;&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt; 不存在&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#424242&quot; style=&quot;font-size: 12px;&quot;&gt;没有对象,this 指向谁?&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="585" y="640" width="240" height="50" as="geometry" />
        </mxCell>
        
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

2.3 方法重载(Overload)

<mxfile>
  <diagram id="method-overloading" name="方法重载">
    <mxGraphModel dx="1281" dy="581" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;fontStyle=1;fontColor=#1E293B;" value="方法重载" vertex="1">
          <mxGeometry height="45" width="200" x="350" y="25" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=13;fontColor=#FFFFFF;rounded=1;" value="Method Overloading" vertex="1">
          <mxGeometry height="26" width="190" x="355" y="70" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#8B5CF6;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="90" width="740" x="80" y="120" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="定义" vertex="1">
          <mxGeometry height="32" width="70" x="100" y="135" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#4C1D95;" value="同一个类中,方法名相同,参数列表不同" vertex="1">
          <mxGeometry height="40" width="600" x="190" y="130" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#A78BFA;strokeWidth=2;fontColor=#6D28D9;fontSize=12;fontStyle=1;" value="同一个类" vertex="1">
          <mxGeometry height="28" width="90" x="200" y="170" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#8B5CF6;" value="+" vertex="1">
          <mxGeometry height="28" width="30" x="295" y="170" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#A78BFA;strokeWidth=2;fontColor=#6D28D9;fontSize=12;fontStyle=1;" value="方法名相同" vertex="1">
          <mxGeometry height="28" width="100" x="330" y="170" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#8B5CF6;" value="+" vertex="1">
          <mxGeometry height="28" width="30" x="435" y="170" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#A78BFA;strokeWidth=2;fontColor=#6D28D9;fontSize=12;fontStyle=1;" value="参数列表不同" vertex="1">
          <mxGeometry height="28" width="110" x="470" y="170" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="200" width="740" x="80" y="230" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="参数列表不同指" vertex="1">
          <mxGeometry height="32" width="140" x="100" y="248" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#166534;fontStyle=2;" value="✅ 有效的重载条件" vertex="1">
          <mxGeometry height="28" width="140" x="255" y="250" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#86EFAC;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="120" width="220" x="100" y="295" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=none;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="1" vertex="1">
          <mxGeometry height="30" width="30" x="115" y="310" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#166534;" value="参数个数不同" vertex="1">
          <mxGeometry height="30" width="150" x="155" y="310" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0FDF4;align=left;verticalAlign=middle;fontSize=12;fontFamily=Courier New;fontColor=#15803D;rounded=1;spacingLeft=8;" value="add(int a)&lt;br&gt;add(int a, int b)" vertex="1">
          <mxGeometry height="55" width="190" x="115" y="350" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#86EFAC;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="120" width="220" x="340" y="295" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=none;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="2" vertex="1">
          <mxGeometry height="30" width="30" x="355" y="310" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#166534;" value="参数类型不同" vertex="1">
          <mxGeometry height="30" width="150" x="395" y="310" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0FDF4;align=left;verticalAlign=middle;fontSize=12;fontFamily=Courier New;fontColor=#15803D;rounded=1;spacingLeft=8;" value="add(int a)&lt;br&gt;add(double a)" vertex="1">
          <mxGeometry height="55" width="190" x="355" y="350" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#86EFAC;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="120" width="220" x="580" y="295" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=none;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="3" vertex="1">
          <mxGeometry height="30" width="30" x="595" y="310" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#166534;" value="参数顺序不同" vertex="1">
          <mxGeometry height="30" width="150" x="635" y="310" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0FDF4;align=left;verticalAlign=middle;fontSize=12;fontFamily=Courier New;fontColor=#15803D;rounded=1;spacingLeft=8;" value="add(int a, double b)&lt;br&gt;add(double a, int b)" vertex="1">
          <mxGeometry height="55" width="190" x="595" y="350" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#92400E;rounded=1;" value="⚠️ 不推荐" vertex="1">
          <mxGeometry height="22" width="65" x="729" y="314" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="130" width="740" x="80" y="450" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="⚠️ 注意事项" vertex="1">
          <mxGeometry height="32" width="120" x="100" y="468" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#DC2626;fontStyle=2;" value="❌ 无效的重载条件" vertex="1">
          <mxGeometry height="28" width="140" x="235" y="470" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FCA5A5;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="340" x="100" y="515" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;" value="❌" vertex="1">
          <mxGeometry height="30" width="30" x="110" y="525" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#DC2626;" value="与返回值类型无关!" vertex="1">
          <mxGeometry height="30" width="180" x="145" y="525" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF2F2;align=center;verticalAlign=middle;fontSize=11;fontColor=#991B1B;rounded=1;" value="仅返回值不同不构成重载" vertex="1">
          <mxGeometry height="24" width="120" x="310" y="528" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FCA5A5;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="340" x="460" y="515" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;" value="❌" vertex="1">
          <mxGeometry height="30" width="30" x="470" y="525" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#DC2626;" value="与参数名无关!" vertex="1">
          <mxGeometry height="30" width="150" x="505" y="525" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF2F2;align=center;verticalAlign=middle;fontSize=11;fontColor=#991B1B;rounded=1;" value="仅参数名不同不构成重载" vertex="1">
          <mxGeometry height="24" width="120" x="670" y="528" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="💡 编译器通过参数列表区分重载方法,调用时根据传入参数自动匹配" vertex="1">
          <mxGeometry height="30" width="500" x="200" y="605" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
public class OverloadDemo {

    // ═══════════════════════════════════════════════════════════════════
    // ✅ 正确的重载
    // ═══════════════════════════════════════════════════════════════════

    // 参数个数不同
    public int add(int a, int b) {
        return a + b;
    }

    public int add(int a, int b, int c) {
        return a + b + c;
    }

    // 参数类型不同
    public double add(double a, double b) {
        return a + b;
    }

    public String add(String a, String b) {
        return a + b;
    }

    // ═══════════════════════════════════════════════════════════════════
    // ❌ 错误的重载
    // ═══════════════════════════════════════════════════════════════════

    // 只有返回值不同,不构成重载
    // public double add(int a, int b) {  // ❌ 编译错误
    //     return a + b;
    // }

    // 只有参数名不同,不构成重载
    // public int add(int x, int y) {  // ❌ 编译错误
    //     return x + y;
    // }

    public static void main(String[] args) {
        OverloadDemo demo = new OverloadDemo();

        // 编译器根据参数类型和个数选择合适的方法
        System.out.println(demo.add(1, 2));          // 调用 add(int, int)
        System.out.println(demo.add(1, 2, 3));       // 调用 add(int, int, int)
        System.out.println(demo.add(1.5, 2.5));      // 调用 add(double, double)
        System.out.println(demo.add("Hello", "!"));  // 调用 add(String, String)
    }
}

重载的编译期绑定

<mxfile>
  <diagram id="static-binding-fixed" name="重载的静态绑定-修复版">
    <mxGraphModel dx="2340" dy="1001" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="670" width="850" x="25" y="15" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=26;fontStyle=1;fontColor=#3949ab;" value="重载的静态绑定" vertex="1">
          <mxGeometry height="40" width="350" x="275" y="30" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="710" x="95" y="80" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#3949ab&quot; style=&quot;font-size: 14px;&quot;&gt;重载方法的选择发生在&lt;b&gt;编译期&lt;/b&gt;,称为&quot;&lt;b&gt;静态绑定&lt;/b&gt;&quot;&quot;&lt;b&gt;早期绑定&lt;/b&gt;&quot;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="710" x="95" y="80" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#263238;strokeColor=#37474f;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="50" width="300" x="300" y="150" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#80cbc4&quot; style=&quot;font-size: 15px;&quot;&gt;demo.&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ffcc80&quot; style=&quot;font-size: 15px;&quot;&gt;add&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ffffff&quot; style=&quot;font-size: 15px;&quot;&gt;(&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ce93d8&quot; style=&quot;font-size: 15px;&quot;&gt;1&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ffffff&quot; style=&quot;font-size: 15px;&quot;&gt;, &lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ce93d8&quot; style=&quot;font-size: 15px;&quot;&gt;2&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ffffff&quot; style=&quot;font-size: 15px;&quot;&gt;);&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="300" x="300" y="150" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=#f57c00;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="35" width="100" x="60" y="235" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;⚙️ 编译期&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="100" x="60" y="235" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ff9800;strokeWidth=2;dashed=1;dashPattern=5 5;" value="" vertex="1">
          <mxGeometry height="330" width="680" x="170" y="220" as="geometry" />
        </mxCell>
        <mxCell id="11" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#ff9800;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="450" y="200" as="sourcePoint" />
            <mxPoint x="450" y="250" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="60" width="380" x="260" y="260" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;① 编译器分析参数&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#424242&quot;&gt;两个 &lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#7b1fa2&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt;&lt;font color=&quot;#424242&quot;&gt; 类型&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="380" x="260" y="260" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;" value="🔍" vertex="1">
          <mxGeometry height="50" width="50" x="660" y="265" as="geometry" />
        </mxCell>
        <mxCell id="15" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#ff9800;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="450" y="320" as="sourcePoint" />
            <mxPoint x="450" y="360" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="60" width="380" x="260" y="370" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;② 匹配方法签名&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#424242&quot;&gt;add(&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#7b1fa2&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#424242&quot;&gt;, &lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#7b1fa2&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#424242&quot;&gt;)&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="380" x="260" y="370" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;" value="🎯" vertex="1">
          <mxGeometry height="50" width="50" x="660" y="375" as="geometry" />
        </mxCell>
        <mxCell id="19" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#ff9800;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="450" y="430" as="sourcePoint" />
            <mxPoint x="450" y="470" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fce4ec;strokeColor=#e91e63;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="60" width="480" x="210" y="480" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#c2185b&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;③ 生成字节码&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#424242&quot;&gt;invokevirtual #2 &lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#78909c&quot;&gt;// add:(II)I&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="480" x="210" y="480" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;" value="📝" vertex="1">
          <mxGeometry height="50" width="50" x="710" y="485" as="geometry" />
        </mxCell>
        <mxCell id="23" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#9c27b0;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="450" y="540" as="sourcePoint" />
            <mxPoint x="450" y="590" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#9c27b0;strokeColor=#7b1fa2;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="35" width="100" x="60" y="595" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;▶️ 运行时&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="100" x="60" y="595" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f3e5f5;strokeColor=#9c27b0;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="65" width="680" x="170" y="580" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#7b1fa2&quot; style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;直接调用 &lt;/b&gt;&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#7b1fa2&quot; style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;add(int, int)&lt;/b&gt;&lt;/font&gt;&lt;font color=&quot;#7b1fa2&quot; style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;,不需要再选择&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="65" width="680" x="170" y="580" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=28;" value="⚡" vertex="1">
          <mxGeometry height="50" width="50" x="780" y="590" as="geometry" />
        </mxCell>
        <mxCell id="29" edge="1" parent="1" style="endArrow=none;html=1;strokeWidth=4;strokeColor=#ff9800;dashed=1;dashPattern=1 2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="160" y="270" as="sourcePoint" />
            <mxPoint x="160" y="530" as="targetPoint" />
          </mxGeometry>
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

2.4 方法重写(Override)

<mxfile>
  <diagram id="method-override" name="方法重写">
    <mxGraphModel dx="900" dy="850" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="850">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" value="方法重写" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;fontStyle=1;fontColor=#1E293B;" vertex="1" parent="1">
          <mxGeometry x="350" y="20" width="200" height="45" as="geometry" />
        </mxCell>
        <mxCell id="3" value="Method Override" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=13;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="365" y="65" width="170" height="26" as="geometry" />
        </mxCell>
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#8B5CF6;strokeWidth=3;" vertex="1" parent="1">
          <mxGeometry x="80" y="110" width="740" height="70" as="geometry" />
        </mxCell>
        <mxCell id="5" value="定义" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="128" width="70" height="32" as="geometry" />
        </mxCell>
        <mxCell id="6" value="子类重新实现父类的方法" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#4C1D95;" vertex="1" parent="1">
          <mxGeometry x="190" y="125" width="350" height="40" as="geometry" />
        </mxCell>
        <mxCell id="7" value="@Override" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=center;verticalAlign=middle;fontSize=12;fontFamily=Courier New;fontColor=#6D28D9;rounded=1;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="680" y="130" width="120" height="28" as="geometry" />
        </mxCell>
        <mxCell id="8" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;" vertex="1" parent="1">
          <mxGeometry x="80" y="200" width="740" height="340" as="geometry" />
        </mxCell>
        <mxCell id="9" value="重写规则" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="218" width="100" height="32" as="geometry" />
        </mxCell>
        <mxCell id="10" value="两同两小一大" style="text;html=1;strokeColor=none;fillColor=#166534;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="215" y="218" width="120" height="32" as="geometry" />
        </mxCell>
        <mxCell id="11" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#86EFAC;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="100" y="268" width="230" height="130" as="geometry" />
        </mxCell>
        <mxCell id="12" value="两同" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="115" y="280" width="60" height="28" as="geometry" />
        </mxCell>
        <mxCell id="13" value="✅ 方法名相同" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#166534;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="115" y="318" width="200" height="30" as="geometry" />
        </mxCell>
        <mxCell id="14" value="✅ 参数列表相同" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#166534;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="115" y="353" width="200" height="30" as="geometry" />
        </mxCell>
        <mxCell id="15" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#86EFAC;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="350" y="268" width="280" height="130" as="geometry" />
        </mxCell>
        <mxCell id="16" value="两小" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="365" y="280" width="60" height="28" as="geometry" />
        </mxCell>
        <mxCell id="17" value="✅ 返回值类型相同或是子类" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#166534;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="365" y="318" width="250" height="26" as="geometry" />
        </mxCell>
        <mxCell id="18" value="协变返回类型" style="text;html=1;strokeColor=none;fillColor=#F0FDF4;align=center;verticalAlign=middle;fontSize=10;fontColor=#15803D;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="510" y="320" width="80" height="20" as="geometry" />
        </mxCell>
        <mxCell id="19" value="✅ 抛出异常相同或是子类" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#166534;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="365" y="358" width="250" height="26" as="geometry" />
        </mxCell>
        <mxCell id="20" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#86EFAC;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="650" y="268" width="150" height="130" as="geometry" />
        </mxCell>
        <mxCell id="21" value="一大" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="665" y="280" width="60" height="28" as="geometry" />
        </mxCell>
        <mxCell id="22" value="✅ 访问权限&lt;br&gt;相同或更大" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#166534;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="665" y="320" width="120" height="50" as="geometry" />
        </mxCell>
        <mxCell id="23" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#86EFAC;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="415" width="700" height="110" as="geometry" />
        </mxCell>
        <mxCell id="24" value="📝 示例" style="text;html=1;strokeColor=none;fillColor=#166534;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="115" y="425" width="70" height="24" as="geometry" />
        </mxCell>
        <mxCell id="25" value="// 父类&lt;br&gt;protected Object getValue() throws Exception" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#166534;rounded=1;spacingLeft=8;" vertex="1" parent="1">
          <mxGeometry x="115" y="458" width="310" height="55" as="geometry" />
        </mxCell>
        <mxCell id="26" value="→" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#22C55E;" vertex="1" parent="1">
          <mxGeometry x="430" y="465" width="40" height="40" as="geometry" />
        </mxCell>
        <mxCell id="27" value="// 子类重写&lt;br&gt;public String getValue() throws IOException" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#166534;rounded=1;spacingLeft=8;" vertex="1" parent="1">
          <mxGeometry x="475" y="458" width="310" height="55" as="geometry" />
        </mxCell>
        <mxCell id="28" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=3;" vertex="1" parent="1">
          <mxGeometry x="80" y="560" width="740" height="180" as="geometry" />
        </mxCell>
        <mxCell id="29" value="不能重写的方法" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="578" width="140" height="32" as="geometry" />
        </mxCell>
        <mxCell id="30" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FCA5A5;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="100" y="628" width="220" height="95" as="geometry" />
        </mxCell>
        <mxCell id="31" value="❌" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=22;" vertex="1" parent="1">
          <mxGeometry x="115" y="640" width="30" height="30" as="geometry" />
        </mxCell>
        <mxCell id="32" value="private 方法" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#DC2626;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="150" y="640" width="150" height="30" as="geometry" />
        </mxCell>
        <mxCell id="33" value="子类不可见" style="text;html=1;strokeColor=none;fillColor=#FEF2F2;align=center;verticalAlign=middle;fontSize=12;fontColor=#991B1B;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="130" y="680" width="170" height="28" as="geometry" />
        </mxCell>
        <mxCell id="34" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FCA5A5;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="340" y="628" width="220" height="95" as="geometry" />
        </mxCell>
        <mxCell id="35" value="❌" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=22;" vertex="1" parent="1">
          <mxGeometry x="355" y="640" width="30" height="30" as="geometry" />
        </mxCell>
        <mxCell id="36" value="final 方法" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#DC2626;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="390" y="640" width="150" height="30" as="geometry" />
        </mxCell>
        <mxCell id="37" value="禁止重写" style="text;html=1;strokeColor=none;fillColor=#FEF2F2;align=center;verticalAlign=middle;fontSize=12;fontColor=#991B1B;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="370" y="680" width="170" height="28" as="geometry" />
        </mxCell>
        <mxCell id="38" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FCA5A5;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="580" y="628" width="220" height="95" as="geometry" />
        </mxCell>
        <mxCell id="39" value="❌" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=22;" vertex="1" parent="1">
          <mxGeometry x="595" y="640" width="30" height="30" as="geometry" />
        </mxCell>
        <mxCell id="40" value="static 方法" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#DC2626;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="630" y="640" width="150" height="30" as="geometry" />
        </mxCell>
        <mxCell id="41" value="属于类,不属于对象" style="text;html=1;strokeColor=none;fillColor=#FEF2F2;align=center;verticalAlign=middle;fontSize=12;fontColor=#991B1B;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="610" y="680" width="170" height="28" as="geometry" />
        </mxCell>
        <mxCell id="42" value="💡 重写实现多态:父类引用调用子类重写的方法,执行子类的实现" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="200" y="765" width="500" height="30" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
class Animal {
    protected String name;
    
    public void eat() {
        System.out.println("动物吃东西");
    }
    
    public Animal getAnimal() {
        return new Animal();
    }
    
    // private 方法不能被重写
    private void privateMethod() {
        System.out.println("私有方法");
    }
    
    // final 方法不能被重写
    public final void finalMethod() {
        System.out.println("final方法");
    }
    
    // static 方法不是重写,是隐藏
    public static void staticMethod() {
        System.out.println("Animal的静态方法");
    }
}

class Dog extends Animal {
    
    // ═══════════════════════════════════════════════════════════════════
    // ✅ 正确的重写
    // ═══════════════════════════════════════════════════════════════════
    @Override
    public void eat() {  // 方法名、参数列表相同
        System.out.println("狗吃骨头");
    }
    
    // 协变返回类型:返回值可以是父类返回值的子类
    @Override
    public Dog getAnimal() {  // 返回 Dog 是 Animal 的子类
        return new Dog();
    }
    
    // ═══════════════════════════════════════════════════════════════════
    // ❌ 错误的重写
    // ═══════════════════════════════════════════════════════════════════
    
    // 不能缩小访问权限
    // @Override
    // void eat() { }  // ❌ default < protected
    
    // 不能重写 final 方法
    // @Override
    // public void finalMethod() { }  // ❌ 编译错误
    
    // static 方法是隐藏,不是重写
    public static void staticMethod() {  // 没有 @Override
        System.out.println("Dog的静态方法");
    }
}
<mxfile>
  <diagram id="overload-vs-override" name="重载vs重写对比">
    <mxGraphModel dx="900" dy="750" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="750">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" value="重载 vs 重写 对比" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;fontStyle=1;fontColor=#1E293B;" vertex="1" parent="1">
          <mxGeometry x="250" y="20" width="400" height="45" as="geometry" />
        </mxCell>
        <mxCell id="3" value="Overload vs Override" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=13;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="345" y="65" width="210" height="26" as="geometry" />
        </mxCell>
        <mxCell id="4" value="对比项" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#374151;strokeColor=#1F2937;strokeWidth=2;fontColor=#FFFFFF;fontSize=15;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="115" width="120" height="50" as="geometry" />
        </mxCell>
        <mxCell id="5" value="重载 Overload" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#8B5CF6;strokeColor=#7C3AED;strokeWidth=2;fontColor=#FFFFFF;fontSize=15;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="210" y="115" width="290" height="50" as="geometry" />
        </mxCell>
        <mxCell id="6" value="重写 Override" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=2;fontColor=#FFFFFF;fontSize=15;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="510" y="115" width="310" height="50" as="geometry" />
        </mxCell>
        <mxCell id="7" value="发生位置" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3F4F6;strokeColor=#D1D5DB;strokeWidth=1;fontColor=#374151;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="175" width="120" height="50" as="geometry" />
        </mxCell>
        <mxCell id="8" value="同一个类中" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#C4B5FD;strokeWidth=1;fontColor=#5B21B6;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="210" y="175" width="290" height="50" as="geometry" />
        </mxCell>
        <mxCell id="9" value="父子类之间" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;fontColor=#166534;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="510" y="175" width="310" height="50" as="geometry" />
        </mxCell>
        <mxCell id="10" value="方法名" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3F4F6;strokeColor=#D1D5DB;strokeWidth=1;fontColor=#374151;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="235" width="120" height="50" as="geometry" />
        </mxCell>
        <mxCell id="11" value="必须相同" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#C4B5FD;strokeWidth=1;fontColor=#5B21B6;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="210" y="235" width="290" height="50" as="geometry" />
        </mxCell>
        <mxCell id="12" value="必须相同" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;fontColor=#166534;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="510" y="235" width="310" height="50" as="geometry" />
        </mxCell>
        <mxCell id="13" value="参数列表" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3F4F6;strokeColor=#D1D5DB;strokeWidth=1;fontColor=#374151;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="295" width="120" height="50" as="geometry" />
        </mxCell>
        <mxCell id="14" value="⚡ 必须不同" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DDD6FE;strokeColor=#A78BFA;strokeWidth=2;fontColor=#5B21B6;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="210" y="295" width="290" height="50" as="geometry" />
        </mxCell>
        <mxCell id="15" value="⚡ 必须相同" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#BBF7D0;strokeColor=#4ADE80;strokeWidth=2;fontColor=#166534;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="510" y="295" width="310" height="50" as="geometry" />
        </mxCell>
        <mxCell id="16" value="返回值" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3F4F6;strokeColor=#D1D5DB;strokeWidth=1;fontColor=#374151;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="355" width="120" height="50" as="geometry" />
        </mxCell>
        <mxCell id="17" value="无要求" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#C4B5FD;strokeWidth=1;fontColor=#5B21B6;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="210" y="355" width="290" height="50" as="geometry" />
        </mxCell>
        <mxCell id="18" value="相同或子类(协变返回)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;fontColor=#166534;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="510" y="355" width="310" height="50" as="geometry" />
        </mxCell>
        <mxCell id="19" value="访问权限" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3F4F6;strokeColor=#D1D5DB;strokeWidth=1;fontColor=#374151;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="415" width="120" height="50" as="geometry" />
        </mxCell>
        <mxCell id="20" value="无要求" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#C4B5FD;strokeWidth=1;fontColor=#5B21B6;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="210" y="415" width="290" height="50" as="geometry" />
        </mxCell>
        <mxCell id="21" value="不能更小(≥ 父类)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;fontColor=#166534;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="510" y="415" width="310" height="50" as="geometry" />
        </mxCell>
        <mxCell id="22" value="绑定时机" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3F4F6;strokeColor=#D1D5DB;strokeWidth=1;fontColor=#374151;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="475" width="120" height="50" as="geometry" />
        </mxCell>
        <mxCell id="23" value="编译期(静态绑定)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#C4B5FD;strokeWidth=1;fontColor=#5B21B6;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="210" y="475" width="290" height="50" as="geometry" />
        </mxCell>
        <mxCell id="24" value="运行期(动态绑定)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;fontColor=#166534;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="510" y="475" width="310" height="50" as="geometry" />
        </mxCell>
        <mxCell id="25" value="多态体现" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3F4F6;strokeColor=#D1D5DB;strokeWidth=1;fontColor=#374151;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="535" width="120" height="50" as="geometry" />
        </mxCell>
        <mxCell id="26" value="编译时多态" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DDD6FE;strokeColor=#A78BFA;strokeWidth=2;fontColor=#5B21B6;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="210" y="535" width="290" height="50" as="geometry" />
        </mxCell>
        <mxCell id="27" value="运行时多态" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#BBF7D0;strokeColor=#4ADE80;strokeWidth=2;fontColor=#166534;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="510" y="535" width="310" height="50" as="geometry" />
        </mxCell>
        <mxCell id="28" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="80" y="610" width="740" height="80" as="geometry" />
        </mxCell>
        <mxCell id="29" value="💡 核心区别" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="625" width="100" height="28" as="geometry" />
        </mxCell>
        <mxCell id="30" value="重载:同类中方法名同、参数不同 → 编译器根据参数选择方法" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=center;verticalAlign=middle;fontSize=12;fontColor=#92400E;rounded=1;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="658" width="340" height="24" as="geometry" />
        </mxCell>
        <mxCell id="31" value="重写:父子类方法签名相同 → JVM运行时根据对象类型选择方法" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=center;verticalAlign=middle;fontSize=12;fontColor=#92400E;rounded=1;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="460" y="658" width="345" height="24" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

2.5 可变参数(Varargs)

<mxfile>
  <diagram id="varargs" name="可变参数">
    <mxGraphModel dx="900" dy="680" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="680" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        
        <!-- 主容器 -->
        <mxCell id="2" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="25" y="15" width="850" height="650" as="geometry" />
        </mxCell>
        
        <!-- 标题 -->
        <mxCell id="3" value="可变参数" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=28;fontStyle=1;fontColor=#3949ab;" vertex="1" parent="1">
          <mxGeometry x="325" y="30" width="250" height="45" as="geometry" />
        </mxCell>
        
        <!-- ==================== 语法部分 ==================== -->
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="45" y="90" width="810" height="100" as="geometry" />
        </mxCell>
        
        <!-- 语法标题 -->
        <mxCell id="5" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="45" y="90" width="100" height="35" as="geometry" />
        </mxCell>
        <mxCell id="6" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;📝 语法&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="45" y="90" width="100" height="35" as="geometry" />
        </mxCell>
        
        <!-- 语法代码 -->
        <mxCell id="7" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#263238;strokeColor=#37474f;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="65" y="140" width="770" height="40" as="geometry" />
        </mxCell>
        <mxCell id="8" value="&lt;font face=&quot;Courier New&quot; color=&quot;#ce93d8&quot; style=&quot;font-size: 16px;&quot;&gt;类型&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ffcc80&quot; style=&quot;font-size: 16px;&quot;&gt;&lt;b&gt;...&lt;/b&gt;&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ffffff&quot; style=&quot;font-size: 16px;&quot;&gt; 参数名&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="65" y="140" width="770" height="40" as="geometry" />
        </mxCell>
        
        <!-- ==================== 本质部分 ==================== -->
        <mxCell id="9" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ff9800;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="45" y="210" width="810" height="110" as="geometry" />
        </mxCell>
        
        <!-- 本质标题 -->
        <mxCell id="10" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="45" y="210" width="100" height="35" as="geometry" />
        </mxCell>
        <mxCell id="11" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;⚙️ 本质&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="45" y="210" width="100" height="35" as="geometry" />
        </mxCell>
        
        <!-- 本质说明 -->
        <mxCell id="12" value="&lt;font color=&quot;#e65100&quot; style=&quot;font-size: 16px;&quot;&gt;&lt;b&gt;编译时转换为数组&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="45" y="250" width="810" height="30" as="geometry" />
        </mxCell>
        
        <!-- 转换示意 -->
        <mxCell id="13" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#263238;strokeColor=#37474f;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="280" width="200" height="30" as="geometry" />
        </mxCell>
        <mxCell id="14" value="&lt;font face=&quot;Courier New&quot; color=&quot;#80cbc4&quot;&gt;int... nums&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" vertex="1" parent="1">
          <mxGeometry x="100" y="280" width="200" height="30" as="geometry" />
        </mxCell>
        
        <!-- 箭头 -->
        <mxCell id="15" value="" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#ff9800;flowAnimation=1;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="320" y="295" as="sourcePoint" />
            <mxPoint x="420" y="295" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="16" value="&lt;font color=&quot;#e65100&quot;&gt;编译&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" vertex="1" parent="1">
          <mxGeometry x="345" y="270" width="50" height="20" as="geometry" />
        </mxCell>
        
        <mxCell id="17" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#263238;strokeColor=#37474f;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="440" y="280" width="200" height="30" as="geometry" />
        </mxCell>
        <mxCell id="18" value="&lt;font face=&quot;Courier New&quot; color=&quot;#80cbc4&quot;&gt;int[] nums&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" vertex="1" parent="1">
          <mxGeometry x="440" y="280" width="200" height="30" as="geometry" />
        </mxCell>
        
        <!-- 等号图标 -->
        <mxCell id="19" value="&lt;font color=&quot;#4caf50&quot; style=&quot;font-size: 20px;&quot;&gt;&lt;b&gt;&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="660" y="275" width="40" height="40" as="geometry" />
        </mxCell>
        
        <!-- ==================== 规则部分 ==================== -->
        <mxCell id="20" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="45" y="340" width="810" height="305" as="geometry" />
        </mxCell>
        
        <!-- 规则标题 -->
        <mxCell id="21" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="45" y="340" width="100" height="35" as="geometry" />
        </mxCell>
        <mxCell id="22" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;📋 规则&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="45" y="340" width="100" height="35" as="geometry" />
        </mxCell>
        
        <!-- 规则1 -->
        <mxCell id="23" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#81c784;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="65" y="390" width="370" height="70" as="geometry" />
        </mxCell>
        <mxCell id="24" value="" style="ellipse;whiteSpace=wrap;html=1;fillColor=#2e7d32;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="80" y="405" width="40" height="40" as="geometry" />
        </mxCell>
        <mxCell id="25" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;1&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;" vertex="1" parent="1">
          <mxGeometry x="80" y="405" width="40" height="40" as="geometry" />
        </mxCell>
        <mxCell id="26" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;一个方法最多只能有&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;一个可变参数&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="130" y="390" width="290" height="70" as="geometry" />
        </mxCell>
        
        <!-- 规则2 -->
        <mxCell id="27" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#81c784;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="465" y="390" width="370" height="70" as="geometry" />
        </mxCell>
        <mxCell id="28" value="" style="ellipse;whiteSpace=wrap;html=1;fillColor=#2e7d32;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="480" y="405" width="40" height="40" as="geometry" />
        </mxCell>
        <mxCell id="29" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;2&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;" vertex="1" parent="1">
          <mxGeometry x="480" y="405" width="40" height="40" as="geometry" />
        </mxCell>
        <mxCell id="30" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;可变参数必须是&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;最后一个参数&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="530" y="390" width="290" height="70" as="geometry" />
        </mxCell>
        
        <!-- 规则3 -->
        <mxCell id="31" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#81c784;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="65" y="480" width="370" height="70" as="geometry" />
        </mxCell>
        <mxCell id="32" value="" style="ellipse;whiteSpace=wrap;html=1;fillColor=#2e7d32;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="80" y="495" width="40" height="40" as="geometry" />
        </mxCell>
        <mxCell id="33" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;3&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;" vertex="1" parent="1">
          <mxGeometry x="80" y="495" width="40" height="40" as="geometry" />
        </mxCell>
        <mxCell id="34" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;可变参数可以接收&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;0 个或多个值&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="130" y="480" width="290" height="70" as="geometry" />
        </mxCell>
        
        <!-- 规则4 -->
        <mxCell id="35" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#81c784;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="465" y="480" width="370" height="70" as="geometry" />
        </mxCell>
        <mxCell id="36" value="" style="ellipse;whiteSpace=wrap;html=1;fillColor=#2e7d32;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="480" y="495" width="40" height="40" as="geometry" />
        </mxCell>
        <mxCell id="37" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;4&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;" vertex="1" parent="1">
          <mxGeometry x="480" y="495" width="40" height="40" as="geometry" />
        </mxCell>
        <mxCell id="38" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;可变参数也可以&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;直接接收数组&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="530" y="480" width="290" height="70" as="geometry" />
        </mxCell>
        
        <!-- 代码示例区域 -->
        <mxCell id="39" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#263238;strokeColor=#37474f;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="65" y="570" width="770" height="60" as="geometry" />
        </mxCell>
        <mxCell id="40" value="&lt;font face=&quot;Courier New&quot; color=&quot;#78909c&quot; style=&quot;font-size: 12px;&quot;&gt;// 示例&lt;/font&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ce93d8&quot; style=&quot;font-size: 13px;&quot;&gt;void&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ffffff&quot; style=&quot;font-size: 13px;&quot;&gt; &lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ffcc80&quot; style=&quot;font-size: 13px;&quot;&gt;print&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ffffff&quot; style=&quot;font-size: 13px;&quot;&gt;(&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#80cbc4&quot; style=&quot;font-size: 13px;&quot;&gt;String&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ffcc80&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;b&gt;...&lt;/b&gt;&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ffffff&quot; style=&quot;font-size: 13px;&quot;&gt; messages) { &lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#78909c&quot; style=&quot;font-size: 13px;&quot;&gt;/* messages 是 String[] */&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#ffffff&quot; style=&quot;font-size: 13px;&quot;&gt; }&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="65" y="570" width="770" height="60" as="geometry" />
        </mxCell></root>
    </mxGraphModel>
  </diagram>
</mxfile>
public class VarargsDemo {
    
    // ═══════════════════════════════════════════════════════════════════
    // 可变参数方法
    // ═══════════════════════════════════════════════════════════════════
    public int sum(int... numbers) {
        // numbers 本质是 int[] 数组
        int total = 0;
        for (int num : numbers) {
            total += num;
        }
        return total;
    }
    
    // 可变参数必须在最后
    public void print(String prefix, String... messages) {
        for (String msg : messages) {
            System.out.println(prefix + msg);
        }
    }
    
    // ❌ 错误:可变参数不是最后一个
    // public void wrong(String... messages, String suffix) { }
    
    // ❌ 错误:多个可变参数
    // public void wrong(String... a, int... b) { }
    
    public static void main(String[] args) {
        VarargsDemo demo = new VarargsDemo();
        
        // 可变参数的多种调用方式
        System.out.println(demo.sum());           // 0 个参数,结果:0
        System.out.println(demo.sum(1));          // 1 个参数,结果:1
        System.out.println(demo.sum(1, 2, 3));    // 3 个参数,结果:6
        System.out.println(demo.sum(1, 2, 3, 4, 5)); // 5 个参数,结果:15
        
        // 也可以直接传入数组
        int[] nums = {1, 2, 3, 4, 5};
        System.out.println(demo.sum(nums));       // 传入数组,结果:15
    }
}
<mxfile>
  <diagram id="varargs-overload-priority" name="可变参数与重载优先级">
    <mxGraphModel dx="900" dy="700" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" value="可变参数与重载的优先级" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#1E293B;" vertex="1" parent="1">
          <mxGeometry x="225" y="20" width="450" height="40" as="geometry" />
        </mxCell>
        <mxCell id="3" value="Varargs &amp; Overload Priority" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="330" y="60" width="240" height="24" as="geometry" />
        </mxCell>
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;" vertex="1" parent="1">
          <mxGeometry x="80" y="100" width="740" height="50" as="geometry" />
        </mxCell>
        <mxCell id="5" value="⚡ 核心规则" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="112" width="100" height="26" as="geometry" />
        </mxCell>
        <mxCell id="6" value="当存在重载时,优先匹配确定参数的方法" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#92400E;" vertex="1" parent="1">
          <mxGeometry x="220" y="108" width="500" height="34" as="geometry" />
        </mxCell>
        <mxCell id="7" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="80" y="170" width="360" height="130" as="geometry" />
        </mxCell>
        <mxCell id="8" value="方法1:确定参数" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="185" width="130" height="26" as="geometry" />
        </mxCell>
        <mxCell id="9" value="⭐ 高优先级" style="text;html=1;strokeColor=none;fillColor=#166534;align=center;verticalAlign=middle;fontSize=11;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="300" y="185" width="80" height="24" as="geometry" />
        </mxCell>
        <mxCell id="10" value="public void test(int a, int b) { }" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=left;verticalAlign=middle;fontSize=13;fontFamily=Courier New;fontColor=#166534;rounded=1;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="100" y="225" width="320" height="40" as="geometry" />
        </mxCell>
        <mxCell id="11" value="参数个数和类型明确" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontColor=#166534;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="140" y="270" width="240" height="22" as="geometry" />
        </mxCell>
        <mxCell id="12" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#8B5CF6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="460" y="170" width="360" height="130" as="geometry" />
        </mxCell>
        <mxCell id="13" value="方法2:可变参数" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="480" y="185" width="130" height="26" as="geometry" />
        </mxCell>
        <mxCell id="14" value="低优先级" style="text;html=1;strokeColor=none;fillColor=#5B21B6;align=center;verticalAlign=middle;fontSize=11;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="700" y="185" width="70" height="24" as="geometry" />
        </mxCell>
        <mxCell id="15" value="public void test(int... nums) { }" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=left;verticalAlign=middle;fontSize=13;fontFamily=Courier New;fontColor=#5B21B6;rounded=1;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="480" y="225" width="320" height="40" as="geometry" />
        </mxCell>
        <mxCell id="16" value="可接受 0 到多个参数" style="text;html=1;strokeColor=none;fillColor=#DDD6FE;align=center;verticalAlign=middle;fontSize=12;fontColor=#5B21B6;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="520" y="270" width="240" height="22" as="geometry" />
        </mxCell>
        <mxCell id="17" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#94A3B8;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="80" y="320" width="740" height="280" as="geometry" />
        </mxCell>
        <mxCell id="18" value="📞 调用示例" style="text;html=1;strokeColor=none;fillColor=#475569;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="335" width="110" height="28" as="geometry" />
        </mxCell>
        <mxCell id="19" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="100" y="380" width="340" height="90" as="geometry" />
        </mxCell>
        <mxCell id="20" value="test(1, 2);" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=center;verticalAlign=middle;fontSize=15;fontFamily=Courier New;fontColor=#166534;fontStyle=1;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="120" y="395" width="140" height="35" as="geometry" />
        </mxCell>
        <mxCell id="21" value="→" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#22C55E;" vertex="1" parent="1">
          <mxGeometry x="265" y="395" width="40" height="35" as="geometry" />
        </mxCell>
        <mxCell id="22" value="调用方法1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="310" y="400" width="110" height="28" as="geometry" />
        </mxCell>
        <mxCell id="23" value="✅ 优先匹配确定参数" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#166534;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="140" y="438" width="280" height="22" as="geometry" />
        </mxCell>
        <mxCell id="24" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#8B5CF6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="460" y="380" width="340" height="200" as="geometry" />
        </mxCell>
        <mxCell id="25" value="test(1);" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=center;verticalAlign=middle;fontSize=14;fontFamily=Courier New;fontColor=#5B21B6;fontStyle=1;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="480" y="395" width="100" height="32" as="geometry" />
        </mxCell>
        <mxCell id="26" value="→" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#8B5CF6;" vertex="1" parent="1">
          <mxGeometry x="590" y="395" width="30" height="32" as="geometry" />
        </mxCell>
        <mxCell id="27" value="调用方法2" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="625" y="400" width="90" height="24" as="geometry" />
        </mxCell>
        <mxCell id="28" value="1个参数" style="text;html=1;strokeColor=none;fillColor=#DDD6FE;align=center;verticalAlign=middle;fontSize=10;fontColor=#5B21B6;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="720" y="402" width="60" height="20" as="geometry" />
        </mxCell>
        <mxCell id="29" value="test(1, 2, 3);" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=center;verticalAlign=middle;fontSize=14;fontFamily=Courier New;fontColor=#5B21B6;fontStyle=1;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="480" y="440" width="100" height="32" as="geometry" />
        </mxCell>
        <mxCell id="30" value="→" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#8B5CF6;" vertex="1" parent="1">
          <mxGeometry x="590" y="440" width="30" height="32" as="geometry" />
        </mxCell>
        <mxCell id="31" value="调用方法2" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="625" y="445" width="90" height="24" as="geometry" />
        </mxCell>
        <mxCell id="32" value="3个参数" style="text;html=1;strokeColor=none;fillColor=#DDD6FE;align=center;verticalAlign=middle;fontSize=10;fontColor=#5B21B6;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="720" y="447" width="60" height="20" as="geometry" />
        </mxCell>
        <mxCell id="33" value="test();" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=center;verticalAlign=middle;fontSize=14;fontFamily=Courier New;fontColor=#5B21B6;fontStyle=1;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="480" y="485" width="100" height="32" as="geometry" />
        </mxCell>
        <mxCell id="34" value="→" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#8B5CF6;" vertex="1" parent="1">
          <mxGeometry x="590" y="485" width="30" height="32" as="geometry" />
        </mxCell>
        <mxCell id="35" value="调用方法2" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="625" y="490" width="90" height="24" as="geometry" />
        </mxCell>
        <mxCell id="36" value="0个参数" style="text;html=1;strokeColor=none;fillColor=#DDD6FE;align=center;verticalAlign=middle;fontSize=10;fontColor=#5B21B6;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="720" y="492" width="60" height="20" as="geometry" />
        </mxCell>
        <mxCell id="37" value="只有可变参数能匹配" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#5B21B6;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="500" y="535" width="260" height="22" as="geometry" />
        </mxCell>
        <mxCell id="38" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="100" y="490" width="340" height="90" as="geometry" />
        </mxCell>
        <mxCell id="39" value="💡 匹配原则" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="120" y="505" width="100" height="24" as="geometry" />
        </mxCell>
        <mxCell id="40" value="精确匹配 &gt; 可变参数" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;" vertex="1" parent="1">
          <mxGeometry x="140" y="535" width="280" height="30" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

2.6 参数传递机制

<mxfile>
  <diagram id="java-param-passing" name="Java参数传递机制">
    <mxGraphModel dx="900" dy="750" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="750" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        
        <!-- 主容器 -->
        <mxCell id="2" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="25" y="15" width="850" height="720" as="geometry" />
        </mxCell>
        
        <!-- 标题 -->
        <mxCell id="3" value="Java 参数传递机制" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=26;fontStyle=1;fontColor=#3949ab;" vertex="1" parent="1">
          <mxGeometry x="250" y="30" width="400" height="45" as="geometry" />
        </mxCell>
        
        <!-- 核心结论区域 -->
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff8e1;strokeColor=#ff9800;strokeWidth=3;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="45" y="90" width="810" height="60" as="geometry" />
        </mxCell>
        <mxCell id="5" value="⭐" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=28;" vertex="1" parent="1">
          <mxGeometry x="60" y="95" width="50" height="50" as="geometry" />
        </mxCell>
        <mxCell id="6" value="&lt;font style=&quot;font-size: 20px;&quot; color=&quot;#e65100&quot;&gt;&lt;b&gt;核心结论:Java 只有值传递!&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="110" y="90" width="730" height="60" as="geometry" />
        </mxCell>
        
        <!-- 两种类型说明 -->
        <mxCell id="7" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="45" y="165" width="395" height="50" as="geometry" />
        </mxCell>
        <mxCell id="8" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;基本类型&lt;/b&gt;:传递&lt;b&gt;值的副本&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="45" y="165" width="395" height="50" as="geometry" />
        </mxCell>
        <mxCell id="9" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f3e5f5;strokeColor=#9c27b0;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="460" y="165" width="395" height="50" as="geometry" />
        </mxCell>
        <mxCell id="10" value="&lt;font color=&quot;#7b1fa2&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;引用类型&lt;/b&gt;:传递&lt;b&gt;引用地址的副本&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="460" y="165" width="395" height="50" as="geometry" />
        </mxCell>
        
        <!-- ==================== 基本类型区域 ==================== -->
        <mxCell id="11" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="45" y="235" width="810" height="180" as="geometry" />
        </mxCell>
        
        <!-- 基本类型标题 -->
        <mxCell id="12" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="45" y="235" width="140" height="35" as="geometry" />
        </mxCell>
        <mxCell id="13" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;📦 基本类型&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="45" y="235" width="140" height="35" as="geometry" />
        </mxCell>
        
        <!-- 原始变量 a -->
        <mxCell id="14" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#1976d2;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="295" width="140" height="60" as="geometry" />
        </mxCell>
        <mxCell id="15" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 13px;&quot;&gt;原始变量&lt;/font&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#1565c0&quot; style=&quot;font-size: 18px;&quot;&gt;&lt;b&gt;a = 10&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="80" y="295" width="140" height="60" as="geometry" />
        </mxCell>
        
        <!-- 传递副本箭头 -->
        <mxCell id="16" value="" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#ff9800;flowAnimation=1;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="230" y="325" as="sourcePoint" />
            <mxPoint x="350" y="325" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="17" value="&lt;font color=&quot;#e65100&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;传递副本&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="255" y="295" width="70" height="25" as="geometry" />
        </mxCell>
        
        <!-- 副本变量 copy -->
        <mxCell id="18" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#1976d2;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="360" y="295" width="140" height="60" as="geometry" />
        </mxCell>
        <mxCell id="19" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 13px;&quot;&gt;方法参数&lt;/font&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#1565c0&quot; style=&quot;font-size: 18px;&quot;&gt;&lt;b&gt;copy = 10&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="360" y="295" width="140" height="60" as="geometry" />
        </mxCell>
        
        <!-- 结论说明 -->
        <mxCell id="20" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="530" y="290" width="300" height="70" as="geometry" />
        </mxCell>
        <mxCell id="21" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 14px;&quot;&gt;✅ 方法内修改 &lt;b&gt;copy&lt;/b&gt;&lt;br&gt;&lt;b&gt;不影响&lt;/b&gt;原始变量 &lt;b&gt;a&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="530" y="290" width="300" height="70" as="geometry" />
        </mxCell>
        
        <!-- 隔离线 -->
        <mxCell id="22" value="" style="endArrow=none;dashed=1;html=1;strokeWidth=2;strokeColor=#90caf9;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="80" y="380" as="sourcePoint" />
            <mxPoint x="830" y="380" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="23" value="&lt;font color=&quot;#1976d2&quot;&gt;两个独立的内存空间&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;" vertex="1" parent="1">
          <mxGeometry x="360" y="380" width="130" height="25" as="geometry" />
        </mxCell>
        
        <!-- ==================== 引用类型区域 ==================== -->
        <mxCell id="24" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f3e5f5;strokeColor=#9c27b0;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="45" y="430" width="810" height="285" as="geometry" />
        </mxCell>
        
        <!-- 引用类型标题 -->
        <mxCell id="25" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#9c27b0;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="45" y="430" width="140" height="35" as="geometry" />
        </mxCell>
        <mxCell id="26" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;🔗 引用类型&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="45" y="430" width="140" height="35" as="geometry" />
        </mxCell>
        
        <!-- 原始引用 ref -->
        <mxCell id="27" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#9c27b0;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="490" width="150" height="60" as="geometry" />
        </mxCell>
        <mxCell id="28" value="&lt;font color=&quot;#7b1fa2&quot; style=&quot;font-size: 13px;&quot;&gt;原始引用&lt;/font&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#7b1fa2&quot; style=&quot;font-size: 18px;&quot;&gt;&lt;b&gt;ref = 0x1&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="80" y="490" width="150" height="60" as="geometry" />
        </mxCell>
        
        <!-- 传递地址副本箭头 -->
        <mxCell id="29" value="" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#ff9800;flowAnimation=1;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="240" y="520" as="sourcePoint" />
            <mxPoint x="380" y="520" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="30" value="&lt;font color=&quot;#e65100&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;传递地址副本&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="255" y="490" width="90" height="25" as="geometry" />
        </mxCell>
        
        <!-- 副本引用 copy -->
        <mxCell id="31" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#9c27b0;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="390" y="490" width="150" height="60" as="geometry" />
        </mxCell>
        <mxCell id="32" value="&lt;font color=&quot;#7b1fa2&quot; style=&quot;font-size: 13px;&quot;&gt;方法参数&lt;/font&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#7b1fa2&quot; style=&quot;font-size: 18px;&quot;&gt;&lt;b&gt;copy = 0x1&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="390" y="490" width="150" height="60" as="geometry" />
        </mxCell>
        
        <!-- 从 ref 指向对象的箭头 -->
        <mxCell id="33" value="" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#7b1fa2;entryX=0;entryY=0.5;entryDx=0;entryDy=0;exitX=0.5;exitY=1;exitDx=0;exitDy=0;edgeStyle=orthogonalEdgeStyle;curved=1;" edge="1" parent="1" source="27" target="39">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="155" y="560" as="sourcePoint" />
            <mxPoint x="280" y="640" as="targetPoint" /><Array as="points">
              <mxPoint x="155" y="640" />
            </Array>
          </mxGeometry>
        </mxCell>
        
        <!-- 从 copy 指向对象的箭头 -->
        <mxCell id="34" value="" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#7b1fa2;entryX=1;entryY=0.5;entryDx=0;entryDy=0;exitX=0.5;exitY=1;exitDx=0;exitDy=0;edgeStyle=orthogonalEdgeStyle;curved=1;" edge="1" parent="1" source="31" target="39">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="465" y="560" as="sourcePoint" />
            <mxPoint x="420" y="640" as="targetPoint" />
            <Array as="points">
              <mxPoint x="465" y="640" />
            </Array>
          </mxGeometry>
        </mxCell>
        
        <!-- 指向同一对象的标注 -->
        <mxCell id="35" value="&lt;font color=&quot;#7b1fa2&quot; style=&quot;font-size: 11px;&quot;&gt;指向同一对象&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="260" y="575" width="90" height="20" as="geometry" />
        </mxCell>
        
        <!-- 堆内存对象 -->
        <mxCell id="39" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffecb3;strokeColor=#ffa000;strokeWidth=3;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="220" y="610" width="180" height="70" as="geometry" />
        </mxCell>
        <mxCell id="40" value="&lt;font color=&quot;#e65100&quot; style=&quot;font-size: 12px;&quot;&gt;堆内存&lt;/font&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#e65100&quot; style=&quot;font-size: 16px;&quot;&gt;&lt;b&gt;Object&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="220" y="610" width="180" height="70" as="geometry" />
        </mxCell>
        
        <!-- 结论说明区域 -->
        <mxCell id="41" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="560" y="490" width="270" height="60" as="geometry" />
        </mxCell>
        <mxCell id="42" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 13px;&quot;&gt;✅ 通过 &lt;b&gt;copy&lt;/b&gt; 可以&lt;br&gt;&lt;b&gt;修改对象内容&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="560" y="490" width="270" height="60" as="geometry" />
        </mxCell>
        
        <mxCell id="43" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffebee;strokeColor=#f44336;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="560" y="560" width="270" height="60" as="geometry" />
        </mxCell>
        <mxCell id="44" value="&lt;font color=&quot;#c62828&quot; style=&quot;font-size: 13px;&quot;&gt;❌ 但&lt;b&gt;不能&lt;/b&gt;改变&lt;br&gt;原引用 &lt;b&gt;ref&lt;/b&gt; 的指向&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="560" y="560" width="270" height="60" as="geometry" />
        </mxCell>
        
        <!-- 关键区别说明 -->
        <mxCell id="45" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#eceff1;strokeColor=#607d8b;strokeWidth=2;dashed=1;" vertex="1" parent="1">
          <mxGeometry x="560" y="630" width="270" height="55" as="geometry" />
        </mxCell>
        <mxCell id="46" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 12px;&quot;&gt;💡 &lt;b&gt;copy = new Object()&lt;/b&gt;&lt;br&gt;只改变 copy,不影响 ref&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
          <mxGeometry x="560" y="630" width="270" height="55" as="geometry" />
        </mxCell></root>
    </mxGraphModel>
  </diagram>
</mxfile>
public class ParameterPassDemo {
    
    public static void main(String[] args) {
        
        // ═══════════════════════════════════════════════════════════════
        // 1. 基本类型传递
        // ═══════════════════════════════════════════════════════════════
        int num = 10;
        changeValue(num);
        System.out.println("num = " + num);  // 还是 10,未改变
        
        // ═══════════════════════════════════════════════════════════════
        // 2. 引用类型传递 - 修改对象内容
        // ═══════════════════════════════════════════════════════════════
        int[] arr = {1, 2, 3};
        changeArray(arr);
        System.out.println("arr[0] = " + arr[0]);  // 变成 100
        
        // ═══════════════════════════════════════════════════════════════
        // 3. 引用类型传递 - 试图改变引用指向
        // ═══════════════════════════════════════════════════════════════
        int[] arr2 = {1, 2, 3};
        changeReference(arr2);
        System.out.println("arr2[0] = " + arr2[0]);  // 还是 1,引用未改变
        
        // ═══════════════════════════════════════════════════════════════
        // 4. String 的特殊性
        // ═══════════════════════════════════════════════════════════════
        String str = "Hello";
        changeString(str);
        System.out.println("str = " + str);  // 还是 "Hello"
    }
    
    // 基本类型:修改的是副本
    public static void changeValue(int n) {
        n = 100;  // 只修改了副本
    }
    
    // 引用类型:可以通过引用修改对象内容
    public static void changeArray(int[] array) {
        array[0] = 100;  // 修改了原对象的内容
    }
    
    // 引用类型:不能改变原引用的指向
    public static void changeReference(int[] array) {
        array = new int[]{100, 200, 300};  // 只改变了副本的指向
    }
    
    // String 是不可变的
    public static void changeString(String s) {
        s = "World";  // 创建了新对象,原引用不变
    }
}
<mxfile>
  <diagram id="parameter-passing-diagram" name="参数传递过程图解">
    <mxGraphModel dx="1100" dy="980" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1100" pageHeight="980">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" value="参数传递过程图解" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#1E293B;" vertex="1" parent="1">
          <mxGeometry x="350" y="15" width="400" height="40" as="geometry" />
        </mxCell>
        <mxCell id="3" value="changeArray(arr) 执行过程" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=13;fontColor=#FFFFFF;rounded=1;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="400" y="55" width="300" height="26" as="geometry" />
        </mxCell>
        <mxCell id="4" value="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="100" width="36" height="36" as="geometry" />
        </mxCell>
        <mxCell id="5" value="调用前" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#1E40AF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="105" y="103" width="90" height="30" as="geometry" />
        </mxCell>
        <mxCell id="6" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="145" width="320" height="130" as="geometry" />
        </mxCell>
        <mxCell id="7" value="main 栈帧" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="75" y="155" width="90" height="24" as="geometry" />
        </mxCell>
        <mxCell id="8" value="arr = 0x100" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#22C55E;strokeWidth=2;fontSize=14;fontFamily=Courier New;fontColor=#166534;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="90" y="195" width="130" height="45" as="geometry" />
        </mxCell>
        <mxCell id="9" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="430" y="145" width="200" height="130" as="geometry" />
        </mxCell>
        <mxCell id="10" value="堆内存 Heap" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="445" y="155" width="100" height="24" as="geometry" />
        </mxCell>
        <mxCell id="11" value="[1, 2, 3]" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#F59E0B;strokeWidth=2;fontSize=16;fontFamily=Courier New;fontColor=#92400E;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="460" y="195" width="140" height="40" as="geometry" />
        </mxCell>
        <mxCell id="12" value="地址: 0x100" style="text;html=1;strokeColor=none;fillColor=#FDE68A;align=center;verticalAlign=middle;fontSize=11;fontColor=#92400E;rounded=1;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="475" y="240" width="110" height="22" as="geometry" />
        </mxCell>
        <mxCell id="13" value="" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#22C55E;endFill=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="8" target="11">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="280" y="220" as="sourcePoint" />
            <mxPoint x="420" y="220" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="14" value="2" style="ellipse;whiteSpace=wrap;html=1;fillColor=#8B5CF6;strokeColor=#7C3AED;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="295" width="36" height="36" as="geometry" />
        </mxCell>
        <mxCell id="15" value="调用中" style="text;html=1;strokeColor=none;fillColor=#EDE9FE;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#5B21B6;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="105" y="298" width="90" height="30" as="geometry" />
        </mxCell>
        <mxCell id="16" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#8B5CF6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="340" width="320" height="90" as="geometry" />
        </mxCell>
        <mxCell id="17" value="changeArray 栈帧" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="75" y="350" width="140" height="24" as="geometry" />
        </mxCell>
        <mxCell id="18" value="array = 0x100" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#8B5CF6;strokeWidth=2;fontSize=14;fontFamily=Courier New;fontColor=#5B21B6;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="90" y="385" width="140" height="35" as="geometry" />
        </mxCell>
        <mxCell id="19" value="值的副本" style="text;html=1;strokeColor=none;fillColor=#DDD6FE;align=center;verticalAlign=middle;fontSize=10;fontColor=#5B21B6;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="240" y="390" width="70" height="20" as="geometry" />
        </mxCell>
        <mxCell id="20" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="440" width="320" height="90" as="geometry" />
        </mxCell>
        <mxCell id="21" value="main 栈帧" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="75" y="450" width="90" height="24" as="geometry" />
        </mxCell>
        <mxCell id="22" value="arr = 0x100" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#22C55E;strokeWidth=2;fontSize=14;fontFamily=Courier New;fontColor=#166534;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="90" y="485" width="130" height="35" as="geometry" />
        </mxCell>
        <mxCell id="23" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="430" y="340" width="200" height="190" as="geometry" />
        </mxCell>
        <mxCell id="24" value="堆内存 Heap" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="445" y="350" width="100" height="24" as="geometry" />
        </mxCell>
        <mxCell id="25" value="[1, 2, 3]" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#F59E0B;strokeWidth=2;fontSize=16;fontFamily=Courier New;fontColor=#92400E;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="460" y="420" width="140" height="45" as="geometry" />
        </mxCell>
        <mxCell id="26" value="地址: 0x100" style="text;html=1;strokeColor=none;fillColor=#FDE68A;align=center;verticalAlign=middle;fontSize=11;fontColor=#92400E;rounded=1;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="475" y="470" width="110" height="22" as="geometry" />
        </mxCell>
        <mxCell id="27" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#8B5CF6;endFill=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0;entryDx=0;entryDy=0;curved=1;" edge="1" parent="1" source="18" target="25">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="280" y="400" as="sourcePoint" />
            <mxPoint x="420" y="400" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="28" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#22C55E;endFill=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=1;entryDx=0;entryDy=0;curved=1;" edge="1" parent="1" source="22" target="25">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="280" y="500" as="sourcePoint" />
            <mxPoint x="420" y="450" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="29" value="两个引用指向同一对象" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#92400E;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="455" y="500" width="150" height="22" as="geometry" />
        </mxCell>
        <mxCell id="30" value="3" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="555" width="36" height="36" as="geometry" />
        </mxCell>
        <mxCell id="31" value="执行 array[0] = 100 后" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#DC2626;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="105" y="558" width="200" height="30" as="geometry" />
        </mxCell>
        <mxCell id="32" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#8B5CF6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="605" width="320" height="90" as="geometry" />
        </mxCell>
        <mxCell id="33" value="changeArray 栈帧" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="75" y="615" width="140" height="24" as="geometry" />
        </mxCell>
        <mxCell id="34" value="array = 0x100" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#8B5CF6;strokeWidth=2;fontSize=14;fontFamily=Courier New;fontColor=#5B21B6;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="90" y="650" width="140" height="35" as="geometry" />
        </mxCell>
        <mxCell id="35" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="705" width="320" height="90" as="geometry" />
        </mxCell>
        <mxCell id="36" value="main 栈帧" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="75" y="715" width="90" height="24" as="geometry" />
        </mxCell>
        <mxCell id="37" value="arr = 0x100" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#22C55E;strokeWidth=2;fontSize=14;fontFamily=Courier New;fontColor=#166534;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="90" y="750" width="130" height="35" as="geometry" />
        </mxCell>
        <mxCell id="38" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=3;" vertex="1" parent="1">
          <mxGeometry x="430" y="605" width="200" height="190" as="geometry" />
        </mxCell>
        <mxCell id="39" value="堆内存 Heap" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="445" y="615" width="100" height="24" as="geometry" />
        </mxCell>
        <mxCell id="40" value="[100, 2, 3]" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#EF4444;strokeWidth=3;fontSize=16;fontFamily=Courier New;fontColor=#DC2626;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="460" y="685" width="140" height="45" as="geometry" />
        </mxCell>
        <mxCell id="41" value="地址: 0x100" style="text;html=1;strokeColor=none;fillColor=#FECACA;align=center;verticalAlign=middle;fontSize=11;fontColor=#DC2626;rounded=1;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="475" y="735" width="110" height="22" as="geometry" />
        </mxCell>
        <mxCell id="42" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#8B5CF6;endFill=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0;entryDx=0;entryDy=0;curved=1;" edge="1" parent="1" source="34" target="40">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="280" y="670" as="sourcePoint" />
            <mxPoint x="420" y="670" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="43" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#22C55E;endFill=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=1;entryDx=0;entryDy=0;curved=1;" edge="1" parent="1" source="37" target="40">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="280" y="770" as="sourcePoint" />
            <mxPoint x="420" y="720" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="44" value="⚡ 对象内容被修改!" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="455" y="765" width="150" height="24" as="geometry" />
        </mxCell>
        <mxCell id="45" value="1 → 100" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#DC2626;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="650" width="80" height="24" as="geometry" />
        </mxCell>
        <mxCell id="46" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="680" y="145" width="360" height="130" as="geometry" />
        </mxCell>
        <mxCell id="47" value="💡 关键理解" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="700" y="160" width="100" height="26" as="geometry" />
        </mxCell>
        <mxCell id="48" value="• 传递的是引用值的副本(地址副本)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="700" y="190" width="320" height="24" as="geometry" />
        </mxCell>
        <mxCell id="49" value="• 两个变量指向同一个堆对象" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="700" y="215" width="320" height="24" as="geometry" />
        </mxCell>
        <mxCell id="50" value="• 通过任一引用修改对象,另一个可见" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="700" y="240" width="320" height="24" as="geometry" />
        </mxCell>
        <mxCell id="51" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="680" y="295" width="360" height="235" as="geometry" />
        </mxCell>
        <mxCell id="52" value="📊 内存布局说明" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="700" y="310" width="130" height="26" as="geometry" />
        </mxCell>
        <mxCell id="53" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="700" y="350" width="150" height="60" as="geometry" />
        </mxCell>
        <mxCell id="54" value="栈 Stack" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="715" y="358" width="70" height="20" as="geometry" />
        </mxCell>
        <mxCell id="55" value="存储局部变量和引用" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#166534;" vertex="1" parent="1">
          <mxGeometry x="705" y="382" width="130" height="20" as="geometry" />
        </mxCell>
        <mxCell id="56" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="870" y="350" width="150" height="60" as="geometry" />
        </mxCell>
        <mxCell id="57" value="堆 Heap" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="885" y="358" width="70" height="20" as="geometry" />
        </mxCell>
        <mxCell id="58" value="存储对象实例" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#92400E;" vertex="1" parent="1">
          <mxGeometry x="875" y="382" width="130" height="20" as="geometry" />
        </mxCell>
        <mxCell id="59" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="700" y="425" width="320" height="90" as="geometry" />
        </mxCell>
        <mxCell id="60" value="⚠️ 注意" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="715" y="435" width="60" height="20" as="geometry" />
        </mxCell>
        <mxCell id="61" value="Java 是值传递,但对于引用类型," style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#DC2626;spacingLeft=5;" vertex="1" parent="1">
          <mxGeometry x="710" y="460" width="300" height="20" as="geometry" />
        </mxCell>
        <mxCell id="62" value="传递的值是对象的地址,所以能修改对象内容" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#DC2626;spacingLeft=5;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="710" y="482" width="300" height="20" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

2.7 递归方法

<mxfile>
  <diagram id="recursion-method" name="递归方法">
    <mxGraphModel dx="2340" dy="1001" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="685" width="850" x="25" y="15" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=28;fontStyle=1;fontColor=#3949ab;" value="递归方法" vertex="1">
          <mxGeometry height="45" width="250" x="325" y="30" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="70" width="810" x="45" y="90" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="100" x="45" y="90" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;📖 定义&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="100" x="45" y="90" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 18px;&quot;&gt;&lt;b&gt;方法调用自身&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="70" width="680" x="155" y="90" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="50" x="700" y="100" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#1565c0&quot;&gt;&lt;b&gt;f()&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="50" x="700" y="100" as="geometry" />
        </mxCell>
        <mxCell id="10" edge="1" parent="1" source="8" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#1976d2;curved=1;entryX=1;entryY=0.5;entryDx=0;entryDy=0;exitX=1;exitY=0;exitDx=0;exitDy=0;" target="8" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <Array as="points">
              <mxPoint x="780" y="100" />
              <mxPoint x="790" y="125" />
            </Array>
            <mxPoint x="760" y="110" as="sourcePoint" />
            <mxPoint x="760" y="140" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="250" width="810" x="45" y="180" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="130" x="45" y="180" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;🔑 递归要素&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="130" x="45" y="180" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#66bb6a;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="80" width="250" x="65" y="235" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#2e7d32;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="45" width="45" x="80" y="250" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;1&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="45" x="80" y="250" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;递归出口&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#388e3c&quot; style=&quot;font-size: 12px;&quot;&gt;(终止条件)&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#43a047&quot; style=&quot;font-size: 11px;&quot;&gt;何时停止递归&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="175" x="130" y="235" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#66bb6a;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="80" width="250" x="325" y="235" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#2e7d32;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="45" width="45" x="340" y="250" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;2&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="45" x="340" y="250" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;递归体&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#43a047&quot; style=&quot;font-size: 12px;&quot;&gt;如何调用自身&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="175" x="390" y="235" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#66bb6a;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="80" width="250" x="585" y="235" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#2e7d32;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="45" width="45" x="600" y="250" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;3&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="45" x="600" y="250" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;递归方向&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#43a047&quot; style=&quot;font-size: 12px;&quot;&gt;必须向着出口方向推进&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="175" x="650" y="235" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#81c784;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="85" width="770" x="65" y="330" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#4caf50;strokeWidth=2;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#2e7d32&quot;&gt;&lt;b&gt;f(n)&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="80" x="85" y="350" as="geometry" />
        </mxCell>
        <mxCell id="28" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#4caf50;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="175" y="370" as="sourcePoint" />
            <mxPoint x="235" y="370" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#4caf50;strokeWidth=2;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#2e7d32&quot;&gt;&lt;b&gt;f(n-1)&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="80" x="245" y="350" as="geometry" />
        </mxCell>
        <mxCell id="30" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#4caf50;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="335" y="370" as="sourcePoint" />
            <mxPoint x="395" y="370" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#4caf50;strokeWidth=2;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#2e7d32&quot;&gt;&lt;b&gt;f(n-2)&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="80" x="405" y="350" as="geometry" />
        </mxCell>
        <mxCell id="32" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#4caf50;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="495" y="370" as="sourcePoint" />
            <mxPoint x="555" y="370" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=20;" value="&lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;...&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="50" x="555" y="350" as="geometry" />
        </mxCell>
        <mxCell id="34" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#4caf50;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="615" y="370" as="sourcePoint" />
            <mxPoint x="675" y="370" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f44336;strokeColor=#d32f2f;strokeWidth=2;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#ffffff&quot;&gt;&lt;b&gt;f(0)&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="80" x="685" y="350" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#c62828&quot; style=&quot;font-size: 11px;&quot;&gt;&lt;b&gt;出口!&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="50" x="775" y="355" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ff9800;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="220" width="810" x="45" y="450" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="140" x="45" y="450" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;⚠️ 注意事项&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="140" x="45" y="450" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffebee;strokeColor=#f44336;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="50" width="770" x="65" y="505" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#c62828&quot; style=&quot;font-size: 14px;&quot;&gt;❌ 必须有&lt;b&gt;终止条件&lt;/b&gt;,否则&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#c62828&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;栈溢出(StackOverflowError)&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="770" x="65" y="505" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff8e1;strokeColor=#ffc107;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="50" width="370" x="65" y="565" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#f57f17&quot; style=&quot;font-size: 14px;&quot;&gt;⚡ 递归深度&lt;b&gt;不宜过深&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="370" x="65" y="565" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="50" width="370" x="465" y="565" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 14px;&quot;&gt;✅ 能用&lt;b&gt;循环&lt;/b&gt;解决的尽量用循环&lt;br&gt;(效率更高)&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="370" x="465" y="565" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#e57373;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="35" width="770" x="65" y="625" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#c62828&quot; style=&quot;font-size: 12px;&quot;&gt;f() → f() → f() → f() → f() → ... → 💥 StackOverflowError&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="770" x="65" y="625" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
public class RecursionDemo {
    
    // ═══════════════════════════════════════════════════════════════════
    // 1. 阶乘:n! = n × (n-1)!
    // ═══════════════════════════════════════════════════════════════════
    public static long factorial(int n) {
        // 递归出口
        if (n <= 1) {
            return 1;
        }
        // 递归体
        return n * factorial(n - 1);
    }
    
    // ═══════════════════════════════════════════════════════════════════
    // 2. 斐波那契数列:F(n) = F(n-1) + F(n-2)
    // ═══════════════════════════════════════════════════════════════════
    public static long fibonacci(int n) {
        // 递归出口
        if (n <= 1) {
            return n;
        }
        // 递归体(效率较低,有重复计算)
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
    
    // 优化版:使用记忆化
    public static long fibonacciOptimized(int n, long[] memo) {
        if (n <= 1) {
            return n;
        }
        if (memo[n] != 0) {
            return memo[n];  // 已计算过,直接返回
        }
        memo[n] = fibonacciOptimized(n - 1, memo) + fibonacciOptimized(n - 2, memo);
        return memo[n];
    }
    
    // ═══════════════════════════════════════════════════════════════════
    // 3. 二分查找
    // ═══════════════════════════════════════════════════════════════════
    public static int binarySearch(int[] arr, int target, int left, int right) {
        // 递归出口
        if (left > right) {
            return -1;  // 未找到
        }
        
        int mid = left + (right - left) / 2;
        
        if (arr[mid] == target) {
            return mid;  // 找到了
        } else if (arr[mid] > target) {
            return binarySearch(arr, target, left, mid - 1);
        } else {
            return binarySearch(arr, target, mid + 1, right);
        }
    }
    
    public static void main(String[] args) {
        System.out.println("5! = " + factorial(5));  // 120
        System.out.println("fib(10) = " + fibonacci(10));  // 55
        
        int[] arr = {1, 3, 5, 7, 9, 11, 13};
        System.out.println("index of 7: " + binarySearch(arr, 7, 0, arr.length - 1));  // 3
    }
}
<mxfile>
  <diagram id="recursion-call-stack" name="递归调用栈图解">
    <mxGraphModel dx="1000" dy="800" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1000" pageHeight="800">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" value="递归调用栈图解" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;fontStyle=1;fontColor=#1E293B;" vertex="1" parent="1">
          <mxGeometry x="300" y="20" width="400" height="40" as="geometry" />
        </mxCell>
        <mxCell id="3" value="factorial(4) 执行过程" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;rounded=1;fontStyle=1;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="375" y="60" width="250" height="28" as="geometry" />
        </mxCell>
        <mxCell id="4" value="📥 调用阶段" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="120" y="110" width="140" height="32" as="geometry" />
        </mxCell>
        <mxCell id="5" value="📤 返回阶段" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="540" y="110" width="140" height="32" as="geometry" />
        </mxCell>
        <mxCell id="6" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="160" width="220" height="70" as="geometry" />
        </mxCell>
        <mxCell id="7" value="factorial(1)" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="75" y="170" width="110" height="24" as="geometry" />
        </mxCell>
        <mxCell id="8" value="基准条件: 返回 1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#DC2626;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="200" width="180" height="22" as="geometry" />
        </mxCell>
        <mxCell id="9" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="245" width="220" height="70" as="geometry" />
        </mxCell>
        <mxCell id="10" value="factorial(2)" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="75" y="255" width="110" height="24" as="geometry" />
        </mxCell>
        <mxCell id="11" value="= 2 × factorial(1)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#92400E;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="80" y="283" width="180" height="22" as="geometry" />
        </mxCell>
        <mxCell id="12" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="330" width="220" height="70" as="geometry" />
        </mxCell>
        <mxCell id="13" value="factorial(3)" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="75" y="340" width="110" height="24" as="geometry" />
        </mxCell>
        <mxCell id="14" value="= 3 × factorial(2)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="80" y="368" width="180" height="22" as="geometry" />
        </mxCell>
        <mxCell id="15" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#8B5CF6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="415" width="220" height="70" as="geometry" />
        </mxCell>
        <mxCell id="16" value="factorial(4)" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="75" y="425" width="110" height="24" as="geometry" />
        </mxCell>
        <mxCell id="17" value="= 4 × factorial(3)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#5B21B6;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="80" y="453" width="180" height="22" as="geometry" />
        </mxCell>
        <mxCell id="18" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="500" width="220" height="55" as="geometry" />
        </mxCell>
        <mxCell id="19" value="main()" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="75" y="512" width="80" height="24" as="geometry" />
        </mxCell>
        <mxCell id="20" value="调用入口" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#166534;" vertex="1" parent="1">
          <mxGeometry x="165" y="515" width="100" height="20" as="geometry" />
        </mxCell>
        <mxCell id="21" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#22C55E;endFill=1;entryX=0.5;entryY=1;entryDx=0;entryDy=0;exitX=0.5;exitY=0;exitDx=0;exitDy=0;" edge="1" parent="1" source="18" target="15">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="170" y="510" as="sourcePoint" />
            <mxPoint x="170" y="495" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="22" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#8B5CF6;endFill=1;entryX=0.5;entryY=1;entryDx=0;entryDy=0;exitX=0.5;exitY=0;exitDx=0;exitDy=0;" edge="1" parent="1" source="15" target="12">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="170" y="425" as="sourcePoint" />
            <mxPoint x="170" y="410" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="23" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#3B82F6;endFill=1;entryX=0.5;entryY=1;entryDx=0;entryDy=0;exitX=0.5;exitY=0;exitDx=0;exitDy=0;" edge="1" parent="1" source="12" target="9">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="170" y="340" as="sourcePoint" />
            <mxPoint x="170" y="325" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="24" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#F59E0B;endFill=1;entryX=0.5;entryY=1;entryDx=0;entryDy=0;exitX=0.5;exitY=0;exitDx=0;exitDy=0;" edge="1" parent="1" source="9" target="6">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="170" y="255" as="sourcePoint" />
            <mxPoint x="170" y="240" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="25" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="480" y="160" width="220" height="70" as="geometry" />
        </mxCell>
        <mxCell id="26" value="返回 1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="545" y="180" width="90" height="28" as="geometry" />
        </mxCell>
        <mxCell id="27" value="基准情况触发" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#DC2626;" vertex="1" parent="1">
          <mxGeometry x="520" y="210" width="140" height="18" as="geometry" />
        </mxCell>
        <mxCell id="28" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="480" y="245" width="220" height="70" as="geometry" />
        </mxCell>
        <mxCell id="29" value="2 × 1 = 2" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#92400E;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="520" y="255" width="140" height="24" as="geometry" />
        </mxCell>
        <mxCell id="30" value="返回 2" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="550" y="283" width="80" height="24" as="geometry" />
        </mxCell>
        <mxCell id="31" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="480" y="330" width="220" height="70" as="geometry" />
        </mxCell>
        <mxCell id="32" value="3 × 2 = 6" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="520" y="340" width="140" height="24" as="geometry" />
        </mxCell>
        <mxCell id="33" value="返回 6" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="550" y="368" width="80" height="24" as="geometry" />
        </mxCell>
        <mxCell id="34" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#8B5CF6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="480" y="415" width="220" height="70" as="geometry" />
        </mxCell>
        <mxCell id="35" value="4 × 6 = 24" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#5B21B6;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="520" y="425" width="140" height="24" as="geometry" />
        </mxCell>
        <mxCell id="36" value="返回 24" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="545" y="453" width="90" height="24" as="geometry" />
        </mxCell>
        <mxCell id="37" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="480" y="500" width="220" height="55" as="geometry" />
        </mxCell>
        <mxCell id="38" value="结果: 24" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="545" y="515" width="90" height="26" as="geometry" />
        </mxCell>
        <mxCell id="39" value="" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#EF4444;endFill=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="6" target="25">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="290" y="195" as="sourcePoint" />
            <mxPoint x="470" y="195" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="40" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#EF4444;endFill=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="25" target="28">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="590" y="240" as="sourcePoint" />
            <mxPoint x="590" y="255" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="41" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#F59E0B;endFill=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="28" target="31">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="590" y="325" as="sourcePoint" />
            <mxPoint x="590" y="340" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="42" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#3B82F6;endFill=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="31" target="34">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="590" y="410" as="sourcePoint" />
            <mxPoint x="590" y="425" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="43" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#8B5CF6;endFill=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="34" target="37">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="590" y="495" as="sourcePoint" />
            <mxPoint x="590" y="510" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="44" value="⬆" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#64748B;" vertex="1" parent="1">
          <mxGeometry x="10" y="300" width="40" height="40" as="geometry" />
        </mxCell>
        <mxCell id="45" value="栈增长方向" style="text;html=1;strokeColor=none;fillColor=#F1F5F9;align=center;verticalAlign=middle;fontSize=11;fontColor=#64748B;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="5" y="340" width="50" height="40" as="geometry" />
        </mxCell>
        <mxCell id="46" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#94A3B8;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="740" y="160" width="230" height="200" as="geometry" />
        </mxCell>
        <mxCell id="47" value="💡 递归要点" style="text;html=1;strokeColor=none;fillColor=#475569;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="760" y="175" width="110" height="28" as="geometry" />
        </mxCell>
        <mxCell id="48" value="1. 基准条件" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=left;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;spacingLeft=8;" vertex="1" parent="1">
          <mxGeometry x="760" y="215" width="100" height="22" as="geometry" />
        </mxCell>
        <mxCell id="49" value="n=1 时直接返回" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#64748B;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="760" y="240" width="190" height="18" as="geometry" />
        </mxCell>
        <mxCell id="50" value="2. 递归条件" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=left;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;spacingLeft=8;" vertex="1" parent="1">
          <mxGeometry x="760" y="265" width="100" height="22" as="geometry" />
        </mxCell>
        <mxCell id="51" value="n × factorial(n-1)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#64748B;spacingLeft=10;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="760" y="290" width="190" height="18" as="geometry" />
        </mxCell>
        <mxCell id="52" value="3. 栈帧管理" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=left;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;spacingLeft=8;" vertex="1" parent="1">
          <mxGeometry x="760" y="315" width="100" height="22" as="geometry" />
        </mxCell>
        <mxCell id="53" value="每次调用创建新栈帧" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#64748B;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="760" y="340" width="190" height="18" as="geometry" />
        </mxCell>
        <mxCell id="54" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="740" y="380" width="230" height="175" as="geometry" />
        </mxCell>
        <mxCell id="55" value="📊 执行流程" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="760" y="395" width="110" height="28" as="geometry" />
        </mxCell>
        <mxCell id="56" value="factorial(4)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#92400E;fontFamily=Courier New;spacingLeft=5;" vertex="1" parent="1">
          <mxGeometry x="760" y="430" width="90" height="18" as="geometry" />
        </mxCell>
        <mxCell id="57" value="→ factorial(3)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#92400E;fontFamily=Courier New;spacingLeft=5;" vertex="1" parent="1">
          <mxGeometry x="770" y="450" width="100" height="18" as="geometry" />
        </mxCell>
        <mxCell id="58" value="→ factorial(2)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#92400E;fontFamily=Courier New;spacingLeft=5;" vertex="1" parent="1">
          <mxGeometry x="785" y="470" width="100" height="18" as="geometry" />
        </mxCell>
        <mxCell id="59" value="→ factorial(1)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#92400E;fontFamily=Courier New;spacingLeft=5;" vertex="1" parent="1">
          <mxGeometry x="800" y="490" width="100" height="18" as="geometry" />
        </mxCell>
        <mxCell id="60" value="← 1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#22C55E;fontFamily=Courier New;fontStyle=1;spacingLeft=5;" vertex="1" parent="1">
          <mxGeometry x="900" y="490" width="50" height="18" as="geometry" />
        </mxCell>
        <mxCell id="61" value="← 2" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#22C55E;fontFamily=Courier New;fontStyle=1;spacingLeft=5;" vertex="1" parent="1">
          <mxGeometry x="885" y="470" width="50" height="18" as="geometry" />
        </mxCell>
        <mxCell id="62" value="← 6" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#22C55E;fontFamily=Courier New;fontStyle=1;spacingLeft=5;" vertex="1" parent="1">
          <mxGeometry x="870" y="450" width="50" height="18" as="geometry" />
        </mxCell>
        <mxCell id="63" value="← 24" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#22C55E;fontFamily=Courier New;fontStyle=1;spacingLeft=5;" vertex="1" parent="1">
          <mxGeometry x="850" y="430" width="50" height="18" as="geometry" />
        </mxCell>
        <mxCell id="64" value="最终结果: 24" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="790" y="520" width="130" height="26" as="geometry" />
        </mxCell>
        <mxCell id="65" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="580" width="910" height="70" as="geometry" />
        </mxCell>
        <mxCell id="66" value="⚠️ 注意事项" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="595" width="110" height="24" as="geometry" />
        </mxCell>
        <mxCell id="67" value="• 每次递归调用都会创建新的栈帧,占用栈空间" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="200" y="590" width="340" height="24" as="geometry" />
        </mxCell>
        <mxCell id="68" value="• 递归过深会导致 StackOverflowError" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="200" y="615" width="300" height="24" as="geometry" />
        </mxCell></root></mxGraphModel></diagram></mxfile>

三、构造方法

3.1 构造方法基础

<mxfile>
  <diagram id="constructor-method" name="构造方法">
    <mxGraphModel dx="2340" dy="1001" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="750" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#7c4dff;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="745" width="850" x="25" y="15" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=28;fontStyle=1;fontColor=#7c4dff;" value="构造方法" vertex="1">
          <mxGeometry height="45" width="250" x="325" y="30" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ede7f6;strokeColor=#7c4dff;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="80" width="810" x="45" y="90" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#7c4dff;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="100" x="45" y="90" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;📖 定义&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="100" x="45" y="90" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#512da8&quot; style=&quot;font-size: 16px;&quot;&gt;用于&lt;b&gt;创建对象&lt;/b&gt;并&lt;b&gt;初始化对象状态&lt;/b&gt;的特殊方法&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="680" x="155" y="90" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=32;" value="🔧" vertex="1">
          <mxGeometry height="50" width="50" x="780" y="100" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="310" width="810" x="45" y="190" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="100" x="45" y="190" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;✨ 特点&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="100" x="45" y="190" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#42a5f5;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="55" width="380" x="65" y="245" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="35" x="80" y="255" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;1&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="35" x="80" y="255" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 14px;&quot;&gt;方法名必须与&lt;b&gt;类名相同&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="55" width="315" x="120" y="245" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#42a5f5;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="55" width="370" x="465" y="245" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="35" x="480" y="255" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;2&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="35" x="480" y="255" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;没有返回值类型&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#1976d2&quot; style=&quot;font-size: 12px;&quot;&gt;(连 void 都没有)&lt;/font&gt;" vertex="1">
          <mxGeometry height="55" width="305" x="520" y="245" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#42a5f5;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="55" width="380" x="65" y="310" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="35" x="80" y="320" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;3&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="35" x="80" y="320" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 14px;&quot;&gt;不能用 &lt;b&gt;return&lt;/b&gt; 返回值&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#1976d2&quot; style=&quot;font-size: 11px;&quot;&gt;(可用空 return; 提前结束)&lt;/font&gt;" vertex="1">
          <mxGeometry height="55" width="315" x="120" y="310" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#42a5f5;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="55" width="370" x="465" y="310" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="35" x="480" y="320" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;4&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="35" x="480" y="320" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 14px;&quot;&gt;创建对象时&lt;b&gt;自动调用&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#1976d2&quot; style=&quot;font-size: 11px;&quot;&gt;(不能手动调用)&lt;/font&gt;" vertex="1">
          <mxGeometry height="55" width="305" x="520" y="310" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#42a5f5;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="55" width="380" x="65" y="375" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="35" x="80" y="385" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;5&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="35" x="80" y="385" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 14px;&quot;&gt;可以&lt;b&gt;重载&lt;/b&gt;(多个构造方法)&lt;/font&gt;" vertex="1">
          <mxGeometry height="55" width="315" x="120" y="375" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#5c6bc0;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="110" width="370" x="465" y="375" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#303f9f&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;public class Person {&lt;br&gt;    // 构造方法&lt;br&gt;    public Person() {&lt;br&gt;        // 初始化代码&lt;br&gt;    }&lt;br&gt;}&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="100" width="340" x="480" y="380" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ff9800;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="200" width="810" x="45" y="520" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="160" x="45" y="520" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;📋 默认构造方法&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="160" x="45" y="520" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="125" width="370" x="65" y="575" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;✅ 未定义任何构造方法&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="370" x="65" y="580" as="geometry" />
        </mxCell>
        <mxCell id="39" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#4caf50;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="250" y="620" as="sourcePoint" />
            <mxPoint x="250" y="655" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#81c784;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="330" x="85" y="660" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 13px;&quot;&gt;编译器&lt;b&gt;自动提供&lt;/b&gt;无参构造方法&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="330" x="85" y="660" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffebee;strokeColor=#f44336;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="125" width="370" x="465" y="575" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#c62828&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;❌ 已定义构造方法&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="370" x="465" y="580" as="geometry" />
        </mxCell>
        <mxCell id="44" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#f44336;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="650" y="620" as="sourcePoint" />
            <mxPoint x="650" y="655" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="45" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#ef9a9a;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="330" x="485" y="660" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#c62828&quot; style=&quot;font-size: 13px;&quot;&gt;编译器&lt;b&gt;不再提供&lt;/b&gt;默认构造方法&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="330" x="485" y="660" as="geometry" />
        </mxCell>
        <mxCell id="47" edge="1" parent="1" style="endArrow=none;html=1;strokeWidth=2;strokeColor=#ffcc80;dashed=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="450" y="690" as="sourcePoint" />
            <mxPoint x="450" y="580" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontStyle=1" value="&lt;font color=&quot;#e65100&quot; style=&quot;font-size: 11px;&quot;&gt;VS&lt;/font&gt;" vertex="1">
          <mxGeometry height="20" width="30" x="435" y="620" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
public class Person {

    private String name;
    private int age;

    // ═══════════════════════════════════════════════════════════════════
    // 无参构造方法
    // ═══════════════════════════════════════════════════════════════════
    public Person() {
        this.name = "Unknown";
        this.age = 0;
        System.out.println("无参构造方法被调用");
    }

    // ═══════════════════════════════════════════════════════════════════
    // 有参构造方法
    // ═══════════════════════════════════════════════════════════════════
    public Person(String name) {
        this.name = name;
        this.age = 0;
        System.out.println("一个参数的构造方法被调用");
    }

    // ═══════════════════════════════════════════════════════════════════
    // 全参构造方法
    // ═══════════════════════════════════════════════════════════════════
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("两个参数的构造方法被调用");
    }

    // ═══════════════════════════════════════════════════════════════════
    // 构造方法调用另一个构造方法(this())
    // ═══════════════════════════════════════════════════════════════════
    public Person(String name, int age, String ignored) {
        this(name, age);  // 调用两个参数的构造方法,必须在第一行
        // 其他初始化代码...
    }

    public static void main(String[] args) {
        Person p1 = new Person();                    // 无参构造
        Person p2 = new Person("Tom");               // 一个参数
        Person p3 = new Person("Jerry", 20);         // 两个参数
    }
}

3.2 构造方法与继承

 <mxfile>
  <diagram id="constructor-inheritance-fixed" name="构造方法与继承">
    <mxGraphModel dx="1281" dy="581" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1100" pageHeight="880" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;fontStyle=1;fontColor=#1E293B;" value="构造方法与继承" vertex="1">
          <mxGeometry height="45" width="400" x="350" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📋 核心规则" vertex="1">
          <mxGeometry height="36" width="140" x="60" y="80" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="280" width="480" x="60" y="130" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#93C5FD;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="55" width="440" x="80" y="150" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="1" vertex="1">
          <mxGeometry height="32" width="32" x="95" y="160" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;spacingLeft=10;" value="构造方法不能被继承" vertex="1">
          <mxGeometry height="35" width="370" x="135" y="160" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#93C5FD;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="55" width="440" x="80" y="215" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="2" vertex="1">
          <mxGeometry height="32" width="32" x="95" y="227" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;spacingLeft=10;" value="子类构造方法第一行必须调用父类构造方法" vertex="1">
          <mxGeometry height="35" width="370" x="135" y="225" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#93C5FD;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="55" width="440" x="80" y="280" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="3" vertex="1">
          <mxGeometry height="32" width="32" x="95" y="292" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;spacingLeft=10;" value="没有显式调用时,编译器自动添加 super()" vertex="1">
          <mxGeometry height="35" width="370" x="135" y="290" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="55" width="440" x="80" y="345" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="4" vertex="1">
          <mxGeometry height="32" width="32" x="95" y="357" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#DC2626;spacingLeft=10;" value="this() 和 super() 不能同时出现(都要在第一行)" vertex="1">
          <mxGeometry height="35" width="380" x="135" y="355" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="⚡ 执行顺序" vertex="1">
          <mxGeometry height="36" width="140" x="560" y="80" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F5F3FF;strokeColor=#8B5CF6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="280" width="480" x="560" y="130" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="115" width="200" x="580" y="150" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="父类 Parent" vertex="1">
          <mxGeometry height="24" width="100" x="600" y="160" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="1" vertex="1">
          <mxGeometry height="28" width="28" x="595" y="195" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#92400E;spacingLeft=5;" value="静态代码块" vertex="1">
          <mxGeometry height="22" width="140" x="628" y="198" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="3" vertex="1">
          <mxGeometry height="28" width="28" x="595" y="228" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#92400E;spacingLeft=5;" value="实例代码块 → 构造方法" vertex="1">
          <mxGeometry height="22" width="150" x="628" y="231" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="115" width="200" x="820" y="150" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="子类 Child" vertex="1">
          <mxGeometry height="24" width="100" x="840" y="160" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="2" vertex="1">
          <mxGeometry height="28" width="28" x="835" y="195" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=5;" value="静态代码块" vertex="1">
          <mxGeometry height="22" width="140" x="868" y="198" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="4" vertex="1">
          <mxGeometry height="28" width="28" x="835" y="228" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=5;" value="实例代码块 → 构造方法" vertex="1">
          <mxGeometry height="22" width="150" x="868" y="231" as="geometry" />
        </mxCell>
        <mxCell id="31" edge="1" parent="1" source="19" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#8B5CF6;endFill=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" target="25" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="790" y="210" as="sourcePoint" />
            <mxPoint x="810" y="210" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#EDE9FE;align=center;verticalAlign=middle;fontSize=11;fontColor=#7C3AED;rounded=1;fontStyle=1;" value="extends" vertex="1">
          <mxGeometry height="20" width="60" x="770" y="178" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="115" width="440" x="580" y="280" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📊 完整执行顺序" vertex="1">
          <mxGeometry height="24" width="130" x="600" y="290" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="1" vertex="1">
          <mxGeometry height="26" width="26" x="600" y="325" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=10;fontColor=#92400E;rounded=1;" value="父类静态块" vertex="1">
          <mxGeometry height="20" width="70" x="590" y="355" as="geometry" />
        </mxCell>
        <mxCell id="37" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#22C55E;endFill=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="635" y="338" as="sourcePoint" />
            <mxPoint x="665" y="338" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="38" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="2" vertex="1">
          <mxGeometry height="26" width="26" x="670" y="325" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=10;fontColor=#1E40AF;rounded=1;" value="子类静态块" vertex="1">
          <mxGeometry height="20" width="70" x="658" y="355" as="geometry" />
        </mxCell>
        <mxCell id="40" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#22C55E;endFill=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="705" y="338" as="sourcePoint" />
            <mxPoint x="735" y="338" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="41" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="3" vertex="1">
          <mxGeometry height="26" width="26" x="740" y="325" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=10;fontColor=#92400E;rounded=1;" value="父类实例块" vertex="1">
          <mxGeometry height="20" width="70" x="728" y="355" as="geometry" />
        </mxCell>
        <mxCell id="43" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#22C55E;endFill=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="775" y="338" as="sourcePoint" />
            <mxPoint x="805" y="338" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="44" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="4" vertex="1">
          <mxGeometry height="26" width="26" x="810" y="325" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=10;fontColor=#92400E;rounded=1;" value="父类构造" vertex="1">
          <mxGeometry height="20" width="60" x="798" y="355" as="geometry" />
        </mxCell>
        <mxCell id="46" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#22C55E;endFill=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="845" y="338" as="sourcePoint" />
            <mxPoint x="875" y="338" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="47" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="5" vertex="1">
          <mxGeometry height="26" width="26" x="880" y="325" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=10;fontColor=#1E40AF;rounded=1;" value="子类实例块" vertex="1">
          <mxGeometry height="20" width="70" x="868" y="355" as="geometry" />
        </mxCell>
        <mxCell id="49" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#22C55E;endFill=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="915" y="338" as="sourcePoint" />
            <mxPoint x="945" y="338" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="50" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="6" vertex="1">
          <mxGeometry height="26" width="26" x="950" y="325" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=10;fontColor=#1E40AF;rounded=1;" value="子类构造" vertex="1">
          <mxGeometry height="20" width="60" x="938" y="355" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💻 代码示例" vertex="1">
          <mxGeometry height="36" width="140" x="60" y="430" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="340" width="480" x="60" y="480" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#F59E0B;fontFamily=Courier New;spacingLeft=10;" value="class Parent {" vertex="1">
          <mxGeometry height="22" width="200" x="75" y="495" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#94A3B8;fontFamily=Courier New;spacingLeft=10;" value="    static { System.out.println(&quot;1.父类静态块&quot;); }" vertex="1">
          <mxGeometry height="20" width="440" x="75" y="517" as="geometry" />
        </mxCell>
        <mxCell id="56" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#94A3B8;fontFamily=Courier New;spacingLeft=10;" value="    { System.out.println(&quot;3.父类实例块&quot;); }" vertex="1">
          <mxGeometry height="20" width="440" x="75" y="537" as="geometry" />
        </mxCell>
        <mxCell id="57" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#94A3B8;fontFamily=Courier New;spacingLeft=10;" value="    Parent() { System.out.println(&quot;4.父类构造方法&quot;); }" vertex="1">
          <mxGeometry height="20" width="440" x="75" y="557" as="geometry" />
        </mxCell>
        <mxCell id="58" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#F59E0B;fontFamily=Courier New;spacingLeft=10;" value="}" vertex="1">
          <mxGeometry height="22" width="50" x="75" y="577" as="geometry" />
        </mxCell>
        <mxCell id="59" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#3B82F6;fontFamily=Courier New;spacingLeft=10;" value="class Child extends Parent {" vertex="1">
          <mxGeometry height="22" width="300" x="75" y="610" as="geometry" />
        </mxCell>
        <mxCell id="60" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#94A3B8;fontFamily=Courier New;spacingLeft=10;" value="    static { System.out.println(&quot;2.子类静态块&quot;); }" vertex="1">
          <mxGeometry height="20" width="440" x="75" y="632" as="geometry" />
        </mxCell>
        <mxCell id="61" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#94A3B8;fontFamily=Courier New;spacingLeft=10;" value="    { System.out.println(&quot;5.子类实例块&quot;); }" vertex="1">
          <mxGeometry height="20" width="440" x="75" y="652" as="geometry" />
        </mxCell>
        <mxCell id="62" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#94A3B8;fontFamily=Courier New;spacingLeft=10;" value="    Child() {" vertex="1">
          <mxGeometry height="20" width="200" x="75" y="672" as="geometry" />
        </mxCell>
        <mxCell id="63" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#22C55E;fontFamily=Courier New;spacingLeft=10;" value="        super();  // 隐式调用,可省略" vertex="1">
          <mxGeometry height="20" width="350" x="75" y="692" as="geometry" />
        </mxCell>
        <mxCell id="64" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#94A3B8;fontFamily=Courier New;spacingLeft=10;" value="        System.out.println(&quot;6.子类构造方法&quot;);" vertex="1">
          <mxGeometry height="20" width="400" x="75" y="712" as="geometry" />
        </mxCell>
        <mxCell id="65" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#94A3B8;fontFamily=Courier New;spacingLeft=10;" value="    }" vertex="1">
          <mxGeometry height="20" width="50" x="75" y="732" as="geometry" />
        </mxCell>
        <mxCell id="66" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#3B82F6;fontFamily=Courier New;spacingLeft=10;" value="}" vertex="1">
          <mxGeometry height="22" width="50" x="75" y="752" as="geometry" />
        </mxCell>
        <mxCell id="67" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#6B7280;fontFamily=Courier New;spacingLeft=10;fontStyle=2;" value="// new Child() 输出顺序: 1→2→3→4→5→6" vertex="1">
          <mxGeometry height="22" width="400" x="75" y="785" as="geometry" />
        </mxCell>
        <mxCell id="68" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔍 super() vs this()" vertex="1">
          <mxGeometry height="36" width="180" x="560" y="430" as="geometry" />
        </mxCell>
        <mxCell id="69" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#EF4444;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="340" width="480" x="560" y="480" as="geometry" />
        </mxCell>
        <mxCell id="70" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FECACA;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="130" width="200" x="580" y="500" as="geometry" />
        </mxCell>
        <mxCell id="71" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;fontFamily=Courier New;" value="super()" vertex="1">
          <mxGeometry height="28" width="100" x="620" y="515" as="geometry" />
        </mxCell>
        <mxCell id="72" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#92400E;spacingLeft=5;" value="• 调用父类构造方法" vertex="1">
          <mxGeometry height="22" width="170" x="595" y="550" as="geometry" />
        </mxCell>
        <mxCell id="73" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#92400E;spacingLeft=5;" value="• 必须在第一行" vertex="1">
          <mxGeometry height="22" width="170" x="595" y="575" as="geometry" />
        </mxCell>
        <mxCell id="74" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FECACA;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="130" width="200" x="820" y="500" as="geometry" />
        </mxCell>
        <mxCell id="75" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;fontFamily=Courier New;" value="this()" vertex="1">
          <mxGeometry height="28" width="100" x="860" y="515" as="geometry" />
        </mxCell>
        <mxCell id="76" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=5;" value="• 调用本类其他构造方法" vertex="1">
          <mxGeometry height="22" width="170" x="835" y="550" as="geometry" />
        </mxCell>
        <mxCell id="77" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=5;" value="• 必须在第一行" vertex="1">
          <mxGeometry height="22" width="170" x="835" y="575" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
class Parent {
    private String name;

    static {
        System.out.println("1. 父类静态代码块");
    }

    {
        System.out.println("3. 父类实例代码块");
    }

    public Parent() {
        System.out.println("4. 父类无参构造方法");
    }

    public Parent(String name) {
        this.name = name;
        System.out.println("4. 父类有参构造方法");
    }
}

class Child extends Parent {
    private int age;

    static {
        System.out.println("2. 子类静态代码块");
    }

    {
        System.out.println("5. 子类实例代码块");
    }

    public Child() {
        // 隐式调用 super();
        System.out.println("6. 子类无参构造方法");
    }

    public Child(String name, int age) {
        super(name);  // 显式调用父类有参构造方法
        this.age = age;
        System.out.println("6. 子类有参构造方法");
    }
}

// 运行 new Child() 输出:
// 1. 父类静态代码块
// 2. 子类静态代码块
// 3. 父类实例代码块
// 4. 父类无参构造方法
// 5. 子类实例代码块
// 6. 子类无参构造方法

四、this 与 super 关键字

<mxfile>
  <diagram id="this-vs-super" name="this vs super">
    <mxGraphModel dx="1614" dy="690" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="705" width="850" x="25" y="15" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=28;fontStyle=1;fontColor=#5c6bc0;" value="this vs super" vertex="1">
          <mxGeometry height="45" width="300" x="300" y="30" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#5c6bc0;strokeColor=#3949ab;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="120" x="45" y="90" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 16px;&quot;&gt;&lt;b&gt;对比项&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="120" x="45" y="90" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#2196f3;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="340" x="165" y="90" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Courier New;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 18px;&quot;&gt;&lt;b&gt;this&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="340" x="165" y="90" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=#f57c00;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="350" x="505" y="90" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Courier New;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 18px;&quot;&gt;&lt;b&gt;super&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="350" x="505" y="90" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="120" x="45" y="140" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#3949ab&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;含义&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="120" x="45" y="140" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="340" x="165" y="140" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;当前对象&lt;/b&gt;的引用&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="340" x="165" y="140" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="350" x="505" y="140" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#e65100&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;父类对象&lt;/b&gt;的引用&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="350" x="505" y="140" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="80" width="120" x="45" y="200" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#3949ab&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;访问变量&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="120" x="45" y="200" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="80" width="340" x="165" y="200" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#1565c0&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;this.变量名&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#1976d2&quot; style=&quot;font-size: 12px;&quot;&gt;访问当前类的成员变量&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="340" x="165" y="200" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="80" width="350" x="505" y="200" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#e65100&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;super.变量名&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#f57c00&quot; style=&quot;font-size: 12px;&quot;&gt;访问父类的成员变量&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="350" x="505" y="200" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="80" width="120" x="45" y="280" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#3949ab&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;访问方法&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="120" x="45" y="280" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="80" width="340" x="165" y="280" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#1565c0&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;this.方法名()&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#1976d2&quot; style=&quot;font-size: 12px;&quot;&gt;调用当前类的方法&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="340" x="165" y="280" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="80" width="350" x="505" y="280" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#e65100&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;super.方法名()&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#f57c00&quot; style=&quot;font-size: 12px;&quot;&gt;调用父类的方法&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="350" x="505" y="280" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="80" width="120" x="45" y="360" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#3949ab&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;访问构造器&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="120" x="45" y="360" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="80" width="340" x="165" y="360" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#1565c0&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;this(参数)&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#1976d2&quot; style=&quot;font-size: 12px;&quot;&gt;调用本类其他构造方法&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="340" x="165" y="360" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="80" width="350" x="505" y="360" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#e65100&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;super(参数)&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#f57c00&quot; style=&quot;font-size: 12px;&quot;&gt;调用父类构造方法&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="350" x="505" y="360" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="120" x="45" y="440" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#3949ab&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;位置要求&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="120" x="45" y="440" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="340" x="165" y="440" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 13px;&quot;&gt;必须在构造方法&lt;b&gt;第一行&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="340" x="165" y="440" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="350" x="505" y="440" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#e65100&quot; style=&quot;font-size: 13px;&quot;&gt;必须在构造方法&lt;b&gt;第一行&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="350" x="505" y="440" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="120" x="45" y="500" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#3949ab&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;静态方法&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="120" x="45" y="500" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffebee;strokeColor=#ef9a9a;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="340" x="165" y="500" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#c62828&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;不能使用&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="340" x="165" y="500" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffebee;strokeColor=#ef9a9a;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="350" x="505" y="500" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#c62828&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;不能使用&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="350" x="505" y="500" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fce4ec;strokeColor=#f48fb1;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="90" width="810" x="45" y="580" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#c2185b&quot; style=&quot;font-size: 14px;&quot;&gt;⚠️ &lt;b&gt;注意&lt;/b&gt;:this() 和 super() &lt;b&gt;不能同时出现&lt;/b&gt;在同一个构造方法中&lt;br&gt;(因为它们都必须在第一行)&lt;/font&gt;" vertex="1">
          <mxGeometry height="90" width="605" x="295" y="592.5" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#64b5f6;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="35" width="150" x="65" y="620" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#1565c0&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;this&lt;/b&gt;&lt;/font&gt;&lt;font color=&quot;#1976d2&quot; style=&quot;font-size: 12px;&quot;&gt; → 本类&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="150" x="65" y="620" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffb74d;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="35" width="150" x="235" y="620" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#e65100&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;super&lt;/b&gt;&lt;/font&gt;&lt;font color=&quot;#f57c00&quot; style=&quot;font-size: 12px;&quot;&gt; → 父类&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="150" x="235" y="620" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
class Animal {
    protected String name = "Animal";

    public void eat() {
        System.out.println("Animal eating");
    }
}

class Dog extends Animal {
    private String name = "Dog";  // 同名成员变量

    public void eat() {
        System.out.println("Dog eating");
    }

    public void demo() {
        // ═══════════════════════════════════════════════════════════════
        // this 的使用
        // ═══════════════════════════════════════════════════════════════
        System.out.println(this.name);   // "Dog" - 当前类的 name
        this.eat();                      // "Dog eating" - 当前类的方法

        // ═══════════════════════════════════════════════════════════════
        // super 的使用
        // ═══════════════════════════════════════════════════════════════
        System.out.println(super.name);  // "Animal" - 父类的 name
        super.eat();                     // "Animal eating" - 父类的方法
    }

    public void showThis() {
        // this 可以作为参数传递,表示当前对象
        printObject(this);
    }

    public void printObject(Dog d) {
        System.out.println(d.name);
    }
}

五、方法调用的内存过程

<mxfile>
  <diagram id="stack-frame-structure" name="栈帧结构">
    <mxGraphModel dx="883" dy="401" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#1E293B;" value="栈帧结构 (Stack Frame)" vertex="1">
          <mxGeometry height="40" width="400" x="250" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#E0E7FF;align=center;verticalAlign=middle;fontSize=13;fontColor=#4338CA;rounded=1;" value="JVM 虚拟机栈中每个方法调用对应一个栈帧" vertex="1">
          <mxGeometry height="26" width="350" x="275" y="60" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#6366F1;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="520" width="700" x="100" y="110" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="栈帧 (Stack Frame)" vertex="1">
          <mxGeometry height="35" width="240" x="330" y="125" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="115" width="620" x="140" y="180" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📋 局部变量表 (Local Variable Table)" vertex="1">
          <mxGeometry height="30" width="320" x="160" y="195" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#93C5FD;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="180" x="160" y="235" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=#BFDBFE;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="方法参数" vertex="1">
          <mxGeometry height="24" width="70" x="175" y="245" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#64748B;fontFamily=Courier New;" value="int a, String b..." vertex="1">
          <mxGeometry height="20" width="85" x="250" y="248" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#93C5FD;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="180" x="355" y="235" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#BFDBFE;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="局部变量" vertex="1">
          <mxGeometry height="24" width="70" x="370" y="245" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#64748B;" value="方法内声明的变量" vertex="1">
          <mxGeometry height="20" width="90" x="440" y="248" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#93C5FD;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="50" width="190" x="550" y="235" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=#BFDBFE;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="this 引用" vertex="1">
          <mxGeometry height="24" width="70" x="565" y="245" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#64748B;" value="实例方法专有" vertex="1">
          <mxGeometry height="20" width="90" x="640" y="248" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="95" width="620" x="140" y="310" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📊 操作数栈 (Operand Stack)" vertex="1">
          <mxGeometry height="30" width="260" x="160" y="325" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FCD34D;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="280" x="160" y="365" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#92400E;" value="执行运算时的临时存储" vertex="1">
          <mxGeometry height="24" width="250" x="175" y="368" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FCD34D;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="280" x="460" y="365" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#92400E;" value="方法返回值的传递" vertex="1">
          <mxGeometry height="24" width="250" x="475" y="368" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#E0E7FF;strokeColor=#8B5CF6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="620" x="140" y="420" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔗 动态链接 (Dynamic Linking)" vertex="1">
          <mxGeometry height="30" width="270" x="160" y="435" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#C4B5FD;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="24" width="580" x="160" y="470" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#5B21B6;" value="指向运行时常量池的方法引用,支持多态调用" vertex="1">
          <mxGeometry height="24" width="550" x="175" y="470" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="620" x="140" y="515" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📍 方法返回地址 (Return Address)" vertex="1">
          <mxGeometry height="30" width="290" x="160" y="530" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#86EFAC;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="24" width="580" x="160" y="565" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#166534;" value="方法结束后,返回到调用处继续执行的位置(PC 计数器值)" vertex="1">
          <mxGeometry height="24" width="550" x="175" y="565" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;fontStyle=1;fontColor=#6366F1;" value="⬆" vertex="1">
          <mxGeometry height="40" width="40" x="820" y="300" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#EEF2FF;align=center;verticalAlign=middle;fontSize=12;fontColor=#6366F1;rounded=1;fontStyle=1;" value="栈顶" vertex="1">
          <mxGeometry height="24" width="50" x="815" y="340" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;fontStyle=1;fontColor=#6366F1;" value="⬇" vertex="1">
          <mxGeometry height="40" width="40" x="820" y="400" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#EEF2FF;align=center;verticalAlign=middle;fontSize=12;fontColor=#6366F1;rounded=1;fontStyle=1;" value="栈底" vertex="1">
          <mxGeometry height="24" width="50" x="815" y="440" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="1" vertex="1">
          <mxGeometry height="28" width="28" x="120" y="180" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="2" vertex="1">
          <mxGeometry height="28" width="28" x="120" y="310" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#8B5CF6;strokeColor=#7C3AED;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="3" vertex="1">
          <mxGeometry height="28" width="28" x="120" y="420" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="4" vertex="1">
          <mxGeometry height="28" width="28" x="120" y="515" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#94A3B8;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="700" x="95" y="640" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=#475569;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💡 要点" vertex="1">
          <mxGeometry height="24" width="70" x="115" y="652" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=#E2E8F0;align=center;verticalAlign=middle;fontSize=11;fontColor=#475569;rounded=1;" value="每个方法调用创建一个栈帧" vertex="1">
          <mxGeometry height="22" width="160" x="195" y="652" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#E2E8F0;align=center;verticalAlign=middle;fontSize=11;fontColor=#475569;rounded=1;" value="方法结束时栈帧出栈销毁" vertex="1">
          <mxGeometry height="22" width="155" x="365" y="652" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=#E2E8F0;align=center;verticalAlign=middle;fontSize=11;fontColor=#475569;rounded=1;" value="栈帧大小编译期可确定" vertex="1">
          <mxGeometry height="22" width="145" x="530" y="652" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=11;fontColor=#DC2626;rounded=1;fontStyle=1;" value="递归过深会导致 StackOverflowError" vertex="1">
          <mxGeometry height="22" width="230" x="195" y="680" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=11;fontColor=#166534;rounded=1;fontStyle=1;" value="线程私有,不存在并发问题" vertex="1">
          <mxGeometry height="22" width="170" x="435" y="680" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
 <mxfile>
  <diagram id="method-call-stack-fixed" name="方法调用栈帧图解-优化版">
    <mxGraphModel dx="1281" dy="581" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1100" pageHeight="850" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="835" width="1060" x="20" y="15" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=26;fontStyle=1;fontColor=#5c6bc0;" value="Java 方法调用栈帧 (Stack Frame) 图解" vertex="1">
          <mxGeometry height="40" width="440" x="330" y="30" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#263238;strokeColor=#37474f;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="170" width="1020" x="40" y="85" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;" value="&lt;font color=&quot;#80cbc4&quot;&gt;📝 示例代码 (StackTest.java)&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="220" x="55" y="92" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#37474f;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="120" width="480" x="55" y="120" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=top;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#c792ea&quot;&gt;public static void&lt;/font&gt; &lt;font color=&quot;#82aaff&quot;&gt;main&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;(&lt;/font&gt;&lt;font color=&quot;#ffcb6b&quot;&gt;String&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;[]&lt;/font&gt; &lt;font color=&quot;#f78c6c&quot;&gt;args&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;)&lt;/font&gt; &lt;font color=&quot;#89ddff&quot;&gt;{&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=&quot;#c792ea&quot;&gt;int&lt;/font&gt; &lt;font color=&quot;#f78c6c&quot;&gt;result&lt;/font&gt; &lt;font color=&quot;#89ddff&quot;&gt;=&lt;/font&gt; &lt;font color=&quot;#82aaff&quot;&gt;add&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;(&lt;/font&gt;&lt;font color=&quot;#f78c6c&quot;&gt;3&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;,&lt;/font&gt; &lt;font color=&quot;#f78c6c&quot;&gt;5&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;);&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=&quot;#ffcb6b&quot;&gt;System&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;.&lt;/font&gt;&lt;font color=&quot;#f78c6c&quot;&gt;out&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;.&lt;/font&gt;&lt;font color=&quot;#82aaff&quot;&gt;println&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;(&lt;/font&gt;&lt;font color=&quot;#f78c6c&quot;&gt;result&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;);&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#89ddff&quot;&gt;}&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="100" width="460" x="65" y="130" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#37474f;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="120" width="490" x="555" y="120" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=top;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#c792ea&quot;&gt;public static int&lt;/font&gt; &lt;font color=&quot;#82aaff&quot;&gt;add&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;(&lt;/font&gt;&lt;font color=&quot;#c792ea&quot;&gt;int&lt;/font&gt; &lt;font color=&quot;#f78c6c&quot;&gt;a&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;,&lt;/font&gt; &lt;font color=&quot;#c792ea&quot;&gt;int&lt;/font&gt; &lt;font color=&quot;#f78c6c&quot;&gt;b&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;)&lt;/font&gt; &lt;font color=&quot;#89ddff&quot;&gt;{&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;font color=&quot;#c792ea&quot;&gt;int&lt;/font&gt; &lt;font color=&quot;#f78c6c&quot;&gt;sum&lt;/font&gt; &lt;font color=&quot;#89ddff&quot;&gt;=&lt;/font&gt; &lt;font color=&quot;#f78c6c&quot;&gt;a&lt;/font&gt; &lt;font color=&quot;#89ddff&quot;&gt;+&lt;/font&gt; &lt;font color=&quot;#f78c6c&quot;&gt;b&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;;&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=&quot;#c792ea&quot;&gt;return&lt;/font&gt; &lt;font color=&quot;#f78c6c&quot;&gt;sum&lt;/font&gt;&lt;font color=&quot;#89ddff&quot;&gt;;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#89ddff&quot;&gt;}&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="100" width="470" x="565" y="130" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;fontColor=#5c6bc0;" value="⚡ 执行过程 (Stack LIFO)" vertex="1">
          <mxGeometry height="30" width="250" x="40" y="270" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="240" width="240" x="40" y="310" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="240" x="40" y="310" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;Step 1: main() 入栈&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="240" x="40" y="310" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#64b5f6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="170" width="200" x="60" y="360" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;strokeDasharray=3 3;" value="" vertex="1">
          <mxGeometry height="80" width="180" x="70" y="370" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#90a4ae&quot; style=&quot;font-size: 12px;&quot;&gt;(空闲空间)&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="70" y="395" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=#388e3c;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="65" width="180" x="70" y="455" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;main() 栈帧&lt;/b&gt;&lt;br&gt;args = ...&lt;br&gt;result = ?&lt;/font&gt;" vertex="1">
          <mxGeometry height="65" width="180" x="70" y="455" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 11px;&quot;&gt;栈底&lt;/font&gt;" vertex="1">
          <mxGeometry height="20" width="40" x="60" y="525" as="geometry" />
        </mxCell>
        <mxCell id="49" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#5c6bc0;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="280" y="430" as="sourcePoint" />
            <mxPoint x="300" y="430" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ff9800;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="240" width="240" x="300" y="310" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="240" x="300" y="310" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;Step 2: add() 入栈&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="240" x="300" y="310" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe0b2;strokeColor=#ffb74d;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="170" width="200" x="320" y="360" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#2196f3;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="180" x="330" y="370" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;add() 栈帧&lt;/b&gt;&lt;br&gt;a = 3&lt;br&gt;b = 5&lt;br&gt;sum = 8&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="180" x="330" y="370" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=#388e3c;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="65" width="180" x="330" y="455" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;main() 栈帧&lt;/b&gt;&lt;br&gt;args = ...&lt;br&gt;result = ?&lt;/font&gt;" vertex="1">
          <mxGeometry height="65" width="180" x="330" y="455" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#e65100&quot; style=&quot;font-size: 11px;&quot;&gt;&lt;b&gt;PUSH(压栈)&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="20" width="70" x="525" y="390" as="geometry" />
        </mxCell>
        <mxCell id="50" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#5c6bc0;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="540" y="430" as="sourcePoint" />
            <mxPoint x="560" y="430" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fce4ec;strokeColor=#e91e63;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="240" width="240" x="560" y="310" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e91e63;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="240" x="560" y="310" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;Step 3: add() 返回出栈&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="240" x="560" y="310" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f8bbd9;strokeColor=#f48fb1;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="170" width="200" x="580" y="360" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#ef9a9a;strokeWidth=1;strokeDasharray=3 3;" value="" vertex="1">
          <mxGeometry height="80" width="180" x="590" y="370" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#c62828&quot; style=&quot;font-size: 11px;&quot;&gt;&lt;b&gt;POP(弹栈) - 销毁&lt;/b&gt;&lt;br&gt;返回值 8 传给调用者&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="180" x="590" y="385" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=#388e3c;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="65" width="180" x="590" y="455" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;main() 栈帧&lt;/b&gt;&lt;br&gt;args = ...&lt;br&gt;result = 8 ←&lt;/font&gt;" vertex="1">
          <mxGeometry height="65" width="180" x="590" y="455" as="geometry" />
        </mxCell>
        <mxCell id="38" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#e91e63;flowAnimation=1;curved=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="680" y="430" as="sourcePoint" />
            <mxPoint x="680" y="455" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#c2185b&quot; style=&quot;font-size: 11px;&quot;&gt;&lt;b&gt;返回 8&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="20" width="50" x="695" y="430" as="geometry" />
        </mxCell>
        <mxCell id="51" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#5c6bc0;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="800" y="430" as="sourcePoint" />
            <mxPoint x="820" y="430" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="240" width="240" x="820" y="310" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="240" x="820" y="310" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;Step 4: main() 继续执行&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="240" x="820" y="310" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#a5d6a7;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="170" width="200" x="840" y="360" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;strokeDasharray=3 3;" value="" vertex="1">
          <mxGeometry height="80" width="180" x="850" y="370" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#90a4ae&quot; style=&quot;font-size: 12px;&quot;&gt;(空闲空间)&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="850" y="395" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=#388e3c;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="65" width="180" x="850" y="455" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;main() 栈帧&lt;/b&gt;&lt;br&gt;args = ...&lt;br&gt;result = 8&lt;/font&gt;" vertex="1">
          <mxGeometry height="65" width="180" x="850" y="455" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontStyle=1" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 11px;&quot;&gt;✓ 输出: 8&lt;/font&gt;" vertex="1">
          <mxGeometry height="20" width="80" x="890" y="525" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f3e5f5;strokeColor=#9c27b0;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="220" width="1020" x="40" y="580" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="text;html=1;strokeColor=none;fillColor=#9c27b0;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;shadow=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 16px;&quot;&gt;&lt;b&gt;💡 栈帧 (Stack Frame) 核心机制&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="1020" x="40" y="580" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#e1bee7;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="150" width="300" x="60" y="630" as="geometry" />
        </mxCell>
        <mxCell id="54_1" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;fontColor=#8e24aa;" value="1. LIFO (后进先出)" vertex="1">
          <mxGeometry height="30" width="300" x="60" y="640" as="geometry" />
        </mxCell>
        <mxCell id="54_2" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=top;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#4a148c;spacingLeft=15;spacingRight=15;" value="栈(Stack)就像一个弹夹。&lt;br&gt;最后被调用的方法(如 add)最先入栈,&lt;br&gt;也最先执行完毕出栈。&lt;br&gt;main 方法最先进入,最后离开。" vertex="1">
          <mxGeometry height="100" width="300" x="60" y="670" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#e1bee7;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="150" width="300" x="400" y="630" as="geometry" />
        </mxCell>
        <mxCell id="55_1" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;fontColor=#8e24aa;" value="2. 栈帧内容与隔离性" vertex="1">
          <mxGeometry height="30" width="300" x="400" y="640" as="geometry" />
        </mxCell>
        <mxCell id="55_2" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=top;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#4a148c;spacingLeft=15;spacingRight=15;" value="每个方法调用都会创建一个独立的栈帧。&lt;br&gt;栈帧包含:&lt;br&gt;• 局部变量表 (Local Variables)&lt;br&gt;• 操作数栈 (Operand Stack)&lt;br&gt;• 动态链接 &amp; 方法返回地址" vertex="1">
          <mxGeometry height="100" width="300" x="400" y="670" as="geometry" />
        </mxCell>
        <mxCell id="56" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#e1bee7;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="150" width="300" x="740" y="630" as="geometry" />
        </mxCell>
        <mxCell id="56_1" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;fontColor=#8e24aa;" value="3. 线程私有 &amp; 自动管理" vertex="1">
          <mxGeometry height="30" width="300" x="740" y="640" as="geometry" />
        </mxCell>
        <mxCell id="56_2" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=top;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#4a148c;spacingLeft=15;spacingRight=15;" value="栈内存是线程私有的,&lt;br&gt;不存在线程安全问题(局部变量)。&lt;br&gt;栈内存不需要垃圾回收(GC),&lt;br&gt;方法结束自动弹出释放。" vertex="1">
          <mxGeometry height="100" width="300" x="740" y="670" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

六、常见面试题

 <mxfile>
  <diagram id="interview-questions-fixed" name="高频面试题-完善版">
    <mxGraphModel dx="2340" dy="1001" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1200" pageHeight="1000" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="965" width="1160" x="20" y="15" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=28;fontStyle=1;fontColor=#5c6bc0;" value="🎯 Java 基础高频面试题图解" vertex="1">
          <mxGeometry height="45" width="440" x="380" y="30" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="130" width="545" x="40" y="90" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=#1565c0;strokeWidth=2;fontColor=#ffffff;fontStyle=1;fontSize=16;" value="Q1" vertex="1">
          <mxGeometry height="45" width="45" x="55" y="105" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;重载和重写的区别?&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="460" x="110" y="105" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="515" x="55" y="145" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1976d2&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;b&gt;重载&lt;/b&gt;:同一类中方法名相同、参数不同,&lt;b&gt;编译期绑定&lt;/b&gt;&lt;br&gt;&lt;b&gt;重写&lt;/b&gt;:子类重新实现父类方法,&lt;b&gt;运行期绑定&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="495" x="65" y="150" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ff9800;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="130" width="555" x="605" y="90" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=#f57c00;strokeWidth=2;fontColor=#ffffff;fontStyle=1;fontSize=16;" value="Q2" vertex="1">
          <mxGeometry height="45" width="45" x="620" y="105" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#e65100&quot; style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;静态方法能否访问非静态成员?为什么?&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="470" x="675" y="105" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe0b2;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="525" x="620" y="145" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#e65100&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;b&gt;不能&lt;/b&gt;。静态方法属于类,在类加载时就存在;&lt;br&gt;非静态成员属于对象,需创建对象后才存在。调用静态方法时可能还没有对象。&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="505" x="630" y="150" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="140" width="545" x="40" y="235" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=#388e3c;strokeWidth=2;fontColor=#ffffff;fontStyle=1;fontSize=16;" value="Q3" vertex="1">
          <mxGeometry height="45" width="45" x="55" y="250" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;Java 是值传递还是引用传递?&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="460" x="110" y="250" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="65" width="515" x="55" y="295" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#2e7d32&quot; style=&quot;font-size: 13px;&quot;&gt;Java &lt;b&gt;只有值传递&lt;/b&gt;。&lt;br&gt;• 基本类型:传值的副本&lt;br&gt;• 引用类型:传引用地址的副本(可修改对象内容,但不能改变原引用指向)&lt;/font&gt;" vertex="1">
          <mxGeometry height="58" width="495" x="65" y="298" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fce4ec;strokeColor=#e91e63;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="140" width="555" x="605" y="235" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#e91e63;strokeColor=#c2185b;strokeWidth=2;fontColor=#ffffff;fontStyle=1;fontSize=16;" value="Q4" vertex="1">
          <mxGeometry height="45" width="45" x="620" y="250" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#c2185b&quot; style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;成员变量和局部变量的区别?&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="470" x="675" y="250" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f8bbd9;strokeColor=#f48fb1;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="65" width="525" x="620" y="295" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#c2185b&quot; style=&quot;font-size: 12px;&quot;&gt;① &lt;b&gt;定义位置&lt;/b&gt;:成员变量在类中,局部变量在方法中&lt;br&gt;② &lt;b&gt;存储位置&lt;/b&gt;:成员变量在堆中,局部变量在栈中&lt;br&gt;③ &lt;b&gt;生命周期&lt;/b&gt;:成员变量随对象,局部变量随方法&lt;br&gt;④ &lt;b&gt;初始化&lt;/b&gt;:成员变量有默认值,局部变量必须初始化&lt;/font&gt;" vertex="1">
          <mxGeometry height="58" width="505" x="630" y="298" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f3e5f5;strokeColor=#9c27b0;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="130" width="545" x="40" y="390" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#9c27b0;strokeColor=#7b1fa2;strokeWidth=2;fontColor=#ffffff;fontStyle=1;fontSize=16;" value="Q5" vertex="1">
          <mxGeometry height="45" width="45" x="55" y="405" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#7b1fa2&quot; style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;构造方法能否被重写?能否被重载?&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="460" x="110" y="405" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e1bee7;strokeColor=#ce93d8;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="55" width="515" x="55" y="450" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#7b1fa2&quot; style=&quot;font-size: 13px;&quot;&gt;构造方法&lt;b&gt;不能被重写&lt;/b&gt;(不能继承)&lt;br&gt;但&lt;b&gt;可以被重载&lt;/b&gt;(同一类中多个构造器)&lt;/font&gt;" vertex="1">
          <mxGeometry height="48" width="495" x="65" y="453" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e0f2f1;strokeColor=#009688;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="130" width="555" x="605" y="390" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#009688;strokeColor=#00796b;strokeWidth=2;fontColor=#ffffff;fontStyle=1;fontSize=16;" value="Q6" vertex="1">
          <mxGeometry height="45" width="45" x="620" y="405" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#00695c&quot; style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;this 和 super 能否同时出现在构造方法中?&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="470" x="675" y="405" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#b2dfdb;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="55" width="525" x="620" y="450" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#00695c&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;b&gt;不能&lt;/b&gt;。this() 和 super() 都必须是构造方法的&lt;b&gt;第一条语句&lt;/b&gt;,&lt;br&gt;所以二者不能同时出现。&lt;/font&gt;" vertex="1">
          <mxGeometry height="48" width="505" x="630" y="453" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#eceff1;strokeColor=#607d8b;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="400" width="1120" x="40" y="540" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;fontColor=#455a64;" value="📊 核心概念对比速查表" vertex="1">
          <mxGeometry height="30" width="360" x="420" y="555" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=#1565c0;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="35" width="520" x="60" y="600" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;重载 (Overload) vs 重写 (Override)&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="520" x="60" y="600" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="35" width="100" x="60" y="635" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;对比项&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="100" x="60" y="635" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="35" width="210" x="160" y="635" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;重载 Overload&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="210" x="160" y="635" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="35" width="210" x="370" y="635" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1565c0&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;重写 Override&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="210" x="370" y="635" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="100" x="60" y="670" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;位置&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="100" x="60" y="670" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="160" y="670" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1976d2&quot; style=&quot;font-size: 11px;&quot;&gt;同一个类中&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="160" y="670" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="370" y="670" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1976d2&quot; style=&quot;font-size: 11px;&quot;&gt;子类与父类之间&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="370" y="670" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="100" x="60" y="700" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;方法名&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="100" x="60" y="700" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="160" y="700" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1976d2&quot; style=&quot;font-size: 11px;&quot;&gt;必须相同&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="160" y="700" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="370" y="700" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#1976d2&quot; style=&quot;font-size: 11px;&quot;&gt;必须相同&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="370" y="700" as="geometry" />
        </mxCell>
        <mxCell id="56" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="100" x="60" y="730" as="geometry" />
        </mxCell>
        <mxCell id="57" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;参数列表&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="100" x="60" y="730" as="geometry" />
        </mxCell>
        <mxCell id="58" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="160" y="730" as="geometry" />
        </mxCell>
        <mxCell id="59" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#d32f2f&quot; style=&quot;font-size: 11px;&quot;&gt;&lt;b&gt;必须不同&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="160" y="730" as="geometry" />
        </mxCell>
        <mxCell id="60" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="370" y="730" as="geometry" />
        </mxCell>
        <mxCell id="61" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#d32f2f&quot; style=&quot;font-size: 11px;&quot;&gt;&lt;b&gt;必须相同&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="370" y="730" as="geometry" />
        </mxCell>
        <mxCell id="62" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="100" x="60" y="760" as="geometry" />
        </mxCell>
        <mxCell id="63" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;返回类型&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="100" x="60" y="760" as="geometry" />
        </mxCell>
        <mxCell id="64" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="160" y="760" as="geometry" />
        </mxCell>
        <mxCell id="65" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;无关&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="160" y="760" as="geometry" />
        </mxCell>
        <mxCell id="66" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="370" y="760" as="geometry" />
        </mxCell>
        <mxCell id="67" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;需相同或为子类&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="370" y="760" as="geometry" />
        </mxCell>
        <mxCell id="68" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="100" x="60" y="790" as="geometry" />
        </mxCell>
        <mxCell id="69" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;异常&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="100" x="60" y="790" as="geometry" />
        </mxCell>
        <mxCell id="70" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="160" y="790" as="geometry" />
        </mxCell>
        <mxCell id="71" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;无关&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="160" y="790" as="geometry" />
        </mxCell>
        <mxCell id="72" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="370" y="790" as="geometry" />
        </mxCell>
        <mxCell id="73" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;范围不能比父类大&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="370" y="790" as="geometry" />
        </mxCell>
        <mxCell id="74" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="100" x="60" y="820" as="geometry" />
        </mxCell>
        <mxCell id="75" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;访问权限&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="100" x="60" y="820" as="geometry" />
        </mxCell>
        <mxCell id="76" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="160" y="820" as="geometry" />
        </mxCell>
        <mxCell id="77" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;无关&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="160" y="820" as="geometry" />
        </mxCell>
        <mxCell id="78" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="370" y="820" as="geometry" />
        </mxCell>
        <mxCell id="79" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;不能比父类严&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="370" y="820" as="geometry" />
        </mxCell>
        <mxCell id="80" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#009688;strokeColor=#00796b;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="35" width="520" x="620" y="600" as="geometry" />
        </mxCell>
        <mxCell id="81" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;抽象类 (Abstract Class) vs 接口 (Interface)&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="520" x="620" y="600" as="geometry" />
        </mxCell>
        <mxCell id="82" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e0f2f1;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="35" width="100" x="620" y="635" as="geometry" />
        </mxCell>
        <mxCell id="83" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#00695c&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;对比项&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="100" x="620" y="635" as="geometry" />
        </mxCell>
        <mxCell id="84" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e0f2f1;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="35" width="210" x="720" y="635" as="geometry" />
        </mxCell>
        <mxCell id="85" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#00695c&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;抽象类&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="210" x="720" y="635" as="geometry" />
        </mxCell>
        <mxCell id="86" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e0f2f1;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="35" width="210" x="930" y="635" as="geometry" />
        </mxCell>
        <mxCell id="87" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#00695c&quot; style=&quot;font-size: 12px;&quot;&gt;&lt;b&gt;接口&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="210" x="930" y="635" as="geometry" />
        </mxCell>
        <mxCell id="88" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="100" x="620" y="670" as="geometry" />
        </mxCell>
        <mxCell id="89" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;成员变量&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="100" x="620" y="670" as="geometry" />
        </mxCell>
        <mxCell id="90" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="720" y="670" as="geometry" />
        </mxCell>
        <mxCell id="91" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#004d40&quot; style=&quot;font-size: 11px;&quot;&gt;各种类型&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="720" y="670" as="geometry" />
        </mxCell>
        <mxCell id="92" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="930" y="670" as="geometry" />
        </mxCell>
        <mxCell id="93" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#004d40&quot; style=&quot;font-size: 11px;&quot;&gt;public static final (常量)&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="930" y="670" as="geometry" />
        </mxCell>
        <mxCell id="94" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="100" x="620" y="700" as="geometry" />
        </mxCell>
        <mxCell id="95" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;继承性&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="100" x="620" y="700" as="geometry" />
        </mxCell>
        <mxCell id="96" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="720" y="700" as="geometry" />
        </mxCell>
        <mxCell id="97" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#004d40&quot; style=&quot;font-size: 11px;&quot;&gt;单继承&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="720" y="700" as="geometry" />
        </mxCell>
        <mxCell id="98" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="930" y="700" as="geometry" />
        </mxCell>
        <mxCell id="99" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#004d40&quot; style=&quot;font-size: 11px;&quot;&gt;多实现&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="930" y="700" as="geometry" />
        </mxCell>
        <mxCell id="100" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="100" x="620" y="730" as="geometry" />
        </mxCell>
        <mxCell id="101" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;构造方法&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="100" x="620" y="730" as="geometry" />
        </mxCell>
        <mxCell id="102" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="720" y="730" as="geometry" />
        </mxCell>
        <mxCell id="103" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#004d40&quot; style=&quot;font-size: 11px;&quot;&gt;有 (供子类调用)&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="720" y="730" as="geometry" />
        </mxCell>
        <mxCell id="104" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="210" x="930" y="730" as="geometry" />
        </mxCell>
        <mxCell id="105" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#004d40&quot; style=&quot;font-size: 11px;&quot;&gt;无&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="210" x="930" y="730" as="geometry" />
        </mxCell>
        <mxCell id="106" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="90" width="100" x="620" y="760" as="geometry" />
        </mxCell>
        <mxCell id="107" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#455a64&quot; style=&quot;font-size: 11px;&quot;&gt;设计目的&lt;/font&gt;" vertex="1">
          <mxGeometry height="90" width="100" x="620" y="760" as="geometry" />
        </mxCell>
        <mxCell id="108" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="90" width="210" x="720" y="760" as="geometry" />
        </mxCell>
        <mxCell id="109" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#004d40&quot; style=&quot;font-size: 11px;&quot;&gt;&lt;b&gt;代码复用 (IS-A)&lt;/b&gt;&lt;br&gt;对一类事物的抽象&lt;br&gt;如:狗是动物&lt;/font&gt;" vertex="1">
          <mxGeometry height="90" width="210" x="720" y="760" as="geometry" />
        </mxCell>
        <mxCell id="110" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#80cbc4;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="90" width="210" x="930" y="760" as="geometry" />
        </mxCell>
        <mxCell id="111" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#004d40&quot; style=&quot;font-size: 11px;&quot;&gt;&lt;b&gt;行为规范 (LIKE-A)&lt;/b&gt;&lt;br&gt;定义一套行为标准&lt;br&gt;如:飞机能飞,鸟能飞&lt;/font&gt;" vertex="1">
          <mxGeometry height="90" width="210" x="930" y="760" as="geometry" />
        </mxCell>
        <mxCell id="112" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#607d8b;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="30" width="1120" x="40" y="880" as="geometry" />
        </mxCell>
        <mxCell id="113" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" value="&lt;font color=&quot;#ffffff&quot; style=&quot;font-size: 12px;&quot;&gt;💡 面试小贴士:回答区别类问题时,建议先说定义,再列举维度(如位置、修饰符、异常等),最后举例说明场景。&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="1120" x="40" y="880" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
// 经典面试题:下面代码输出什么?
public class Test {
    public static void main(String[] args) {
        String str = "hello";
        change(str);
        System.out.println(str);  // 输出:hello

        StringBuilder sb = new StringBuilder("hello");
        change(sb);
        System.out.println(sb);   // 输出:helloworld
    }

    public static void change(String s) {
        s = "world";  // 新对象,不影响原引用
    }

    public static void change(StringBuilder sb) {
        sb.append("world");  // 修改原对象内容
    }
}