变量与方法
一、变量详解
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="<font color="#ffffff" style="font-size: 18px;"><b>变量</b></font>" 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="<font style="font-size: 17px;" color="#1565c0"><b>局部变量</b></font>" 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="<font color="#ffffff"><b>📍 定义位置</b></font>" 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="方法内部<br>代码块内部<br>构造器内部" 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="<font color="#ffffff"><b>🏷️ 修饰符</b></font>" 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="<font color="#c62828"><b>无修饰符</b></font><br><font color="#757575" style="font-size: 11px;">(不能用访问修饰符)</font>" 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="<font color="#ffffff"><b>⏱️ 生命周期</b></font>" 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="方法调用时创建<br>方法结束时销毁" 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="<font color="#ffffff"><b>💾 存储位置</b></font>" 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="<font color="#1565c0"><b>栈内存</b></font>" 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="<font style="font-size: 17px;" color="#e65100"><b>成员变量</b></font><br><font color="#757575" style="font-size: 12px;">(实例变量)</font>" 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="<font color="#ffffff"><b>📍 定义位置</b></font>" 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="类中<br>方法外" 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="<font color="#ffffff"><b>🏷️ 修饰符</b></font>" 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="<font color="#c62828"><b>无 static</b></font><br><font color="#757575" style="font-size: 11px;">(可用访问修饰符)</font>" 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="<font color="#ffffff"><b>⏱️ 生命周期</b></font>" 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="对象创建时创建<br>对象销毁时销毁" 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="<font color="#ffffff"><b>💾 存储位置</b></font>" 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="<font color="#e65100"><b>堆内存</b></font>" 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="<font style="font-size: 17px;" color="#2e7d32"><b>类变量</b></font><br><font color="#757575" style="font-size: 12px;">(静态变量)</font>" 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="<font color="#ffffff"><b>📍 定义位置</b></font>" 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="类中<br>方法外" 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="<font color="#ffffff"><b>🏷️ 修饰符</b></font>" 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="<font color="#2e7d32"><b>有 static</b></font><br><font color="#757575" style="font-size: 11px;">(可用访问修饰符)</font>" 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="<font color="#ffffff"><b>⏱️ 生命周期</b></font>" 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="类加载时创建<br>类卸载时销毁" 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="<font color="#ffffff"><b>💾 存储位置</b></font>" 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="<font color="#2e7d32"><b>方法区/元空间</b></font>" 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="<font color="#ffffff"><b>对比项</b></font>" 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="<font color="#ffffff"><b>局部变量</b></font>" 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="<font color="#ffffff"><b>成员变量</b></font>" 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="<font color="#ffffff"><b>静态变量</b></font>" 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="<b>📍 定义位置</b>" 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="<b>🏷️ 修饰符</b>" 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="<font color="#c62828">不能用访问修饰符</font><br>可用 <font face="Courier New" color="#1565c0"><b>final</b></font>" 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="可用<br><font face="Courier New" color="#e65100"><b>private/public</b></font> 等" 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="必须有 <font face="Courier New" color="#2e7d32"><b>static</b></font><br>可有其他修饰符" 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="<b>💾 存储位置</b>" 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="<font color="#1565c0"><b>栈</b></font>" 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="<font color="#e65100"><b>堆</b></font>(对象内)" 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="<font color="#2e7d32"><b>方法区/元空间</b></font>" 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="<b>⏱️ 生命周期</b>" 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="<b>📋 默认值</b>" 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="<font color="#c62828"><b>无</b></font>,必须初始化" 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="<font color="#4caf50"><b>有</b></font>默认值" 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="<font color="#4caf50"><b>有</b></font>默认值" 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="<b>🎯 作用范围</b>" 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="<b>🔑 访问方式</b>" 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="<font face="Courier New" color="#e65100"><b>this.</b></font>变量名<br>或直接使用" 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="<font face="Courier New" color="#2e7d32"><b>类名.</b></font>变量名<br>或直接使用" 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="<b>🔒 线程安全</b>" 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 = "Tom"" 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="<font style="font-size: 16px;">❓ <b>问题:</b>为什么局部变量必须初始化,而成员变量可以不初始化?</font>" 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="<font color="#ffffff" style="font-size: 16px;"><b>📋 原因 1:编译器的控制能力</b></font>" 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="<font color="#1565c0" style="font-size: 14px;"><b>局部变量</b></font>" 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="• 生命周期短,作用域小<br>• 编译器可以<b>轻松追踪</b>是否被初始化<br>• 如果使用前未初始化,<font color="#c62828"><b>编译器直接报错</b></font>" 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="<font color="#e65100" style="font-size: 14px;"><b>成员变量</b></font>" 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="• 生命周期长,可能在<b>多个方法</b>中被使用<br>• 初始化可能发生在构造器、初始化块、或任意方法中<br>• 编译器<font color="#c62828"><b>难以判断</b></font>使用时是否已初始化" 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="<font color="#ffffff" style="font-size: 16px;"><b>🔒 原因 2:安全性考虑</b></font>" 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="<font color="#c62828">⚠️ <b>如果成员变量不给默认值:</b></font><br>会读取到内存中的<font color="#c62828"><b>"垃圾值"</b></font>,导致程序行为不可预测" 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="<font color="#2e7d32">✅ <b>给默认值可以保证:</b></font>" 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="<font color="#1565c0"><b>数值类型</b></font><br><font style="font-size: 20px;" color="#1976d2"><b>0</b></font><br><font color="#757575" style="font-size: 11px;">安全的起始值</font>" 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="<font color="#e65100"><b>引用类型</b></font><br><font style="font-size: 20px;" color="#ff9800"><b>null</b></font><br><font color="#757575" style="font-size: 11px;">明确未指向对象</font>" 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="<font color="#7b1fa2"><b>布尔类型</b></font><br><font style="font-size: 20px;" color="#9c27b0"><b>false</b></font><br><font color="#757575" style="font-size: 11px;">安全的初始状态</font>" 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="<font color="#ffffff" style="font-size: 16px;"><b>📊 默认值一览表</b></font>" 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="<font color="#ffffff"><b>类型</b></font>" 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="<font color="#ffffff"><b>默认值</b></font>" 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="<font face="Courier New" color="#2e7d32"><b>byte / short / int</b></font>" 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="<font face="Courier New" style="font-size: 16px;" color="#1565c0"><b>0</b></font>" 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="<font face="Courier New" color="#2e7d32"><b>long</b></font>" 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="<font face="Courier New" style="font-size: 16px;" color="#1565c0"><b>0L</b></font>" 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="<font face="Courier New" color="#2e7d32"><b>float</b></font>" 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="<font face="Courier New" style="font-size: 16px;" color="#1565c0"><b>0.0f</b></font>" 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="<font face="Courier New" color="#2e7d32"><b>double</b></font>" 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="<font face="Courier New" style="font-size: 16px;" color="#1565c0"><b>0.0d</b></font>" 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="<font face="Courier New" color="#2e7d32"><b>char</b></font>" 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="<font face="Courier New" style="font-size: 16px;" color="#1565c0"><b>'\u0000'</b></font>" 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="<font face="Courier New" color="#2e7d32"><b>boolean</b></font>" 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="<font face="Courier New" style="font-size: 16px;" color="#9c27b0"><b>false</b></font>" 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="<font face="Courier New" color="#e65100"><b>引用类型 (String, Object...)</b></font>" 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="<font face="Courier New" style="font-size: 16px;" color="#e65100"><b>null</b></font>" 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="<font color="#ffffff" style="font-size: 17px;"><b>📋 按参数和返回值分类</b></font>" 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="<font color="#ffffff"><b>类型</b></font>" 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="<font color="#ffffff"><b>示例</b></font>" 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="<font color="#1565c0"><b>无参无返回值</b></font>" 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="<font face="Courier New" color="#424242"><font color="#7b1fa2">void</font> <font color="#1565c0"><b>printHello</b></font>() { ... }</font>" 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="<font color="#e65100"><b>有参无返回值</b></font>" 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="<font face="Courier New" color="#424242"><font color="#7b1fa2">void</font> <font color="#1565c0"><b>printMessage</b></font>(<font color="#2e7d32">String msg</font>) { ... }</font>" 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="<font color="#2e7d32"><b>无参有返回值</b></font>" 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="<font face="Courier New" color="#424242"><font color="#7b1fa2">int</font> <font color="#1565c0"><b>getCount</b></font>() { ... }</font>" 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="<font color="#c62828"><b>有参有返回值</b></font>" 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="<font face="Courier New" color="#424242"><font color="#7b1fa2">int</font> <font color="#1565c0"><b>add</b></font>(<font color="#2e7d32">int a, int b</font>) { ... }</font>" 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="<font color="#ffffff" style="font-size: 17px;"><b>⚡ 按是否静态分类</b></font>" 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="<font color="#ffffff"><b>类型</b></font>" 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="<font color="#ffffff"><b>特点</b></font>" 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="<font color="#e65100"><b>静态方法</b></font><br><font face="Courier New" color="#7b1fa2" style="font-size: 12px;">static</font>" 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="属于<b>类</b>,通过<font color="#1565c0"><b>类名</b></font>调用 &nbsp; <font face="Courier New" color="#757575">ClassName.method()</font>" 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="<font color="#2e7d32"><b>实例方法</b></font>" 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="属于<b>对象</b>,通过<font color="#2e7d32"><b>实例</b></font>调用 &nbsp; <font face="Courier New" color="#757575">object.method()</font>" 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="<font color="#c62828"><b>构造方法</b></font><br><font face="Courier New" color="#757575" style="font-size: 12px;">Constructor</font>" 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="创建对象时调用,<font color="#c62828"><b>无返回值</b></font> &nbsp; <font face="Courier New" color="#757575">new ClassName()</font>" 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="<font face="Courier New">类名.方法名()</font><br><font color="#9ca3af" style="font-size: 11px;">或 对象.方法名()</font>" 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="<font face="Courier New">对象.方法名()</font>" 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="❌ 不能访问实例变量<br>✅ 可以访问静态变量" 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="✅ 可以访问实例变量<br>✅ 可以访问静态变量" 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="❌ 不能调用实例方法<br>✅ 可以调用静态方法" 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="✅ 可以调用实例方法<br>✅ 可以调用静态方法" 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="<font color="#ffffff" style="font-size: 16px;"><b>⏱️ 时间线:成员的生命周期</b></font>" 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="<font color="#ffffff"><b>① 类加载</b></font>" 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="<font color="#1565c0"><b>静态成员就绪</b></font><br><font color="#424242">• 静态变量赋值</font><br><font color="#424242">• 静态方法可用</font>" 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="<font color="#ffffff"><b>② 创建对象</b></font>" 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="<font color="#e65100"><b>实例成员就绪</b></font><br><font color="#424242">• 实例变量赋值</font><br><font color="#424242">• </font><font face="Courier New" color="#7b1fa2">this</font><font color="#424242"> 可用</font>" 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="<font color="#ffffff"><b>③ 调用实例方法</b></font>" 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="<font color="#7b1fa2"><b>所有成员可用</b></font><br><font color="#424242">• 静态成员 ✅</font><br><font color="#424242">• 实例成员 ✅</font>" 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="<font color="#2e7d32" style="font-size: 12px;">时间</font>" 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="<font color="#ffffff" style="font-size: 16px;"><b>❓ 问题:为什么静态方法不能访问非静态成员?</b></font>" 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="<font style="font-size: 15px;" color="#c62828">⚠️ <b>当调用静态方法时,可能还没有创建任何对象!</b></font>" 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="<font face="Courier New" color="#80cbc4" style="font-size: 14px;">ClassName.</font><font face="Courier New" color="#ffcc80" style="font-size: 14px;">staticMethod</font><font face="Courier New" color="#ffffff" style="font-size: 14px;">();</font><font color="#78909c" style="font-size: 13px;"> // 不需要对象就能调用</font>" 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="<font color="#c62828" style="font-size: 15px;"><b>此时的状态:</b></font>" 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="<font color="#2e7d32" style="font-size: 22px;">✅</font>" 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="<font color="#2e7d32"><b>静态变量已存在</b></font><br><font color="#424242" style="font-size: 12px;">类加载时就已初始化</font>" 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="<font color="#c62828" style="font-size: 22px;">❌</font>" 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="<font color="#c62828"><b>实例变量不存在</b></font><br><font color="#424242" style="font-size: 12px;">没有对象,哪来的实例变量?</font>" 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="<font color="#c62828" style="font-size: 22px;">❌</font>" 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="<font face="Courier New" color="#c62828"><b>this</b></font><font color="#c62828"><b> 不存在</b></font><br><font color="#424242" style="font-size: 12px;">没有对象,this 指向谁?</font>" 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)<br>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)<br>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)<br>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="<font color="#3949ab" style="font-size: 14px;">重载方法的选择发生在<b>编译期</b>,称为"<b>静态绑定</b>"或"<b>早期绑定</b>"</font>" 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="<font face="Courier New" color="#80cbc4" style="font-size: 15px;">demo.</font><font face="Courier New" color="#ffcc80" style="font-size: 15px;">add</font><font face="Courier New" color="#ffffff" style="font-size: 15px;">(</font><font face="Courier New" color="#ce93d8" style="font-size: 15px;">1</font><font face="Courier New" color="#ffffff" style="font-size: 15px;">, </font><font face="Courier New" color="#ce93d8" style="font-size: 15px;">2</font><font face="Courier New" color="#ffffff" style="font-size: 15px;">);</font>" 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="<font color="#ffffff"><b>⚙️ 编译期</b></font>" 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="<font color="#1565c0" style="font-size: 14px;"><b>① 编译器分析参数</b></font><br><font color="#424242">两个 </font><font face="Courier New" color="#7b1fa2"><b>int</b></font><font color="#424242"> 类型</font>" 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="<font color="#2e7d32" style="font-size: 14px;"><b>② 匹配方法签名</b></font><br><font face="Courier New" color="#424242">add(</font><font face="Courier New" color="#7b1fa2"><b>int</b></font><font face="Courier New" color="#424242">, </font><font face="Courier New" color="#7b1fa2"><b>int</b></font><font face="Courier New" color="#424242">)</font>" 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="<font color="#c2185b" style="font-size: 14px;"><b>③ 生成字节码</b></font><br><font face="Courier New" color="#424242">invokevirtual #2 </font><font face="Courier New" color="#78909c">// add:(II)I</font>" 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="<font color="#ffffff"><b>▶️ 运行时</b></font>" 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="<font color="#7b1fa2" style="font-size: 15px;"><b>直接调用 </b></font><font face="Courier New" color="#7b1fa2" style="font-size: 15px;"><b>add(int, int)</b></font><font color="#7b1fa2" style="font-size: 15px;"><b>,不需要再选择</b></font>" 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="✅ 访问权限<br>相同或更大" 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="// 父类<br>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="// 子类重写<br>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="<font color="#ffffff"><b>📝 语法</b></font>" 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="<font face="Courier New" color="#ce93d8" style="font-size: 16px;">类型</font><font face="Courier New" color="#ffcc80" style="font-size: 16px;"><b>...</b></font><font face="Courier New" color="#ffffff" style="font-size: 16px;"> 参数名</font>" 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="<font color="#ffffff"><b>⚙️ 本质</b></font>" 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="<font color="#e65100" style="font-size: 16px;"><b>编译时转换为数组</b></font>" 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="<font face="Courier New" color="#80cbc4">int... nums</font>" 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="<font color="#e65100">编译</font>" 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="<font face="Courier New" color="#80cbc4">int[] nums</font>" 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="<font color="#4caf50" style="font-size: 20px;"><b>≡</b></font>" 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="<font color="#ffffff"><b>📋 规则</b></font>" 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="<font color="#ffffff"><b>1</b></font>" 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="<font color="#2e7d32" style="font-size: 14px;"><b>一个方法最多只能有</b></font><br><font color="#2e7d32" style="font-size: 14px;"><b>一个可变参数</b></font>" 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="<font color="#ffffff"><b>2</b></font>" 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="<font color="#2e7d32" style="font-size: 14px;"><b>可变参数必须是</b></font><br><font color="#2e7d32" style="font-size: 14px;"><b>最后一个参数</b></font>" 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="<font color="#ffffff"><b>3</b></font>" 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="<font color="#2e7d32" style="font-size: 14px;"><b>可变参数可以接收</b></font><br><font color="#2e7d32" style="font-size: 14px;"><b>0 个或多个值</b></font>" 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="<font color="#ffffff"><b>4</b></font>" 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="<font color="#2e7d32" style="font-size: 14px;"><b>可变参数也可以</b></font><br><font color="#2e7d32" style="font-size: 14px;"><b>直接接收数组</b></font>" 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="<font face="Courier New" color="#78909c" style="font-size: 12px;">// 示例</font><br><font face="Courier New" color="#ce93d8" style="font-size: 13px;">void</font><font face="Courier New" color="#ffffff" style="font-size: 13px;"> </font><font face="Courier New" color="#ffcc80" style="font-size: 13px;">print</font><font face="Courier New" color="#ffffff" style="font-size: 13px;">(</font><font face="Courier New" color="#80cbc4" style="font-size: 13px;">String</font><font face="Courier New" color="#ffcc80" style="font-size: 13px;"><b>...</b></font><font face="Courier New" color="#ffffff" style="font-size: 13px;"> messages) { </font><font face="Courier New" color="#78909c" style="font-size: 13px;">/* messages 是 String[] */</font><font face="Courier New" color="#ffffff" style="font-size: 13px;"> }</font>" 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 & 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="精确匹配 > 可变参数" 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="<font style="font-size: 20px;" color="#e65100"><b>核心结论:Java 只有值传递!</b></font>" 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="<font color="#1565c0" style="font-size: 14px;">• <b>基本类型</b>:传递<b>值的副本</b></font>" 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="<font color="#7b1fa2" style="font-size: 14px;">• <b>引用类型</b>:传递<b>引用地址的副本</b></font>" 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="<font color="#ffffff"><b>📦 基本类型</b></font>" 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="<font color="#1565c0" style="font-size: 13px;">原始变量</font><br><font face="Courier New" color="#1565c0" style="font-size: 18px;"><b>a = 10</b></font>" 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="<font color="#e65100" style="font-size: 12px;"><b>传递副本</b></font>" 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="<font color="#1565c0" style="font-size: 13px;">方法参数</font><br><font face="Courier New" color="#1565c0" style="font-size: 18px;"><b>copy = 10</b></font>" 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="<font color="#2e7d32" style="font-size: 14px;">✅ 方法内修改 <b>copy</b><br><b>不影响</b>原始变量 <b>a</b></font>" 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="<font color="#1976d2">两个独立的内存空间</font>" 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="<font color="#ffffff"><b>🔗 引用类型</b></font>" 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="<font color="#7b1fa2" style="font-size: 13px;">原始引用</font><br><font face="Courier New" color="#7b1fa2" style="font-size: 18px;"><b>ref = 0x1</b></font>" 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="<font color="#e65100" style="font-size: 12px;"><b>传递地址副本</b></font>" 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="<font color="#7b1fa2" style="font-size: 13px;">方法参数</font><br><font face="Courier New" color="#7b1fa2" style="font-size: 18px;"><b>copy = 0x1</b></font>" 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="<font color="#7b1fa2" style="font-size: 11px;">指向同一对象</font>" 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="<font color="#e65100" style="font-size: 12px;">堆内存</font><br><font face="Courier New" color="#e65100" style="font-size: 16px;"><b>Object</b></font>" 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="<font color="#2e7d32" style="font-size: 13px;">✅ 通过 <b>copy</b> 可以<br><b>修改对象内容</b></font>" 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="<font color="#c62828" style="font-size: 13px;">❌ 但<b>不能</b>改变<br>原引用 <b>ref</b> 的指向</font>" 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="<font color="#455a64" style="font-size: 12px;">💡 <b>copy = new Object()</b><br>只改变 copy,不影响 ref</font>" 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="<font color="#ffffff"><b>📖 定义</b></font>" 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="<font color="#1565c0" style="font-size: 18px;"><b>方法调用自身</b></font>" 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="<font face="Courier New" color="#1565c0"><b>f()</b></font>" 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="<font color="#ffffff"><b>🔑 递归要素</b></font>" 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="<font color="#ffffff"><b>1</b></font>" 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="<font color="#2e7d32" style="font-size: 15px;"><b>递归出口</b></font><br><font color="#388e3c" style="font-size: 12px;">(终止条件)</font><br><font color="#43a047" style="font-size: 11px;">何时停止递归</font>" 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="<font color="#ffffff"><b>2</b></font>" 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="<font color="#2e7d32" style="font-size: 15px;"><b>递归体</b></font><br><font color="#43a047" style="font-size: 12px;">如何调用自身</font>" 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="<font color="#ffffff"><b>3</b></font>" 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="<font color="#2e7d32" style="font-size: 15px;"><b>递归方向</b></font><br><font color="#43a047" style="font-size: 12px;">必须向着出口方向推进</font>" 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="<font face="Courier New" color="#2e7d32"><b>f(n)</b></font>" 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="<font face="Courier New" color="#2e7d32"><b>f(n-1)</b></font>" 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="<font face="Courier New" color="#2e7d32"><b>f(n-2)</b></font>" 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="<font color="#2e7d32"><b>...</b></font>" 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="<font face="Courier New" color="#ffffff"><b>f(0)</b></font>" 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="<font color="#c62828" style="font-size: 11px;"><b>出口!</b></font>" 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="<font color="#ffffff"><b>⚠️ 注意事项</b></font>" 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="<font color="#c62828" style="font-size: 14px;">❌ 必须有<b>终止条件</b>,否则</font><font face="Courier New" color="#c62828" style="font-size: 14px;"><b>栈溢出(StackOverflowError)</b></font>" 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="<font color="#f57f17" style="font-size: 14px;">⚡ 递归深度<b>不宜过深</b></font>" 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="<font color="#2e7d32" style="font-size: 14px;">✅ 能用<b>循环</b>解决的尽量用循环<br>(效率更高)</font>" 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="<font face="Courier New" color="#c62828" style="font-size: 12px;">f() → f() → f() → f() → f() → ... → 💥 StackOverflowError</font>" 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="<font color="#ffffff"><b>📖 定义</b></font>" 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="<font color="#512da8" style="font-size: 16px;">用于<b>创建对象</b>并<b>初始化对象状态</b>的特殊方法</font>" 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="<font color="#ffffff"><b>✨ 特点</b></font>" 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="<font color="#ffffff"><b>1</b></font>" 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="<font color="#1565c0" style="font-size: 14px;">方法名必须与<b>类名相同</b></font>" 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="<font color="#ffffff"><b>2</b></font>" 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="<font color="#1565c0" style="font-size: 14px;"><b>没有返回值类型</b></font><br><font color="#1976d2" style="font-size: 12px;">(连 void 都没有)</font>" 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="<font color="#ffffff"><b>3</b></font>" 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="<font color="#1565c0" style="font-size: 14px;">不能用 <b>return</b> 返回值</font><br><font color="#1976d2" style="font-size: 11px;">(可用空 return; 提前结束)</font>" 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="<font color="#ffffff"><b>4</b></font>" 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="<font color="#1565c0" style="font-size: 14px;">创建对象时<b>自动调用</b></font><br><font color="#1976d2" style="font-size: 11px;">(不能手动调用)</font>" 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="<font color="#ffffff"><b>5</b></font>" 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="<font color="#1565c0" style="font-size: 14px;">可以<b>重载</b>(多个构造方法)</font>" 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="<font face="Courier New" color="#303f9f" style="font-size: 12px;"><b>public class Person {<br> // 构造方法<br> public Person() {<br> // 初始化代码<br> }<br>}</b></font>" 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="<font color="#ffffff"><b>📋 默认构造方法</b></font>" 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="<font color="#2e7d32" style="font-size: 14px;"><b>✅ 未定义任何构造方法</b></font>" 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="<font color="#2e7d32" style="font-size: 13px;">编译器<b>自动提供</b>无参构造方法</font>" 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="<font color="#c62828" style="font-size: 14px;"><b>❌ 已定义构造方法</b></font>" 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="<font color="#c62828" style="font-size: 13px;">编译器<b>不再提供</b>默认构造方法</font>" 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="<font color="#e65100" style="font-size: 11px;">VS</font>" 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("1.父类静态块"); }" 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("3.父类实例块"); }" 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("4.父类构造方法"); }" 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("2.子类静态块"); }" 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("5.子类实例块"); }" 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("6.子类构造方法");" 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="<font color="#ffffff" style="font-size: 16px;"><b>对比项</b></font>" 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="<font color="#ffffff" style="font-size: 18px;"><b>this</b></font>" 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="<font color="#ffffff" style="font-size: 18px;"><b>super</b></font>" 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="<font color="#3949ab" style="font-size: 14px;"><b>含义</b></font>" 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="<font color="#1565c0" style="font-size: 14px;"><b>当前对象</b>的引用</font>" 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="<font color="#e65100" style="font-size: 14px;"><b>父类对象</b>的引用</font>" 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="<font color="#3949ab" style="font-size: 14px;"><b>访问变量</b></font>" 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="<font face="Courier New" color="#1565c0" style="font-size: 14px;"><b>this.变量名</b></font><br><font color="#1976d2" style="font-size: 12px;">访问当前类的成员变量</font>" 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="<font face="Courier New" color="#e65100" style="font-size: 14px;"><b>super.变量名</b></font><br><font color="#f57c00" style="font-size: 12px;">访问父类的成员变量</font>" 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="<font color="#3949ab" style="font-size: 14px;"><b>访问方法</b></font>" 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="<font face="Courier New" color="#1565c0" style="font-size: 14px;"><b>this.方法名()</b></font><br><font color="#1976d2" style="font-size: 12px;">调用当前类的方法</font>" 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="<font face="Courier New" color="#e65100" style="font-size: 14px;"><b>super.方法名()</b></font><br><font color="#f57c00" style="font-size: 12px;">调用父类的方法</font>" 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="<font color="#3949ab" style="font-size: 14px;"><b>访问构造器</b></font>" 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="<font face="Courier New" color="#1565c0" style="font-size: 14px;"><b>this(参数)</b></font><br><font color="#1976d2" style="font-size: 12px;">调用本类其他构造方法</font>" 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="<font face="Courier New" color="#e65100" style="font-size: 14px;"><b>super(参数)</b></font><br><font color="#f57c00" style="font-size: 12px;">调用父类构造方法</font>" 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="<font color="#3949ab" style="font-size: 14px;"><b>位置要求</b></font>" 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="<font color="#1565c0" style="font-size: 13px;">必须在构造方法<b>第一行</b></font>" 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="<font color="#e65100" style="font-size: 13px;">必须在构造方法<b>第一行</b></font>" 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="<font color="#3949ab" style="font-size: 14px;"><b>静态方法</b></font>" 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="<font color="#c62828" style="font-size: 14px;">❌ <b>不能使用</b></font>" 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="<font color="#c62828" style="font-size: 14px;">❌ <b>不能使用</b></font>" 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="<font color="#c2185b" style="font-size: 14px;">⚠️ <b>注意</b>:this() 和 super() <b>不能同时出现</b>在同一个构造方法中<br>(因为它们都必须在第一行)</font>" 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="<font face="Courier New" color="#1565c0" style="font-size: 12px;"><b>this</b></font><font color="#1976d2" style="font-size: 12px;"> → 本类</font>" 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="<font face="Courier New" color="#e65100" style="font-size: 12px;"><b>super</b></font><font color="#f57c00" style="font-size: 12px;"> → 父类</font>" 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="<font color="#80cbc4">📝 示例代码 (StackTest.java)</font>" 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="<font face="Courier New"><font color="#c792ea">public static void</font> <font color="#82aaff">main</font><font color="#89ddff">(</font><font color="#ffcb6b">String</font><font color="#89ddff">[]</font> <font color="#f78c6c">args</font><font color="#89ddff">)</font> <font color="#89ddff">{</font><br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#c792ea">int</font> <font color="#f78c6c">result</font> <font color="#89ddff">=</font> <font color="#82aaff">add</font><font color="#89ddff">(</font><font color="#f78c6c">3</font><font color="#89ddff">,</font> <font color="#f78c6c">5</font><font color="#89ddff">);</font><br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ffcb6b">System</font><font color="#89ddff">.</font><font color="#f78c6c">out</font><font color="#89ddff">.</font><font color="#82aaff">println</font><font color="#89ddff">(</font><font color="#f78c6c">result</font><font color="#89ddff">);</font><br><font color="#89ddff">}</font></font>" 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="<font face="Courier New"><font color="#c792ea">public static int</font> <font color="#82aaff">add</font><font color="#89ddff">(</font><font color="#c792ea">int</font> <font color="#f78c6c">a</font><font color="#89ddff">,</font> <font color="#c792ea">int</font> <font color="#f78c6c">b</font><font color="#89ddff">)</font> <font color="#89ddff">{</font><br>&nbsp;&nbsp;<font color="#c792ea">int</font> <font color="#f78c6c">sum</font> <font color="#89ddff">=</font> <font color="#f78c6c">a</font> <font color="#89ddff">+</font> <font color="#f78c6c">b</font><font color="#89ddff">;</font><br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#c792ea">return</font> <font color="#f78c6c">sum</font><font color="#89ddff">;</font><br><font color="#89ddff">}</font></font>" 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="<font color="#ffffff" style="font-size: 14px;"><b>Step 1: main() 入栈</b></font>" 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="<font color="#90a4ae" style="font-size: 12px;">(空闲空间)</font>" 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="<font color="#ffffff" style="font-size: 12px;"><b>main() 栈帧</b><br>args = ...<br>result = ?</font>" 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="<font color="#1565c0" style="font-size: 11px;">栈底</font>" 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="<font color="#ffffff" style="font-size: 14px;"><b>Step 2: add() 入栈</b></font>" 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="<font color="#ffffff" style="font-size: 12px;"><b>add() 栈帧</b><br>a = 3<br>b = 5<br>sum = 8</font>" 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="<font color="#ffffff" style="font-size: 12px;"><b>main() 栈帧</b><br>args = ...<br>result = ?</font>" 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="<font color="#e65100" style="font-size: 11px;"><b>PUSH(压栈)</b></font>" 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="<font color="#ffffff" style="font-size: 14px;"><b>Step 3: add() 返回出栈</b></font>" 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="<font color="#c62828" style="font-size: 11px;"><b>POP(弹栈) - 销毁</b><br>返回值 8 传给调用者</font>" 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="<font color="#ffffff" style="font-size: 12px;"><b>main() 栈帧</b><br>args = ...<br>result = 8 ←</font>" 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="<font color="#c2185b" style="font-size: 11px;"><b>返回 8</b></font>" 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="<font color="#ffffff" style="font-size: 14px;"><b>Step 4: main() 继续执行</b></font>" 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="<font color="#90a4ae" style="font-size: 12px;">(空闲空间)</font>" 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="<font color="#ffffff" style="font-size: 12px;"><b>main() 栈帧</b><br>args = ...<br>result = 8</font>" 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="<font color="#2e7d32" style="font-size: 11px;">✓ 输出: 8</font>" 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="<font color="#ffffff" style="font-size: 16px;"><b>💡 栈帧 (Stack Frame) 核心机制</b></font>" 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)就像一个弹夹。<br>最后被调用的方法(如 add)最先入栈,<br>也最先执行完毕出栈。<br>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="每个方法调用都会创建一个独立的栈帧。<br>栈帧包含:<br>• 局部变量表 (Local Variables)<br>• 操作数栈 (Operand Stack)<br>• 动态链接 & 方法返回地址" 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. 线程私有 & 自动管理" 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="栈内存是线程私有的,<br>不存在线程安全问题(局部变量)。<br>栈内存不需要垃圾回收(GC),<br>方法结束自动弹出释放。" 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="<font color="#1565c0" style="font-size: 15px;"><b>重载和重写的区别?</b></font>" 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="<font color="#1976d2" style="font-size: 13px;"><b>重载</b>:同一类中方法名相同、参数不同,<b>编译期绑定</b><br><b>重写</b>:子类重新实现父类方法,<b>运行期绑定</b></font>" 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="<font color="#e65100" style="font-size: 15px;"><b>静态方法能否访问非静态成员?为什么?</b></font>" 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="<font color="#e65100" style="font-size: 13px;"><b>不能</b>。静态方法属于类,在类加载时就存在;<br>非静态成员属于对象,需创建对象后才存在。调用静态方法时可能还没有对象。</font>" 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="<font color="#2e7d32" style="font-size: 15px;"><b>Java 是值传递还是引用传递?</b></font>" 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="<font color="#2e7d32" style="font-size: 13px;">Java <b>只有值传递</b>。<br>• 基本类型:传值的副本<br>• 引用类型:传引用地址的副本(可修改对象内容,但不能改变原引用指向)</font>" 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="<font color="#c2185b" style="font-size: 15px;"><b>成员变量和局部变量的区别?</b></font>" 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="<font color="#c2185b" style="font-size: 12px;">① <b>定义位置</b>:成员变量在类中,局部变量在方法中<br>② <b>存储位置</b>:成员变量在堆中,局部变量在栈中<br>③ <b>生命周期</b>:成员变量随对象,局部变量随方法<br>④ <b>初始化</b>:成员变量有默认值,局部变量必须初始化</font>" 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="<font color="#7b1fa2" style="font-size: 15px;"><b>构造方法能否被重写?能否被重载?</b></font>" 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="<font color="#7b1fa2" style="font-size: 13px;">构造方法<b>不能被重写</b>(不能继承)<br>但<b>可以被重载</b>(同一类中多个构造器)</font>" 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="<font color="#00695c" style="font-size: 15px;"><b>this 和 super 能否同时出现在构造方法中?</b></font>" 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="<font color="#00695c" style="font-size: 13px;"><b>不能</b>。this() 和 super() 都必须是构造方法的<b>第一条语句</b>,<br>所以二者不能同时出现。</font>" 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="<font color="#ffffff" style="font-size: 14px;"><b>重载 (Overload) vs 重写 (Override)</b></font>" 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="<font color="#1565c0" style="font-size: 12px;"><b>对比项</b></font>" 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="<font color="#1565c0" style="font-size: 12px;"><b>重载 Overload</b></font>" 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="<font color="#1565c0" style="font-size: 12px;"><b>重写 Override</b></font>" 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="<font color="#455a64" style="font-size: 11px;">位置</font>" 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="<font color="#1976d2" style="font-size: 11px;">同一个类中</font>" 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="<font color="#1976d2" style="font-size: 11px;">子类与父类之间</font>" 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="<font color="#455a64" style="font-size: 11px;">方法名</font>" 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="<font color="#1976d2" style="font-size: 11px;">必须相同</font>" 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="<font color="#1976d2" style="font-size: 11px;">必须相同</font>" 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="<font color="#455a64" style="font-size: 11px;">参数列表</font>" 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="<font color="#d32f2f" style="font-size: 11px;"><b>必须不同</b></font>" 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="<font color="#d32f2f" style="font-size: 11px;"><b>必须相同</b></font>" 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="<font color="#455a64" style="font-size: 11px;">返回类型</font>" 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="<font color="#455a64" style="font-size: 11px;">无关</font>" 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="<font color="#455a64" style="font-size: 11px;">需相同或为子类</font>" 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="<font color="#455a64" style="font-size: 11px;">异常</font>" 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="<font color="#455a64" style="font-size: 11px;">无关</font>" 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="<font color="#455a64" style="font-size: 11px;">范围不能比父类大</font>" 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="<font color="#455a64" style="font-size: 11px;">访问权限</font>" 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="<font color="#455a64" style="font-size: 11px;">无关</font>" 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="<font color="#455a64" style="font-size: 11px;">不能比父类严</font>" 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="<font color="#ffffff" style="font-size: 14px;"><b>抽象类 (Abstract Class) vs 接口 (Interface)</b></font>" 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="<font color="#00695c" style="font-size: 12px;"><b>对比项</b></font>" 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="<font color="#00695c" style="font-size: 12px;"><b>抽象类</b></font>" 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="<font color="#00695c" style="font-size: 12px;"><b>接口</b></font>" 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="<font color="#455a64" style="font-size: 11px;">成员变量</font>" 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="<font color="#004d40" style="font-size: 11px;">各种类型</font>" 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="<font color="#004d40" style="font-size: 11px;">public static final (常量)</font>" 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="<font color="#455a64" style="font-size: 11px;">继承性</font>" 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="<font color="#004d40" style="font-size: 11px;">单继承</font>" 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="<font color="#004d40" style="font-size: 11px;">多实现</font>" 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="<font color="#455a64" style="font-size: 11px;">构造方法</font>" 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="<font color="#004d40" style="font-size: 11px;">有 (供子类调用)</font>" 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="<font color="#004d40" style="font-size: 11px;">无</font>" 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="<font color="#455a64" style="font-size: 11px;">设计目的</font>" 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="<font color="#004d40" style="font-size: 11px;"><b>代码复用 (IS-A)</b><br>对一类事物的抽象<br>如:狗是动物</font>" 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="<font color="#004d40" style="font-size: 11px;"><b>行为规范 (LIKE-A)</b><br>定义一套行为标准<br>如:飞机能飞,鸟能飞</font>" 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="<font color="#ffffff" style="font-size: 12px;">💡 面试小贴士:回答区别类问题时,建议先说定义,再列举维度(如位置、修饰符、异常等),最后举例说明场景。</font>" 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"); // 修改原对象内容
}
}
💬 评论