面向对象三大特征


〇、三大特征总览

<mxfile>
  <diagram id="oop-three-features" 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="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="630" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🎯 面向对象三大核心特征" vertex="1">
          <mxGeometry height="60" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="290" width="820" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="120" width="230" x="70" y="130" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=36;" value="🔒" vertex="1">
          <mxGeometry height="40" width="60" x="155" y="135" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=22;fontColor=#FFFFFF;fontStyle=1;" value="封装" vertex="1">
          <mxGeometry height="35" width="230" x="70" y="175" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#93C5FD;fontStyle=2;" value="Encapsulation" vertex="1">
          <mxGeometry height="30" width="230" x="70" y="210" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="120" width="230" x="335" y="130" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=36;" value="🔗" vertex="1">
          <mxGeometry height="40" width="60" x="420" y="135" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=22;fontColor=#FFFFFF;fontStyle=1;" value="继承" vertex="1">
          <mxGeometry height="35" width="230" x="335" y="175" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#6EE7B7;fontStyle=2;" value="Inheritance" vertex="1">
          <mxGeometry height="30" width="230" x="335" y="210" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="120" width="230" x="600" y="130" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=36;" value="🔄" vertex="1">
          <mxGeometry height="40" width="60" x="685" y="135" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=22;fontColor=#FFFFFF;fontStyle=1;" value="多态" vertex="1">
          <mxGeometry height="35" width="230" x="600" y="175" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#FDE68A;fontStyle=2;" value="Polymorphism" vertex="1">
          <mxGeometry height="30" width="230" x="600" y="210" as="geometry" />
        </mxCell>
        <mxCell id="17" edge="1" parent="1" source="5" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#3B82F6;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="18" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="230" x="70" y="280" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#93C5FD;fontStyle=1;whiteSpace=wrap;" value="隐藏细节&#xa;保护数据" vertex="1">
          <mxGeometry height="80" width="230" x="70" y="280" as="geometry" />
        </mxCell>
        <mxCell id="20" edge="1" parent="1" source="9" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#10B981;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="21" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#064E3B;strokeColor=#10B981;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="230" x="335" y="280" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#6EE7B7;fontStyle=1;whiteSpace=wrap;" value="代码复用&#xa;层次结构" vertex="1">
          <mxGeometry height="80" width="230" x="335" y="280" as="geometry" />
        </mxCell>
        <mxCell id="23" edge="1" parent="1" source="13" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#F59E0B;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="24" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#78350F;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="230" x="600" y="280" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#FDE68A;fontStyle=1;whiteSpace=wrap;" value="灵活扩展&#xa;统一接口" vertex="1">
          <mxGeometry height="80" width="230" x="600" y="280" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔗 三者关系" vertex="1">
          <mxGeometry height="40" width="820" x="40" y="410" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="70" width="220" x="60" y="470" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="封装是基础" vertex="1">
          <mxGeometry height="30" width="220" x="60" y="475" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#93C5FD;" value="隐藏实现细节" vertex="1">
          <mxGeometry height="25" width="220" x="60" y="505" as="geometry" />
        </mxCell>
        <mxCell id="30" edge="1" parent="1" source="27" style="endArrow=classic;html=1;strokeWidth=4;strokeColor=#6366F1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;exitX=1;exitY=0.5;exitDx=0;exitDy=0;" target="31" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#064E3B;strokeColor=#10B981;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="70" width="220" x="340" y="470" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="继承是手段" vertex="1">
          <mxGeometry height="30" width="220" x="340" y="475" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#6EE7B7;" value="实现代码复用" vertex="1">
          <mxGeometry height="25" width="220" x="340" y="505" as="geometry" />
        </mxCell>
        <mxCell id="34" edge="1" parent="1" source="31" style="endArrow=classic;html=1;strokeWidth=4;strokeColor=#6366F1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;exitX=1;exitY=0.5;exitDx=0;exitDy=0;" target="35" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#78350F;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="70" width="220" x="620" y="470" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="多态是目的" vertex="1">
          <mxGeometry height="30" width="220" x="620" y="475" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#FDE68A;" value="实现灵活设计" vertex="1">
          <mxGeometry height="25" width="220" x="620" y="505" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;strokeWidth=2;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="基础" vertex="1">
          <mxGeometry height="24" width="45" x="290" y="490" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=2;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="手段" vertex="1">
          <mxGeometry height="24" width="45" x="567" y="490" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#6366F1;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="55" width="780" x="60" y="560" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#E2E8F0;fontStyle=1;" value="💡 封装提供安全性 → 继承实现复用性 → 多态带来灵活性" vertex="1">
          <mxGeometry height="55" width="780" x="60" y="560" as="geometry" />
        </mxCell>
        <mxCell id="42" edge="1" parent="1" source="2" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" target="2">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

一、封装(Encapsulation)

1.1 概念定义

<mxfile>
  <diagram id="encapsulation-essence" 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="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=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="720" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔒 封装的本质" vertex="1">
          <mxGeometry height="60" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="60" width="820" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontColor=#E2E8F0;fontStyle=1;" value="封装 = 隐藏对象内部细节 + 只暴露必要的接口" vertex="1">
          <mxGeometry height="60" width="820" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="380" width="820" x="40" y="180" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#0EA5E9;strokeColor=#0284C7;strokeWidth=2;opacity=80;" value="" vertex="1">
          <mxGeometry height="50" width="140" x="380" y="200" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="🌐 外部世界" vertex="1">
          <mxGeometry height="50" width="140" x="380" y="200" as="geometry" />
        </mxCell>
        <mxCell id="9" edge="1" parent="1" source="7" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#0EA5E9;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="12" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="450" y="320" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#7DD3FC;fontStyle=2;" value="只能通过公开接口访问" vertex="1">
          <mxGeometry height="30" width="160" x="460" y="265" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="230" width="620" x="140" y="310" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="对象" vertex="1">
          <mxGeometry height="30" width="620" x="140" y="310" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#059669;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="560" x="170" y="355" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="📤 公开接口 (public)" vertex="1">
          <mxGeometry height="30" width="560" x="170" y="355" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#A7F3D0;fontFamily=Courier New;" value="getter()  /  setter()  /  业务方法()" vertex="1">
          <mxGeometry height="30" width="560" x="170" y="385" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7F1D1D;strokeColor=#EF4444;strokeWidth=2;dashed=1;" value="" vertex="1">
          <mxGeometry height="80" width="560" x="170" y="445" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#FCA5A5;fontStyle=1;" value="🔐 私有数据 (private)" vertex="1">
          <mxGeometry height="30" width="280" x="170" y="450" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#FCA5A5;fontStyle=1;" value="🔐 私有方法 (private)" vertex="1">
          <mxGeometry height="30" width="280" x="450" y="450" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#991B1B;" value="████████████████" vertex="1">
          <mxGeometry height="30" width="280" x="170" y="480" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#991B1B;" value="████████████████" vertex="1">
          <mxGeometry height="30" width="280" x="450" y="480" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#EF4444;fontStyle=1;" value="← 隐藏" vertex="1">
          <mxGeometry height="30" width="60" x="740" y="470" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#10B981;fontStyle=1;" value="← 暴露" vertex="1">
          <mxGeometry height="30" width="60" x="740" y="375" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📺 类比:电视机" vertex="1">
          <mxGeometry height="40" width="820" x="40" y="580" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#059669;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="70" width="380" x="60" y="640" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="✅ 暴露:遥控器按钮" vertex="1">
          <mxGeometry height="30" width="380" x="60" y="645" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#A7F3D0;" value="开关 🔘  音量 🔊  频道 📺" vertex="1">
          <mxGeometry height="25" width="380" x="60" y="675" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7F1D1D;strokeColor=#EF4444;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="70" width="380" x="460" y="640" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="❌ 隐藏:内部结构" vertex="1">
          <mxGeometry height="30" width="380" x="460" y="645" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#FCA5A5;" value="电路板 🔌  芯片 💾  电子元件 ⚡" vertex="1">
          <mxGeometry height="25" width="380" x="460" y="675" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=40;" value="🛡️" vertex="1">
          <mxGeometry height="60" width="60" x="70" y="310" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

1.2 访问修饰符

修饰符 同类 同包 子类 不同包 使用场景
private 属性、内部方法
(default) 包内工具类
protected 允许子类访问
public 对外公开的接口

可见性范围图

<mxfile>
  <diagram id="access-modifier-scope" 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="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="20" y="20" width="860" height="660" as="geometry" />
        </mxCell>
        <mxCell id="3" value="🔐 访问权限范围" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="20" y="20" width="860" height="60" as="geometry" />
        </mxCell>
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#059669;strokeWidth=4;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="100" width="780" height="480" as="geometry" />
        </mxCell>
        <mxCell id="5" value="public" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=top;fontSize=20;fontColor=#FFFFFF;fontStyle=1;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="80" y="110" width="120" height="30" as="geometry" />
        </mxCell>
        <mxCell id="6" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=4;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="150" width="700" height="390" as="geometry" />
        </mxCell>
        <mxCell id="7" value="protected" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=top;fontSize=20;fontColor=#FFFFFF;fontStyle=1;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="120" y="160" width="140" height="30" as="geometry" />
        </mxCell>
        <mxCell id="8" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;strokeWidth=4;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="140" y="200" width="620" height="300" as="geometry" />
        </mxCell>
        <mxCell id="9" value="default (包访问权限)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=top;fontSize=20;fontColor=#FFFFFF;fontStyle=1;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="160" y="210" width="280" height="30" as="geometry" />
        </mxCell>
        <mxCell id="10" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=4;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="180" y="250" width="540" height="210" as="geometry" />
        </mxCell>
        <mxCell id="11" value="private" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=top;fontSize=20;fontColor=#FFFFFF;fontStyle=1;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="200" y="260" width="120" height="30" as="geometry" />
        </mxCell>
        <mxCell id="12" value="🔒 只有本类可见" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="180" y="320" width="540" height="80" as="geometry" />
        </mxCell>
        <mxCell id="13" value="📦 同包可见" style="text;html=1;strokeColor=none;fillColor=none;align=right;verticalAlign=bottom;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="540" y="460" width="200" height="30" as="geometry" />
        </mxCell>
        <mxCell id="14" value="👨👩👧 同包 + 子类可见" style="text;html=1;strokeColor=none;fillColor=none;align=right;verticalAlign=bottom;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="540" y="500" width="240" height="30" as="geometry" />
        </mxCell>
        <mxCell id="15" value="🌍 所有地方可见" style="text;html=1;strokeColor=none;fillColor=none;align=right;verticalAlign=bottom;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="600" y="545" width="220" height="30" as="geometry" />
        </mxCell>
        <mxCell id="16" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="600" width="780" height="60" as="geometry" />
        </mxCell>
        <mxCell id="17" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="80" y="615" width="30" height="30" as="geometry" />
        </mxCell>
        <mxCell id="18" value="private" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#FCA5A5;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="115" y="615" width="70" height="30" as="geometry" />
        </mxCell>
        <mxCell id="19" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="200" y="615" width="30" height="30" as="geometry" />
        </mxCell>
        <mxCell id="20" value="default" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#93C5FD;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="235" y="615" width="70" height="30" as="geometry" />
        </mxCell>
        <mxCell id="21" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="320" y="615" width="30" height="30" as="geometry" />
        </mxCell>
        <mxCell id="22" value="protected" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#FCD34D;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="355" y="615" width="80" height="30" as="geometry" />
        </mxCell>
        <mxCell id="23" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="450" y="615" width="30" height="30" as="geometry" />
        </mxCell>
        <mxCell id="24" value="public" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#6EE7B7;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="485" y="615" width="60" height="30" as="geometry" />
        </mxCell>
        <mxCell id="25" value="范围:小 ◀─────────────────▶ 大" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#94A3B8;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="560" y="615" width="260" height="30" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

1.3 完整代码示例

/**
 * 银行账户类 - 封装示例
 */
public class BankAccount {
    
    // ========== 私有属性(隐藏数据)==========
    private String accountNumber;  // 账号
    private String ownerName;      // 户主
    private double balance;        // 余额
    private String password;       // 密码
    
    // ========== 构造方法 ==========
    public BankAccount(String accountNumber, String ownerName, String password) {
        this.accountNumber = accountNumber;
        this.ownerName = ownerName;
        this.password = password;
        this.balance = 0.0;
    }
    
    // ========== 公开接口(Getter/Setter)==========
    
    // 账号:只读(没有 setter)
    public String getAccountNumber() {
        // 返回脱敏后的账号
        return accountNumber.substring(0, 4) + "****" + 
               accountNumber.substring(accountNumber.length() - 4);
    }
    
    // 户主:只读
    public String getOwnerName() {
        return ownerName;
    }
    
    // 余额:只读(不能直接设置余额)
    public double getBalance() {
        return balance;
    }
    
    // 密码:只写(没有 getter,不能读取密码)
    public void setPassword(String oldPassword, String newPassword) {
        if (validatePassword(oldPassword)) {
            this.password = newPassword;
            System.out.println("密码修改成功");
        } else {
            System.out.println("原密码错误");
        }
    }
    
    // ========== 业务方法(公开接口)==========
    
    /**
     * 存款
     */
    public void deposit(double amount) {
        if (amount <= 0) {
            System.out.println("存款金额必须大于0");
            return;
        }
        balance += amount;
        System.out.println("存款成功,当前余额:" + balance);
    }
    
    /**
     * 取款(需要验证密码)
     */
    public boolean withdraw(double amount, String password) {
        // 1. 验证密码
        if (!validatePassword(password)) {
            System.out.println("密码错误");
            return false;
        }
        
        // 2. 验证金额
        if (amount <= 0) {
            System.out.println("取款金额必须大于0");
            return false;
        }
        
        // 3. 验证余额
        if (amount > balance) {
            System.out.println("余额不足");
            return false;
        }
        
        // 4. 执行取款
        balance -= amount;
        System.out.println("取款成功,当前余额:" + balance);
        return true;
    }
    
    // ========== 私有方法(隐藏实现)==========
    
    /**
     * 验证密码(私有方法,外部不可见)
     */
    private boolean validatePassword(String inputPassword) {
        return this.password.equals(inputPassword);
    }
}

1.4 使用示例

public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount("6222021234567890", "张三", "123456");
        
        // ✅ 通过公开方法操作
        account.deposit(1000);                    // 存款
        account.withdraw(500, "123456");          // 取款
        
        // ✅ 通过 getter 获取信息
        System.out.println("账号:" + account.getAccountNumber());  // 6222****7890
        System.out.println("余额:" + account.getBalance());        // 500.0
        
        // ❌ 无法直接访问私有属性
        // account.balance = 999999;              // 编译错误
        // account.password = "hack";             // 编译错误
        // String pwd = account.password;         // 编译错误
        
        // ❌ 无法调用私有方法
        // account.validatePassword("123456");    // 编译错误
    }
}

1.5 封装的好处

<mxfile>
  <diagram id="encapsulation-benefits" 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="780" 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=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="750" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🎯 封装的四大好处" vertex="1">
          <mxGeometry height="60" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="145" width="820" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=24;fontColor=#FFFFFF;rounded=1;" value="1️⃣" vertex="1">
          <mxGeometry height="50" width="50" x="60" y="115" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=20;fontColor=#3B82F6;fontStyle=1;" value="隐藏实现细节" vertex="1">
          <mxGeometry height="50" width="200" x="120" y="115" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=32;" value="👁️" vertex="1">
          <mxGeometry height="50" width="60" x="780" y="115" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="780" x="60" y="170" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#E2E8F0;" value="用户只需知道怎么用,不需要知道内部实现" vertex="1">
          <mxGeometry height="30" width="400" x="80" y="170" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#94A3B8;fontStyle=2;" value="💡 例:调用 withdraw() 即可取款,无需了解验证逻辑" vertex="1">
          <mxGeometry height="25" width="450" x="80" y="200" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#10B981;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="145" width="820" x="40" y="260" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=24;fontColor=#FFFFFF;rounded=1;" value="2️⃣" vertex="1">
          <mxGeometry height="50" width="50" x="60" y="275" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=20;fontColor=#10B981;fontStyle=1;" value="保护数据安全" vertex="1">
          <mxGeometry height="50" width="200" x="120" y="275" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=32;" value="🛡️" vertex="1">
          <mxGeometry height="50" width="60" x="780" y="275" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="780" x="60" y="330" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#E2E8F0;" value="防止外部随意修改数据" vertex="1">
          <mxGeometry height="30" width="400" x="80" y="330" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#94A3B8;fontStyle=2;" value="💡 例:balance 只能通过 deposit/withdraw 修改" vertex="1">
          <mxGeometry height="25" width="450" x="80" y="360" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="145" width="820" x="40" y="420" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=24;fontColor=#FFFFFF;rounded=1;" value="3️⃣" vertex="1">
          <mxGeometry height="50" width="50" x="60" y="435" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=20;fontColor=#F59E0B;fontStyle=1;" value="便于修改实现" vertex="1">
          <mxGeometry height="50" width="200" x="120" y="435" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=32;" value="🔧" vertex="1">
          <mxGeometry height="50" width="60" x="780" y="435" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="780" x="60" y="490" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#E2E8F0;" value="内部实现可以随时修改,不影响外部调用" vertex="1">
          <mxGeometry height="30" width="400" x="80" y="490" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#94A3B8;fontStyle=2;" value="💡 例:密码从明文改为加密存储,外部代码无需修改" vertex="1">
          <mxGeometry height="25" width="450" x="80" y="520" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#EF4444;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="145" width="820" x="40" y="580" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=24;fontColor=#FFFFFF;rounded=1;" value="4️⃣" vertex="1">
          <mxGeometry height="50" width="50" x="60" y="595" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=20;fontColor=#EF4444;fontStyle=1;" value="数据验证" vertex="1">
          <mxGeometry height="50" width="200" x="120" y="595" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=32;" value="✅" vertex="1">
          <mxGeometry height="50" width="60" x="780" y="595" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="780" x="60" y="650" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#E2E8F0;" value="在 setter / 业务方法中加入验证逻辑" vertex="1">
          <mxGeometry height="30" width="400" x="80" y="650" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#94A3B8;fontStyle=2;" value="💡 例:存款金额必须 &gt; 0,取款不能超过余额" vertex="1">
          <mxGeometry height="25" width="450" x="80" y="680" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

1.6 JavaBean 规范

/**
 * 标准 JavaBean 示例
 */
public class User {
    // 1. 私有属性
    private Long id;
    private String username;
    private String email;
    private Integer age;
    
    // 2. 无参构造
    public User() {
    }
    
    // 3. 全参构造(可选)
    public User(Long id, String username, String email, Integer age) {
        this.id = id;
        this.username = username;
        this.email = email;
        this.age = age;
    }
    
    // 4. 标准 Getter/Setter
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    public Integer getAge() {
        return age;
    }
    
    public void setAge(Integer age) {
        // 可以加入验证逻辑
        if (age != null && age >= 0 && age <= 150) {
            this.age = age;
        }
    }
    
    // 5. toString 方法(可选)
    @Override
    public String toString() {
        return "User{id=" + id + ", username='" + username + "', email='" + email + "', age=" + age + "}";
    }
}

二、继承(Inheritance)

2.1 概念定义

<mxfile>
  <diagram id="inheritance-essence" 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="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="650" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🧬 继承的本质" vertex="1">
          <mxGeometry height="60" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="820" x="40" y="95" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#E2E8F0;fontStyle=1;" value="继承 = 子类自动获得父类的属性和方法 + 可以扩展自己的特性" vertex="1">
          <mxGeometry height="50" width="820" x="40" y="95" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#8B5CF6;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="400" width="820" x="40" y="160" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="Animal" vertex="1">
          <mxGeometry height="40" width="200" x="350" y="185" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#6D28D9;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;" value="父类" vertex="1">
          <mxGeometry height="40" width="50" x="560" y="185" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#312E81;strokeColor=#8B5CF6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="200" x="350" y="225" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#C4B5FD;fontFamily=Courier New;spacingLeft=10;" value="- name&#xa;- age" vertex="1">
          <mxGeometry height="70" width="200" x="350" y="225" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#312E81;strokeColor=#8B5CF6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="200" x="350" y="295" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#C4B5FD;fontFamily=Courier New;spacingLeft=10;" value="+ eat()&#xa;+ sleep()" vertex="1">
          <mxGeometry height="70" width="200" x="350" y="295" as="geometry" />
        </mxCell>
        <mxCell id="13" edge="1" parent="1" style="endArrow=none;html=1;strokeColor=#8B5CF6;strokeWidth=3;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="450" y="365" as="sourcePoint" />
            <mxPoint x="450" y="410" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="14" edge="1" parent="1" style="endArrow=none;html=1;strokeColor=#8B5CF6;strokeWidth=3;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="250" y="410" as="sourcePoint" />
            <mxPoint x="650" y="410" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="15" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#8B5CF6;strokeWidth=3;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="250" y="410" as="sourcePoint" />
            <mxPoint x="250" y="450" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="16" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#8B5CF6;strokeWidth=3;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="650" y="410" as="sourcePoint" />
            <mxPoint x="650" y="450" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#A78BFA;fontStyle=2;" value="extends" vertex="1">
          <mxGeometry height="30" width="80" x="460" y="380" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="Dog" vertex="1">
          <mxGeometry height="40" width="200" x="150" y="450" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="150" y="490" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#93C5FD;fontFamily=Courier New;spacingLeft=10;" value="+ bark()" vertex="1">
          <mxGeometry height="50" width="200" x="150" y="490" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="Cat" vertex="1">
          <mxGeometry height="40" width="200" x="550" y="450" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#064E3B;strokeColor=#10B981;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="550" y="490" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#6EE7B7;fontFamily=Courier New;spacingLeft=10;" value="+ meow()" vertex="1">
          <mxGeometry height="50" width="200" x="550" y="490" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#F59E0B;fontStyle=1;" value="← 子类扩展" vertex="1">
          <mxGeometry height="50" width="90" x="760" y="490" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#0F172A;align=center;verticalAlign=middle;fontSize=14;fontColor=#A78BFA;rounded=1;strokeWidth=1;" value="✅ Dog 和 Cat 自动拥有 name、age、eat()、sleep()" vertex="1">
          <mxGeometry height="35" width="600" x="150" y="555" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="790" x="50" y="590" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="💡" vertex="1">
          <mxGeometry height="50" width="50" x="60" y="585" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#FCD34D;fontStyle=2;" value="类比:孩子继承父母的基因(外貌、血型等),同时有自己的个性" vertex="1">
          <mxGeometry height="40" width="690" x="120" y="595" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

2.2 基本语法

class 子类 extends 父类 {
    // 子类的属性和方法
}

2.3 完整代码示例

父类定义

/**
 * 员工类(父类)
 */
public class Employee {
    // ===== 属性 =====
    protected String name;      // protected:子类可访问
    protected double salary;
    private String id;          // private:子类不可直接访问

    // ===== 构造方法 =====
    public Employee() {
        System.out.println("Employee 无参构造");
    }

    public Employee(String name, double salary, String id) {
        this.name = name;
        this.salary = salary;
        this.id = id;
        System.out.println("Employee 有参构造");
    }

    // ===== 方法 =====
    public void work() {
        System.out.println(name + " 正在工作");
    }

    public void showInfo() {
        System.out.println("姓名:" + name + ",薪资:" + salary);
    }

    // Getter/Setter
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

子类定义

/**
 * 程序员类(子类)
 */
public class Programmer extends Employee {
    // ===== 子类特有属性 =====
    private String language;

    // ===== 构造方法 =====
    public Programmer() {
        super();  // 调用父类无参构造(可省略)
        System.out.println("Programmer 无参构造");
    }

    public Programmer(String name, double salary, String id, String language) {
        super(name, salary, id);  // 必须在第一行调用父类构造
        this.language = language;
        System.out.println("Programmer 有参构造");
    }

    // ===== 重写父类方法 =====
    @Override
    public void work() {
        System.out.println(name + " 正在用 " + language + " 编程");
    }

    // ===== 子类特有方法 =====
    public void debug() {
        System.out.println(name + " 正在调试代码");
    }

    public void code() {
        // 可以访问 protected 属性
        System.out.println(name + "(薪资:" + salary + ")开始写代码");

        // 不能直接访问 private 属性,需通过 getter
        System.out.println("工号:" + getId());
    }
}

使用示例

public class Main {
    public static void main(String[] args) {
        // 创建子类对象
        Programmer p = new Programmer("张三", 15000, "P001", "Java");

        // 调用继承的方法
        p.showInfo();   // 输出:姓名:张三,薪资:15000.0

        // 调用重写的方法
        p.work();       // 输出:张三 正在用 Java 编程

        // 调用子类特有方法
        p.debug();      // 输出:张三 正在调试代码
        p.code();
    }
}

输出结果

Employee 有参构造
Programmer 有参构造
姓名:张三,薪资:15000.0
张三 正在用 Java 编程
张三 正在调试代码
张三(薪资:15000.0)开始写代码
工号:P001

2.4 super 关键字详解

<mxfile>
  <diagram id="super-keyword-usage" name="super关键字用法">
    <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="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=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="720" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#EC4899;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔑 super 关键字用法" vertex="1">
          <mxGeometry height="60" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="185" width="820" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=none;fontSize=18;fontStyle=1;fontColor=#FFFFFF;" value="1" vertex="1">
          <mxGeometry height="40" width="40" x="60" y="115" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontColor=#3B82F6;fontStyle=1;" value="super() - 调用父类构造方法" vertex="1">
          <mxGeometry height="40" width="350" x="110" y="115" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="100" width="780" x="60" y="165" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#93C5FD;fontFamily=Courier New;" value="public Programmer(String name, double salary) {" vertex="1">
          <mxGeometry height="25" width="500" x="80" y="175" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E3A5F;align=left;verticalAlign=middle;fontSize=14;fontColor=#FCD34D;fontFamily=Courier New;rounded=1;" value="super(name, salary);" vertex="1">
          <mxGeometry height="25" width="220" x="100" y="200" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#F87171;fontFamily=Courier New;fontStyle=2;" value="// 必须在第一行" vertex="1">
          <mxGeometry height="25" width="150" x="330" y="200" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#93C5FD;fontFamily=Courier New;" value="}" vertex="1">
          <mxGeometry height="25" width="50" x="80" y="230" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#10B981;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="185" width="820" x="40" y="300" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=none;fontSize=18;fontStyle=1;fontColor=#FFFFFF;" value="2" vertex="1">
          <mxGeometry height="40" width="40" x="60" y="315" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontColor=#10B981;fontStyle=1;" value="super.属性 - 访问父类属性" vertex="1">
          <mxGeometry height="40" width="350" x="110" y="315" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="100" width="780" x="60" y="365" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#6EE7B7;fontFamily=Courier New;" value="public void show() {" vertex="1">
          <mxGeometry height="25" width="300" x="80" y="375" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#064E3B;align=left;verticalAlign=middle;fontSize=14;fontColor=#FCD34D;fontFamily=Courier New;rounded=1;" value="System.out.println(super.name);" vertex="1">
          <mxGeometry height="25" width="330" x="100" y="400" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#94A3B8;fontFamily=Courier New;fontStyle=2;" value="// 父类属性" vertex="1">
          <mxGeometry height="25" width="120" x="440" y="400" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#6EE7B7;fontFamily=Courier New;" value="}" vertex="1">
          <mxGeometry height="25" width="50" x="80" y="430" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="210" width="820" x="40" y="500" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=none;fontSize=18;fontStyle=1;fontColor=#FFFFFF;" value="3" vertex="1">
          <mxGeometry height="40" width="40" x="60" y="515" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontColor=#F59E0B;fontStyle=1;" value="super.方法() - 调用父类方法" vertex="1">
          <mxGeometry height="40" width="350" x="110" y="515" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="125" width="780" x="60" y="565" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#A78BFA;fontFamily=Courier New;" value="@Override" vertex="1">
          <mxGeometry height="25" width="120" x="80" y="575" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#FCD34D;fontFamily=Courier New;" value="public void work() {" vertex="1">
          <mxGeometry height="25" width="250" x="80" y="600" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=#78350F;align=left;verticalAlign=middle;fontSize=14;fontColor=#FCD34D;fontFamily=Courier New;rounded=1;" value="super.work();" vertex="1">
          <mxGeometry height="25" width="150" x="100" y="625" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#94A3B8;fontFamily=Courier New;fontStyle=2;" value="// 先执行父类的 work" vertex="1">
          <mxGeometry height="25" width="200" x="260" y="625" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#FCD34D;fontFamily=Courier New;" value="System.out.println(&quot;然后写代码&quot;);" vertex="1">
          <mxGeometry height="25" width="350" x="100" y="650" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#FCD34D;fontFamily=Courier New;" value="}" vertex="1">
          <mxGeometry height="25" width="50" x="80" y="675" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=24;" value="⚠️" vertex="1">
          <mxGeometry height="40" width="40" x="770" y="115" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=24;" value="📖" vertex="1">
          <mxGeometry height="40" width="40" x="770" y="315" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=24;" value="🔄" vertex="1">
          <mxGeometry height="40" width="40" x="770" y="515" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

super vs this 对比

关键字 指向 用途
this 当前对象 访问本类成员、调用本类构造
super 父类部分 访问父类成员、调用父类构造
public class Child extends Parent {
    private String name = "子类名";

    public void show() {
        System.out.println(this.name);   // 子类名
        System.out.println(super.name);  // 父类名(假设父类有同名属性)
    }

    public Child() {
        // this() 和 super() 不能同时存在,都要求在第一行
        super();      // 调用父类构造
        // this("参数"); // 编译错误
    }
}

2.5 方法重写(Override)

<mxfile>
  <diagram id="method-override-rules-fixed" name="方法重写规则">
    <mxGraphModel dx="1040" dy="820" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="760" pageHeight="880" 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=#424242;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="30" y="30" width="700" height="820" as="geometry" />
        </mxCell>
        <mxCell id="3" value="Java 方法重写 (Override) 核心规则" style="text;html=1;strokeColor=none;fillColor=#5c6bc0;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=22;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="30" y="30" width="700" height="55" as="geometry" />
        </mxCell>
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#5c6bc0;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="105" width="640" height="50" as="geometry" />
        </mxCell>
        <mxCell id="5" value="📖" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;" vertex="1" parent="1">
          <mxGeometry x="75" y="113" width="40" height="35" as="geometry" />
        </mxCell>
        <mxCell id="6" value="方法重写 = 子类重新定义父类中已有的方法" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#3949ab;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="120" y="113" width="560" height="35" as="geometry" />
        </mxCell>
        <mxCell id="7" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#7986cb;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="175" width="640" height="420" as="geometry" />
        </mxCell>
        <mxCell id="8" value="📌 5 大核心规则" style="text;html=1;strokeColor=none;fillColor=#7986cb;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=16;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="60" y="175" width="640" height="40" as="geometry" />
        </mxCell>

        <mxCell id="9" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="230" width="600" height="50" as="geometry" />
        </mxCell>
        <mxCell id="10" value="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;fontColor=#ffffff;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="95" y="240" width="30" height="30" as="geometry" />
        </mxCell>
        <mxCell id="11" value="方法签名必须相同(&lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;方法名 + 参数列表&lt;/b&gt;&lt;/font&gt;)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="135" y="240" width="530" height="30" as="geometry" />
        </mxCell>

        <mxCell id="12" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#43a047;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="290" width="600" height="50" as="geometry" />
        </mxCell>
        <mxCell id="13" value="2" style="ellipse;whiteSpace=wrap;html=1;fillColor=#43a047;strokeColor=none;fontColor=#ffffff;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="95" y="300" width="30" height="30" as="geometry" />
        </mxCell>
        <mxCell id="14" value="返回类型:&lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;相同&lt;/b&gt;&lt;/font&gt;或是&lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;子类型&lt;/b&gt;&lt;/font&gt;(协变返回)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="135" y="300" width="530" height="30" as="geometry" />
        </mxCell>

        <mxCell id="15" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#fb8c00;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="350" width="600" height="90" as="geometry" />
        </mxCell>
        <mxCell id="16" value="3" style="ellipse;whiteSpace=wrap;html=1;fillColor=#fb8c00;strokeColor=none;fontColor=#ffffff;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="95" y="360" width="30" height="30" as="geometry" />
        </mxCell>
        <mxCell id="17" value="访问权限:&lt;font color=&quot;#e65100&quot;&gt;&lt;b&gt;不能比父类更严格&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="135" y="358" width="350" height="30" as="geometry" />
        </mxCell>
        <mxCell id="18" value="父类 public → 子类必须 public" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#666666;fontFamily=Consolas;" vertex="1" parent="1">
          <mxGeometry x="155" y="388" width="250" height="22" as="geometry" />
        </mxCell>
        <mxCell id="19" value="父类 protected → 子类可以 protected 或 public" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#666666;fontFamily=Consolas;" vertex="1" parent="1">
          <mxGeometry x="155" y="410" width="350" height="22" as="geometry" />
        </mxCell>

        <mxCell id="20" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fce4ec;strokeColor=#e91e63;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="450" width="600" height="50" as="geometry" />
        </mxCell>
        <mxCell id="21" value="4" style="ellipse;whiteSpace=wrap;html=1;fillColor=#e91e63;strokeColor=none;fontColor=#ffffff;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="95" y="460" width="30" height="30" as="geometry" />
        </mxCell>
        <mxCell id="22" value="异常:&lt;font color=&quot;#c2185b&quot;&gt;&lt;b&gt;不能抛出比父类更多/更广的异常&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="135" y="460" width="530" height="30" as="geometry" />
        </mxCell>

        <mxCell id="23" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f3e5f5;strokeColor=#9c27b0;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="510" width="600" height="70" as="geometry" />
        </mxCell>
        <mxCell id="24" value="5" style="ellipse;whiteSpace=wrap;html=1;fillColor=#9c27b0;strokeColor=none;fontColor=#ffffff;fontSize=14;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="95" y="525" width="30" height="30" as="geometry" />
        </mxCell>
        <mxCell id="25" value="不能重写:" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="135" y="525" width="80" height="30" as="geometry" />
        </mxCell>
        <mxCell id="26" value="static" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#9c27b0;strokeColor=none;fontColor=#ffffff;fontSize=13;fontStyle=1;fontFamily=Consolas;" vertex="1" parent="1">
          <mxGeometry x="220" y="530" width="70" height="28" as="geometry" />
        </mxCell>
        <mxCell id="27" value="final" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#9c27b0;strokeColor=none;fontColor=#ffffff;fontSize=13;fontStyle=1;fontFamily=Consolas;" vertex="1" parent="1">
          <mxGeometry x="300" y="530" width="70" height="28" as="geometry" />
        </mxCell>
        <mxCell id="28" value="private" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#9c27b0;strokeColor=none;fontColor=#ffffff;fontSize=13;fontStyle=1;fontFamily=Consolas;" vertex="1" parent="1">
          <mxGeometry x="380" y="530" width="70" height="28" as="geometry" />
        </mxCell>
        <mxCell id="29" value="方法" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" vertex="1" parent="1">
          <mxGeometry x="460" y="525" width="50" height="30" as="geometry" />
        </mxCell>
        <mxCell id="30" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff8e1;strokeColor=#ffa000;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="615" width="640" height="175" as="geometry" />
        </mxCell>
        <mxCell id="31" value="💡 记忆口诀" style="text;html=1;strokeColor=none;fillColor=#ffa000;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=16;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="60" y="615" width="640" height="40" as="geometry" />
        </mxCell>
        <mxCell id="32" value="&quot;两同 两小 一大&quot;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;fontStyle=1;fontColor=#ff6f00;" vertex="1" parent="1">
          <mxGeometry x="60" y="665" width="640" height="40" as="geometry" />
        </mxCell>
        <mxCell id="33" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="715" width="195" height="50" as="geometry" />
        </mxCell>
        <mxCell id="34" value="两同" style="text;html=1;strokeColor=none;fillColor=#1976d2;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=13;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="90" y="725" width="45" height="28" as="geometry" />
        </mxCell>
        <mxCell id="35" value="方法名相同&#xa;参数列表相同" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#1565c0;" vertex="1" parent="1">
          <mxGeometry x="145" y="718" width="120" height="40" as="geometry" />
        </mxCell>
        <mxCell id="36" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#43a047;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="285" y="715" width="210" height="50" as="geometry" />
        </mxCell>
        <mxCell id="37" value="两小" style="text;html=1;strokeColor=none;fillColor=#43a047;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=13;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="295" y="725" width="45" height="28" as="geometry" />
        </mxCell>
        <mxCell id="38" value="返回类型 ≤ 父类&#xa;异常 ≤ 父类" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#2e7d32;" vertex="1" parent="1">
          <mxGeometry x="350" y="718" width="130" height="40" as="geometry" />
        </mxCell>
        <mxCell id="39" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#fb8c00;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="505" y="715" width="175" height="50" as="geometry" />
        </mxCell>
        <mxCell id="40" value="一大" style="text;html=1;strokeColor=none;fillColor=#fb8c00;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=13;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="515" y="725" width="45" height="28" as="geometry" />
        </mxCell>
        <mxCell id="41" value="访问权限 ≥ 父类" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#e65100;" vertex="1" parent="1">
          <mxGeometry x="570" y="725" width="105" height="28" as="geometry" />
        </mxCell>
        <mxCell id="42" value="≤ 表示&quot;相同或更小&quot;  |  ≥ 表示&quot;相同或更大&quot;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;fontColor=#757575;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="800" width="640" height="25" as="geometry" />
        </mxCell>
        <mxCell id="43" value="✏️" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=20;" vertex="1" parent="1">
          <mxGeometry x="630" y="235" width="40" height="40" as="geometry" />
        </mxCell>
        <mxCell id="44" value="↩️" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=20;" vertex="1" parent="1">
          <mxGeometry x="630" y="295" width="40" height="40" as="geometry" />
        </mxCell>
        <mxCell id="45" value="🔓" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=20;" vertex="1" parent="1">
          <mxGeometry x="630" y="375" width="40" height="40" as="geometry" />
        </mxCell>
        <mxCell id="46" value="⚠️" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=20;" vertex="1" parent="1">
          <mxGeometry x="630" y="455" width="40" height="40" as="geometry" />
        </mxCell>
        <mxCell id="47" value="🚫" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=20;" vertex="1" parent="1">
          <mxGeometry x="630" y="525" width="40" height="40" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

重写 vs 重载 对比

对比项 重写(Override) 重载(Overload)
发生位置 子类与父类之间 同一个类中
方法名 必须相同 必须相同
参数列表 必须相同 必须不同
返回类型 相同或子类型 无关
访问修饰符 不能更严格 无关
目的 改变行为 提供多种调用方式

2.6 继承的内存结构

<mxfile>
  <diagram id="memory-model" 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="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=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="710" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#8B5CF6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="770" x="65" y="40" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#C4B5FD;fontFamily=Courier New;fontStyle=1;" value="Programmer p = new Programmer(&quot;张三&quot;, 15000, &quot;P001&quot;, &quot;Java&quot;)" vertex="1">
          <mxGeometry height="50" width="780" x="60" y="40" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="280" width="250" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="📚 栈内存 Stack" vertex="1">
          <mxGeometry height="45" width="250" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="100" width="210" x="60" y="165" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E3A5F;align=center;verticalAlign=middle;fontSize=12;fontColor=#93C5FD;rounded=1;" value="局部变量" vertex="1">
          <mxGeometry height="25" width="70" x="70" y="175" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontColor=#60A5FA;fontStyle=1;fontFamily=Courier New;" value="p" vertex="1">
          <mxGeometry height="40" width="40" x="80" y="210" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontColor=#94A3B8;" value="=" vertex="1">
          <mxGeometry height="40" width="30" x="120" y="210" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=#312E81;align=center;verticalAlign=middle;fontSize=16;fontColor=#FCD34D;fontFamily=Courier New;rounded=1;fontStyle=1;" value="0x1234" vertex="1">
          <mxGeometry height="30" width="100" x="150" y="215" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#64748B;fontStyle=2;" value="存储对象的引用地址" vertex="1">
          <mxGeometry height="25" width="210" x="60" y="290" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#60A5FA;" value="特点:存放基本类型值&#xa;和对象引用" vertex="1">
          <mxGeometry height="40" width="210" x="60" y="330" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#10B981;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="400" width="420" x="420" y="100" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="🗄️ 堆内存 Heap" vertex="1">
          <mxGeometry height="45" width="420" x="420" y="100" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#8B5CF6;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="315" width="360" x="450" y="165" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="Programmer 对象" vertex="1">
          <mxGeometry height="35" width="360" x="450" y="165" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#6D28D9;align=center;verticalAlign=middle;fontSize=11;fontColor=#E9D5FF;fontFamily=Courier New;rounded=1;" value="0x1234" vertex="1">
          <mxGeometry height="25" width="70" x="730" y="170" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="140" width="320" x="470" y="215" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="【Employee 部分】父类属性" vertex="1">
          <mxGeometry height="30" width="320" x="470" y="215" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#93C5FD;fontFamily=Courier New;" value="name" vertex="1">
          <mxGeometry height="25" width="80" x="490" y="255" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#64748B;" value="=" vertex="1">
          <mxGeometry height="25" width="30" x="570" y="255" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#0F172A;align=center;verticalAlign=middle;fontSize=14;fontColor=#FCD34D;fontFamily=Courier New;rounded=1;" value="&quot;张三&quot;" vertex="1">
          <mxGeometry height="25" width="80" x="600" y="255" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#93C5FD;fontFamily=Courier New;" value="salary" vertex="1">
          <mxGeometry height="25" width="80" x="490" y="285" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#64748B;" value="=" vertex="1">
          <mxGeometry height="25" width="30" x="570" y="285" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=#0F172A;align=center;verticalAlign=middle;fontSize=14;fontColor=#FCD34D;fontFamily=Courier New;rounded=1;" value="15000" vertex="1">
          <mxGeometry height="25" width="80" x="600" y="285" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#93C5FD;fontFamily=Courier New;" value="id" vertex="1">
          <mxGeometry height="25" width="80" x="490" y="315" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#64748B;" value="=" vertex="1">
          <mxGeometry height="25" width="30" x="570" y="315" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#0F172A;align=center;verticalAlign=middle;fontSize=14;fontColor=#FCD34D;fontFamily=Courier New;rounded=1;" value="&quot;P001&quot;" vertex="1">
          <mxGeometry height="25" width="80" x="600" y="315" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#064E3B;strokeColor=#10B981;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="90" width="320" x="470" y="370" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="【Programmer 部分】子类属性" vertex="1">
          <mxGeometry height="30" width="320" x="470" y="370" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#6EE7B7;fontFamily=Courier New;" value="language" vertex="1">
          <mxGeometry height="25" width="80" x="490" y="410" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#64748B;" value="=" vertex="1">
          <mxGeometry height="25" width="30" x="570" y="410" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#0F172A;align=center;verticalAlign=middle;fontSize=14;fontColor=#FCD34D;fontFamily=Courier New;rounded=1;" value="&quot;Java&quot;" vertex="1">
          <mxGeometry height="25" width="80" x="600" y="410" as="geometry" />
        </mxCell>
        <mxCell id="35" edge="1" parent="1" source="11" style="endArrow=classic;html=1;strokeColor=#F59E0B;strokeWidth=4;entryX=0;entryY=0.5;entryDx=0;entryDy=0;exitX=1;exitY=0.5;exitDx=0;exitDy=0;flowAnimation=1;" target="16" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="350" y="230" />
              <mxPoint x="350" y="323" />
            </Array>
            <mxPoint x="260" y="230" as="sourcePoint" />
            <mxPoint x="450" y="320" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#F59E0B;fontStyle=1;" value="引用指向" vertex="1">
          <mxGeometry height="25" width="70" x="340" y="250" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="100" width="820" x="40" y="520" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="💡" vertex="1">
          <mxGeometry height="50" width="50" x="55" y="535" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="说明" vertex="1">
          <mxGeometry height="30" width="60" x="105" y="545" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#FCD34D;" value="子类对象在内存中包含父类的所有属性(包括 private)" vertex="1">
          <mxGeometry height="25" width="450" x="180" y="535" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#FCD34D;" value="但 private 属性不能直接访问,需通过父类方法访问" vertex="1">
          <mxGeometry height="25" width="450" x="180" y="560" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;" value="🔒" vertex="1">
          <mxGeometry height="25" width="30" x="700" y="255" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;" value="🔒" vertex="1">
          <mxGeometry height="25" width="30" x="700" y="285" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=#7F1D1D;align=center;verticalAlign=middle;fontSize=10;fontColor=#FCA5A5;rounded=1;" value="private" vertex="1">
          <mxGeometry height="18" width="50" x="730" y="260" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=#7F1D1D;align=center;verticalAlign=middle;fontSize=10;fontColor=#FCA5A5;rounded=1;" value="private" vertex="1">
          <mxGeometry height="18" width="50" x="730" y="290" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#64748B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="65" width="780" x="65" y="640" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#94A3B8;" value="🎯 内存分配流程:new 时在堆中分配空间 → 先初始化父类部分 → 再初始化子类部分 → 返回地址给栈中变量" vertex="1">
          <mxGeometry height="40" width="820" x="40" y="655" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

2.7 继承的要点与限制

 <mxfile>
  <diagram id="inheritance-key-points" 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="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=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="740" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📚 继承核心要点" vertex="1">
          <mxGeometry height="60" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#10B981;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="200" width="820" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=32;" value="✅" vertex="1">
          <mxGeometry height="50" width="50" x="55" y="110" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="子类拥有什么" vertex="1">
          <mxGeometry height="35" width="150" x="105" y="120" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#064E3B;strokeColor=#059669;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="115" width="780" x="60" y="170" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontColor=#10B981;" value="•" vertex="1">
          <mxGeometry height="25" width="20" x="75" y="180" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#6EE7B7;" value="父类所有属性(包括 private,但不能直接访问)" vertex="1">
          <mxGeometry height="25" width="400" x="95" y="180" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontColor=#10B981;" value="•" vertex="1">
          <mxGeometry height="25" width="20" x="75" y="205" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#6EE7B7;" value="父类非 private 方法" vertex="1">
          <mxGeometry height="25" width="300" x="95" y="205" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontColor=#10B981;" value="•" vertex="1">
          <mxGeometry height="25" width="20" x="75" y="230" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#6EE7B7;" value="可以添加自己的属性和方法" vertex="1">
          <mxGeometry height="25" width="300" x="95" y="230" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontColor=#10B981;" value="•" vertex="1">
          <mxGeometry height="25" width="20" x="75" y="255" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#6EE7B7;" value="可以重写父类的方法" vertex="1">
          <mxGeometry height="25" width="300" x="95" y="255" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=#047857;align=center;verticalAlign=middle;fontSize=12;fontColor=#A7F3D0;rounded=1;" value="继承" vertex="1">
          <mxGeometry height="20" width="50" x="500" y="183" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#047857;align=center;verticalAlign=middle;fontSize=12;fontColor=#A7F3D0;rounded=1;" value="继承" vertex="1">
          <mxGeometry height="20" width="50" x="270" y="208" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#0369A1;align=center;verticalAlign=middle;fontSize=12;fontColor=#BAE6FD;rounded=1;" value="扩展" vertex="1">
          <mxGeometry height="20" width="50" x="320" y="233" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=#7C3AED;align=center;verticalAlign=middle;fontSize=12;fontColor=#DDD6FE;rounded=1;" value="重写" vertex="1">
          <mxGeometry height="20" width="50" x="270" y="258" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#EF4444;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="145" width="820" x="40" y="320" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=32;" value="❌" vertex="1">
          <mxGeometry height="50" width="50" x="55" y="330" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="子类不能继承什么" vertex="1">
          <mxGeometry height="35" width="180" x="105" y="340" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#450A0A;strokeColor=#B91C1C;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="60" width="780" x="60" y="390" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontColor=#EF4444;" value="•" vertex="1">
          <mxGeometry height="25" width="20" x="75" y="395" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#FCA5A5;" value="父类的构造方法(但可以通过 super() 调用)" vertex="1">
          <mxGeometry height="25" width="400" x="95" y="395" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontColor=#EF4444;" value="•" vertex="1">
          <mxGeometry height="25" width="20" x="75" y="420" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#FCA5A5;" value="父类的 private 方法" vertex="1">
          <mxGeometry height="25" width="300" x="95" y="420" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E40AF;align=center;verticalAlign=middle;fontSize=12;fontColor=#93C5FD;rounded=1;fontFamily=Courier New;" value="super()" vertex="1">
          <mxGeometry height="20" width="60" x="500" y="398" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;" value="🔒" vertex="1">
          <mxGeometry height="25" width="30" x="270" y="420" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="245" width="820" x="40" y="485" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=32;" value="⚠️" vertex="1">
          <mxGeometry height="50" width="50" x="55" y="495" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="Java 继承限制" vertex="1">
          <mxGeometry height="35" width="150" x="105" y="505" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#78350F;strokeColor=#D97706;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="165" width="780" x="60" y="552.5" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontColor=#F59E0B;" value="•" vertex="1">
          <mxGeometry height="25" width="20" x="75" y="565" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#FCD34D;" value="单继承:一个类只能 extends 一个父类" vertex="1">
          <mxGeometry height="25" width="350" x="95" y="565" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontColor=#F59E0B;" value="•" vertex="1">
          <mxGeometry height="25" width="20" x="75" y="600" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#FCD34D;" value="多实现:一个类可以 implements 多个接口" vertex="1">
          <mxGeometry height="25" width="380" x="95" y="600" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontColor=#F59E0B;" value="•" vertex="1">
          <mxGeometry height="25" width="20" x="75" y="635" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#FCD34D;" value="传递性:A extends B, B extends C → A 间接继承 C" vertex="1">
          <mxGeometry height="25" width="420" x="95" y="635" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#DC2626;strokeColor=none;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="1" vertex="1">
          <mxGeometry height="30" width="30" x="450" y="565" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#059669;strokeColor=none;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="N" vertex="1">
          <mxGeometry height="30" width="30" x="490" y="600" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="150" width="280" x="540" y="560" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#6366F1;strokeColor=none;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="C" vertex="1">
          <mxGeometry height="40" width="40" x="665" y="560" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#8B5CF6;strokeColor=none;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="B" vertex="1">
          <mxGeometry height="40" width="40" x="665" y="615" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#A855F7;strokeColor=none;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="A" vertex="1">
          <mxGeometry height="40" width="40" x="665" y="670" as="geometry" />
        </mxCell>
        <mxCell id="46" edge="1" parent="1" source="44" style="endArrow=classic;html=1;strokeColor=#A78BFA;strokeWidth=2;exitX=0.5;exitY=0;exitDx=0;exitDy=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" target="43" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="47" edge="1" parent="1" source="45" style="endArrow=classic;html=1;strokeColor=#A78BFA;strokeWidth=2;exitX=0.5;exitY=0;exitDx=0;exitDy=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" target="44" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="48" edge="1" parent="1" source="45" style="endArrow=classic;html=1;strokeColor=#F59E0B;strokeWidth=2;dashed=1;exitX=0;exitY=0;exitDx=0;exitDy=0;entryX=1;entryY=1;entryDx=0;entryDy=0;" target="43" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="600" y="665" />
              <mxPoint x="600" y="605" />
            </Array>
          </mxGeometry>
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#F59E0B;fontStyle=2;" value="间接继承" vertex="1">
          <mxGeometry height="20" width="60" x="555" y="620" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#A78BFA;fontFamily=Courier New;" value="extends" vertex="1">
          <mxGeometry height="15" width="50" x="710" y="595" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#A78BFA;fontFamily=Courier New;" value="extends" vertex="1">
          <mxGeometry height="15" width="50" x="710" y="640" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

三、多态(Polymorphism)

3.1 概念定义

 <mxfile>
  <diagram id="polymorphism-essence-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="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=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="730" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🎭 多态的本质" vertex="1">
          <mxGeometry height="60" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="50" width="820" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontColor=#FCD34D;fontStyle=1;" value="💡 多态 = 同一个行为(方法调用)具有多种不同的表现形式" vertex="1">
          <mxGeometry height="50" width="820" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#6366F1;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="360" width="820" x="40" y="170" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="代码演示" vertex="1">
          <mxGeometry height="35" width="100" x="40" y="170" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="200" x="60" y="220" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#C4B5FD;fontFamily=Courier New;" value="Animal animal;" vertex="1">
          <mxGeometry height="45" width="200" x="60" y="220" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=#312E81;align=center;verticalAlign=middle;fontSize=11;fontColor=#A5B4FC;rounded=1;" value="声明父类类型变量" vertex="1">
          <mxGeometry height="25" width="110" x="270" y="230" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#F97316;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="200" x="60" y="285" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontColor=#FDBA74;fontFamily=Courier New;" value="animal = new Dog();" vertex="1">
          <mxGeometry height="45" width="200" x="60" y="285" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="200" x="280" y="285" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontColor=#93C5FD;fontFamily=Courier New;" value="animal.makeSound();" vertex="1">
          <mxGeometry height="45" width="200" x="280" y="285" as="geometry" />
        </mxCell>
        <mxCell id="15" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#F97316;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="490" y="307" as="sourcePoint" />
            <mxPoint x="580" y="307" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=#7C2D12;align=center;verticalAlign=middle;fontSize=18;fontColor=#FDBA74;rounded=1;fontStyle=1;" value="🐕 &quot;汪汪汪&quot;" vertex="1">
          <mxGeometry height="45" width="150" x="590" y="285" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="200" x="60" y="350" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontColor=#86EFAC;fontFamily=Courier New;" value="animal = new Cat();" vertex="1">
          <mxGeometry height="45" width="200" x="60" y="350" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="200" x="280" y="350" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontColor=#93C5FD;fontFamily=Courier New;" value="animal.makeSound();" vertex="1">
          <mxGeometry height="45" width="200" x="280" y="350" as="geometry" />
        </mxCell>
        <mxCell id="21" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#22C55E;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="490" y="372" as="sourcePoint" />
            <mxPoint x="580" y="372" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#14532D;align=center;verticalAlign=middle;fontSize=18;fontColor=#86EFAC;rounded=1;fontStyle=1;" value="🐱 &quot;喵喵喵&quot;" vertex="1">
          <mxGeometry height="45" width="150" x="590" y="350" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="200" x="60" y="415" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontColor=#93C5FD;fontFamily=Courier New;" value="animal = new Cow();" vertex="1">
          <mxGeometry height="45" width="200" x="60" y="415" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="200" x="280" y="415" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontColor=#93C5FD;fontFamily=Courier New;" value="animal.makeSound();" vertex="1">
          <mxGeometry height="45" width="200" x="280" y="415" as="geometry" />
        </mxCell>
        <mxCell id="27" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#3B82F6;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="490" y="437" as="sourcePoint" />
            <mxPoint x="580" y="437" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E3A8A;align=center;verticalAlign=middle;fontSize=18;fontColor=#93C5FD;rounded=1;fontStyle=1;" value="🐄 &quot;哞哞哞&quot;" vertex="1">
          <mxGeometry height="45" width="150" x="590" y="415" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7C3AED;strokeColor=#A78BFA;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="35" width="780" x="60" y="480" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="✨ 同样的方法调用,不同的执行结果 → 这就是多态!" vertex="1">
          <mxGeometry height="35" width="780" x="60" y="480" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E40AF;align=center;verticalAlign=middle;fontSize=11;fontColor=#93C5FD;rounded=1;" value="相同方法" vertex="1">
          <mxGeometry height="25" width="70" x="760" y="285" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E40AF;align=center;verticalAlign=middle;fontSize=11;fontColor=#93C5FD;rounded=1;" value="相同方法" vertex="1">
          <mxGeometry height="25" width="70" x="760" y="350" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E40AF;align=center;verticalAlign=middle;fontSize=11;fontColor=#93C5FD;rounded=1;" value="相同方法" vertex="1">
          <mxGeometry height="25" width="70" x="760" y="415" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="shape=curlyBracket;whiteSpace=wrap;html=1;rounded=1;flipH=1;labelPosition=right;verticalLabelPosition=middle;align=left;verticalAlign=middle;strokeColor=#F59E0B;strokeWidth=3;size=0.5;" value="" vertex="1">
          <mxGeometry height="175" width="20" x="750" y="285" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=#78350F;align=center;verticalAlign=middle;fontSize=12;fontColor=#FCD34D;rounded=1;rotation=-90;" value="不同结果" vertex="1">
          <mxGeometry height="25" width="70" x="810" y="350" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#10B981;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="160" width="820" x="40" y="550" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="🌍 生活类比" vertex="1">
          <mxGeometry height="35" width="120" x="40" y="550" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontColor=#6EE7B7;fontStyle=1;" value="同样是&quot;打开&quot;这个动作:" vertex="1">
          <mxGeometry height="30" width="250" x="60" y="595" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#064E3B;strokeColor=#059669;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="240" x="60" y="630" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=36;" value="🚪" vertex="1">
          <mxGeometry height="50" width="50" x="70" y="640" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#A7F3D0;fontStyle=1;" value="打开门" vertex="1">
          <mxGeometry height="25" width="70" x="120" y="640" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#6EE7B7;" value="→ 推/拉" vertex="1">
          <mxGeometry height="25" width="100" x="190" y="640" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#064E3B;strokeColor=#059669;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="240" x="320" y="630" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=36;" value="📺" vertex="1">
          <mxGeometry height="50" width="50" x="330" y="640" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#A7F3D0;fontStyle=1;" value="打开电视" vertex="1">
          <mxGeometry height="25" width="80" x="380" y="640" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#6EE7B7;" value="→ 按遥控器" vertex="1">
          <mxGeometry height="25" width="100" x="455" y="640" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#064E3B;strokeColor=#059669;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="260" x="580" y="630" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=36;" value="🚿" vertex="1">
          <mxGeometry height="50" width="50" x="590" y="640" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#A7F3D0;fontStyle=1;" value="打开水龙头" vertex="1">
          <mxGeometry height="25" width="90" x="640" y="640" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#6EE7B7;" value="→ 旋转" vertex="1">
          <mxGeometry height="25" width="100" x="730" y="640" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=#7C3AED;align=center;verticalAlign=middle;fontSize=12;fontColor=#E9D5FF;fontFamily=Courier New;rounded=1;" value="open()" vertex="1">
          <mxGeometry height="22" width="60" x="130" y="670" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="text;html=1;strokeColor=none;fillColor=#7C3AED;align=center;verticalAlign=middle;fontSize=12;fontColor=#E9D5FF;fontFamily=Courier New;rounded=1;" value="open()" vertex="1">
          <mxGeometry height="22" width="60" x="390" y="670" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="text;html=1;strokeColor=none;fillColor=#7C3AED;align=center;verticalAlign=middle;fontSize=12;fontColor=#E9D5FF;fontFamily=Courier New;rounded=1;" value="open()" vertex="1">
          <mxGeometry height="22" width="60" x="660" y="670" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

3.2 多态的三个必要条件

 <mxfile>
  <diagram id="polymorphism-three-elements" name="多态三要素">
    <mxGraphModel dx="1665" dy="734" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="700" 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=#424242;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="640" width="640" x="20" y="30" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#7c4dff;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=24;fontStyle=1;fontColor=#ffffff;" value="多态三要素" vertex="1">
          <mxGeometry height="60" width="640" x="30" y="30" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="triangle;whiteSpace=wrap;html=1;fillColor=#ede7f6;strokeColor=#7c4dff;strokeWidth=2;rotation=0;" value="" vertex="1">
          <mxGeometry height="90" width="100" x="310" 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;fontSize=16;fontStyle=1;fontColor=#7c4dff;" value="多态" vertex="1">
          <mxGeometry height="30" width="70" x="310" y="140" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="110" width="580" x="60" y="220" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;fontColor=#ffffff;fontSize=20;fontStyle=1;" value="1" vertex="1">
          <mxGeometry height="45" width="45" x="80" y="240" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;fontColor=#1565c0;" value="继承关系" vertex="1">
          <mxGeometry height="35" width="120" x="135" y="245" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;fontSize=14;fontFamily=Consolas;align=center;" value="class Dog extends Animal { }" vertex="1">
          <mxGeometry height="35" width="280" x="135" y="285" as="geometry" />
        </mxCell>
        <mxCell id="10" 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="60" width="60" x="560" y="245" as="geometry" />
        </mxCell>
        <mxCell id="11" edge="1" parent="1" source="4" style="endArrow=classic;html=1;rounded=0;strokeWidth=3;strokeColor=#7c4dff;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.39;exitY=0.764;exitDx=0;exitDy=0;exitPerimeter=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="350" y="205" as="sourcePoint" />
            <mxPoint x="350" y="220" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#43a047;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="110" width="580" x="60" y="350" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#43a047;strokeColor=none;fontColor=#ffffff;fontSize=20;fontStyle=1;" value="2" vertex="1">
          <mxGeometry height="45" width="45" x="80" y="370" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;fontColor=#2e7d32;" value="方法重写" vertex="1">
          <mxGeometry height="35" width="120" x="135" y="375" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#a5d6a7;fontSize=13;fontFamily=Consolas;align=center;fontColor=#43a047;" value="@Override" vertex="1">
          <mxGeometry height="30" width="100" x="135" y="410" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#a5d6a7;fontSize=13;fontFamily=Consolas;align=center;" value="public void makeSound() { ... }" vertex="1">
          <mxGeometry height="30" width="270" x="245" y="410" 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=32;" value="✏️" vertex="1">
          <mxGeometry height="60" width="60" x="560" y="375" as="geometry" />
        </mxCell>
        <mxCell id="18" edge="1" parent="1" style="endArrow=classic;html=1;rounded=0;strokeWidth=3;strokeColor=#7c4dff;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="350" y="330" as="sourcePoint" />
            <mxPoint x="350" y="350" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#fb8c00;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="130" width="580" x="60" y="480" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#fb8c00;strokeColor=none;fontColor=#ffffff;fontSize=20;fontStyle=1;" value="3" vertex="1">
          <mxGeometry height="45" width="45" x="80" y="500" 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=18;fontStyle=1;fontColor=#e65100;" value="父类引用指向子类对象(向上转型)" vertex="1">
          <mxGeometry height="35" width="350" x="135" y="505" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;fontSize=14;fontFamily=Consolas;align=center;fontColor=#ffffff;fontStyle=1;" value="Animal" vertex="1">
          <mxGeometry height="30" width="80" x="135" y="545" 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=14;fontFamily=Consolas;" value="animal = new" vertex="1">
          <mxGeometry height="30" width="100" x="220" y="545" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#43a047;strokeColor=none;fontSize=14;fontFamily=Consolas;align=center;fontColor=#ffffff;fontStyle=1;" value="Dog()" vertex="1">
          <mxGeometry height="30" width="70" x="325" y="545" 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;fontFamily=Consolas;" value=";" vertex="1">
          <mxGeometry height="30" width="20" x="395" y="545" as="geometry" />
        </mxCell>
        <mxCell id="26" edge="1" parent="1" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#1976d2;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="175" y="595" as="sourcePoint" />
            <mxPoint x="175" y="575" as="targetPoint" />
          </mxGeometry>
        </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=#1976d2;fontStyle=1;" value="父类类型" vertex="1">
          <mxGeometry height="20" width="70" x="140" y="595" as="geometry" />
        </mxCell>
        <mxCell id="28" edge="1" parent="1" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#43a047;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="360" y="595" as="sourcePoint" />
            <mxPoint x="360" y="575" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#43a047;fontStyle=1;" value="子类对象" vertex="1">
          <mxGeometry height="20" width="70" x="325" y="595" 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=32;" value="⬆️" vertex="1">
          <mxGeometry height="60" width="60" x="560" y="505" as="geometry" />
        </mxCell>
        <mxCell id="31" edge="1" parent="1" style="endArrow=classic;html=1;rounded=0;strokeWidth=3;strokeColor=#7c4dff;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="350" y="460" as="sourcePoint" />
            <mxPoint x="350" y="480" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#ff5252;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=18;fontStyle=1;fontColor=#ffffff;" value="⚠️ 三者缺一不可!" vertex="1">
          <mxGeometry height="40" width="240" x="230" y="625" as="geometry" />
        </mxCell>
        <mxCell id="33" edge="1" parent="1" style="endArrow=none;html=1;rounded=0;strokeWidth=2;strokeColor=#7c4dff;dashed=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="300" y="160" as="sourcePoint" />
            <mxPoint x="175" y="220" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="34" edge="1" parent="1" style="endArrow=none;html=1;rounded=0;strokeWidth=2;strokeColor=#7c4dff;dashed=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="400" y="160" as="sourcePoint" />
            <mxPoint x="525" y="220" as="targetPoint" />
          </mxGeometry>
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

3.3 完整代码示例

定义类结构

/**
 * 动物类(父类)
 */
public class Animal {
    protected String name;
    
    public Animal(String name) {
        this.name = name;
    }
    
    public void makeSound() {
        System.out.println(name + " 发出声音");
    }
    
    public void eat() {
        System.out.println(name + " 正在吃东西");
    }
}

/**
 * 狗类(子类)
 */
public class Dog extends Animal {
    
    public Dog(String name) {
        super(name);
    }
    
    @Override
    public void makeSound() {
        System.out.println(name + " 汪汪汪!");
    }
    
    // 子类特有方法
    public void guardHouse() {
        System.out.println(name + " 正在看家");
    }
}

/**
 * 猫类(子类)
 */
public class Cat extends Animal {
    
    public Cat(String name) {
        super(name);
    }
    
    @Override
    public void makeSound() {
        System.out.println(name + " 喵喵喵~");
    }
    
    // 子类特有方法
    public void catchMouse() {
        System.out.println(name + " 正在抓老鼠");
    }
}

/**
 * 鸟类(子类)
 */
public class Bird extends Animal {
    
    public Bird(String name) {
        super(name);
    }
    
    @Override
    public void makeSound() {
        System.out.println(name + " 叽叽喳喳!");
    }
    
    // 子类特有方法
    public void fly() {
        System.out.println(name + " 正在飞翔");
    }
}

多态使用示例

public class PolymorphismDemo {
    public static void main(String[] args) {
        
        // ========== 向上转型:父类引用指向子类对象 ==========
        Animal animal1 = new Dog("旺财");
        Animal animal2 = new Cat("小花");
        Animal animal3 = new Bird("小鸟");
        
        // 同样的方法调用,不同的执行结果
        animal1.makeSound();  // 旺财 汪汪汪!
        animal2.makeSound();  // 小花 喵喵喵~
        animal3.makeSound();  // 小鸟 叽叽喳喳!
        
        // ========== 多态数组 ==========
        Animal[] animals = {
            new Dog("大黄"),
            new Cat("咪咪"),
            new Bird("鹦鹉")
        };
        
        // 统一处理
        for (Animal animal : animals) {
            animal.makeSound();  // 自动调用各自的实现
        }
        
        // ========== 多态参数 ==========
        feedAnimal(new Dog("小白"));
        feedAnimal(new Cat("小黑"));
    }
    
    // 多态作为方法参数
    public static void feedAnimal(Animal animal) {
        animal.eat();        // 所有动物都能吃
        animal.makeSound();  // 各自发出不同的声音
    }
}

3.4 多态的执行原理

<mxfile>
  <diagram id="polymorphism-method-call" 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="800" 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=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="790" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="⚙️ 多态方法调用原理" vertex="1">
          <mxGeometry height="55" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#6366F1;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="820" x="40" y="90" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=13;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="💻 代码示例" vertex="1">
          <mxGeometry height="28" width="100" x="40" y="90" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#0F172A;align=center;verticalAlign=middle;fontSize=15;fontColor=#C4B5FD;fontFamily=Courier New;rounded=1;" value="Animal animal = new Dog(&quot;旺财&quot;);" vertex="1">
          <mxGeometry height="28" width="350" x="110" y="125" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=#0F172A;align=center;verticalAlign=middle;fontSize=15;fontColor=#93C5FD;fontFamily=Courier New;rounded=1;" value="animal.makeSound();" vertex="1">
          <mxGeometry height="28" width="220" x="535" y="125" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="270" width="820" x="40" y="180" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="📘 编译时" vertex="1">
          <mxGeometry height="40" width="120" x="40" y="180" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E40AF;align=center;verticalAlign=middle;fontSize=14;fontColor=#93C5FD;rounded=1;fontStyle=1;" value="编译器检查" vertex="1">
          <mxGeometry height="35" width="100" x="100" y="235" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=#0F172A;align=center;verticalAlign=middle;fontSize=15;fontColor=#BFDBFE;rounded=1;strokeColor=#1E40AF;strokeWidth=2;" value="Animal 类是否有 makeSound() 方法?" vertex="1">
          <mxGeometry height="45" width="340" x="220" y="230" as="geometry" />
        </mxCell>
        <mxCell id="12" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#60A5FA;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="390" y="280" as="sourcePoint" />
            <mxPoint x="390" y="310" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="13" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#059669;strokeColor=#10B981;strokeWidth=2;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="✅ 有" vertex="1">
          <mxGeometry height="45" width="80" x="350" y="315" as="geometry" />
        </mxCell>
        <mxCell id="14" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#60A5FA;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="390" y="365" as="sourcePoint" />
            <mxPoint x="390" y="395" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#22C55E;strokeWidth=2;fontSize=16;fontColor=#86EFAC;fontStyle=1;" value="编译通过 ✓" vertex="1">
          <mxGeometry height="40" width="140" x="320" y="396" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#172554;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="75" width="320" x="510" y="300" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=24;" value="⚠️" vertex="1">
          <mxGeometry height="40" width="40" x="520" y="310" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#93C5FD;fontStyle=1;" value="编译时只看引用类型" vertex="1">
          <mxGeometry height="25" width="160" x="560" y="315" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=#2563EB;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontFamily=Courier New;rounded=1;fontStyle=1;" value="Animal" vertex="1">
          <mxGeometry height="25" width="80" x="720" y="315" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#60A5FA;" value="不看实际对象类型" vertex="1">
          <mxGeometry height="25" width="150" x="560" y="343" as="geometry" />
        </mxCell>
        <mxCell id="21" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#F59E0B;strokeWidth=4;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="450" y="460" as="sourcePoint" />
            <mxPoint x="450" y="500" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#22C55E;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="270" width="820" x="40" y="510" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="🚀 运行时" vertex="1">
          <mxGeometry height="40" width="120" x="40" y="510" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#14532D;align=center;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;rounded=1;fontStyle=1;" value="JVM 查看" vertex="1">
          <mxGeometry height="35" width="90" x="100" y="565" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#0F172A;align=center;verticalAlign=middle;fontSize=15;fontColor=#BBF7D0;rounded=1;strokeColor=#14532D;strokeWidth=2;" value="animal 实际指向什么对象?" vertex="1">
          <mxGeometry height="45" width="260" x="210" y="560" as="geometry" />
        </mxCell>
        <mxCell id="26" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#4ADE80;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="340" y="610" as="sourcePoint" />
            <mxPoint x="340" y="640" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="27" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F97316;strokeColor=#FB923C;strokeWidth=2;fontSize=15;fontColor=#FFFFFF;fontStyle=1;" value="🐕 Dog 对象" vertex="1">
          <mxGeometry height="50" width="130" x="275" y="645" as="geometry" />
        </mxCell>
        <mxCell id="28" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#4ADE80;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="340" y="700" as="sourcePoint" />
            <mxPoint x="340" y="730" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#0F172A;align=center;verticalAlign=middle;fontSize=14;fontColor=#FCD34D;rounded=1;strokeColor=#78350F;strokeWidth=2;" value="调用 Dog 类的 makeSound() 方法" vertex="1">
          <mxGeometry height="35" width="280" x="170" y="735" as="geometry" />
        </mxCell>
        <mxCell id="30" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#4ADE80;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="460" y="752" as="sourcePoint" />
            <mxPoint x="520" y="752" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7C2D12;strokeColor=#F97316;strokeWidth=2;fontSize=16;fontColor=#FED7AA;fontStyle=1;" value="🔊 &quot;旺财 汪汪汪!&quot;" vertex="1">
          <mxGeometry height="45" width="180" x="530" y="730" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#052E16;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="75" width="320" x="520" y="565" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=24;" value="⚠️" vertex="1">
          <mxGeometry height="40" width="40" x="530" y="575" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;fontStyle=1;" value="运行时看实际对象类型" vertex="1">
          <mxGeometry height="25" width="180" x="570" y="580" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=#EA580C;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontFamily=Courier New;rounded=1;fontStyle=1;" value="Dog" vertex="1">
          <mxGeometry height="25" width="60" x="750" y="580" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#4ADE80;" value="动态绑定机制" vertex="1">
          <mxGeometry height="25" width="150" x="570" y="608" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#2563EB;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="左边" vertex="1">
          <mxGeometry height="28" width="45" x="60" y="125" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=#EA580C;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="右边" vertex="1">
          <mxGeometry height="28" width="45" x="485" y="125" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

3.5 向上转型与向下转型

<mxfile>
  <diagram id="type-casting" 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="720" pageHeight="880" 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=#424242;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="30" y="30" width="660" height="820" as="geometry" />
        </mxCell>
        
        <!-- 标题栏 -->
        <mxCell id="3" value="类型转换" style="text;html=1;strokeColor=none;fillColor=#5c6bc0;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=24;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="30" y="30" width="660" height="60" as="geometry" />
        </mxCell>
        
        <!-- 向上转型容器 -->
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#43a047;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="50" y="110" width="620" height="330" as="geometry" />
        </mxCell>
        
        <!-- 向上转型标题 -->
        <mxCell id="5" value="向上转型" style="text;html=1;strokeColor=none;fillColor=#43a047;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=20;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="50" y="110" width="620" height="45" as="geometry" />
        </mxCell>
        <mxCell id="6" value="(Upcasting)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#2e7d32;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="50" y="155" width="620" height="25" as="geometry" />
        </mxCell>
        
        <!-- 向上箭头图标 -->
        <mxCell id="7" value="⬆️" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=36;" vertex="1" parent="1">
          <mxGeometry x="600" y="115" width="50" height="50" as="geometry" />
        </mxCell>
        
        <!-- 向上转型代码示例 -->
        <mxCell id="8" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#a5d6a7;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="70" y="190" width="580" height="90" as="geometry" />
        </mxCell>
        <mxCell id="9" value="Animal" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;fontSize=14;fontFamily=Consolas;fontColor=#ffffff;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="90" y="205" width="70" height="28" as="geometry" />
        </mxCell>
        <mxCell id="10" value="animal = new" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontFamily=Consolas;" vertex="1" parent="1">
          <mxGeometry x="165" y="205" width="110" height="28" as="geometry" />
        </mxCell>
        <mxCell id="11" value="Dog(&quot;旺财&quot;)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#43a047;strokeColor=none;fontSize=14;fontFamily=Consolas;fontColor=#ffffff;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="275" y="205" width="110" height="28" as="geometry" />
        </mxCell>
        <mxCell id="12" value=";" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontFamily=Consolas;" vertex="1" parent="1">
          <mxGeometry x="385" y="205" width="20" height="28" as="geometry" />
        </mxCell>
        <mxCell id="13" value="// 自动转型" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontFamily=Consolas;fontColor=#757575;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="410" y="205" width="100" height="28" as="geometry" />
        </mxCell>
        
        <!-- 标注箭头 -->
        <mxCell id="14" value="" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#1976d2;" edge="1" parent="1">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="125" y="255" as="sourcePoint" />
            <mxPoint x="125" y="235" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="15" value="父类型" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#1976d2;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="255" width="50" height="20" as="geometry" />
        </mxCell>
        <mxCell id="16" value="" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#43a047;" edge="1" parent="1">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="330" y="255" as="sourcePoint" />
            <mxPoint x="330" y="235" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="17" value="子类对象" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#43a047;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="300" y="255" width="60" height="20" as="geometry" />
        </mxCell>
        
        <!-- 向上转型特点 -->
        <mxCell id="18" value="特点:" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontStyle=1;fontColor=#2e7d32;" vertex="1" parent="1">
          <mxGeometry x="70" y="290" width="60" height="30" as="geometry" />
        </mxCell>
        <mxCell id="19" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="70" y="320" width="580" height="110" as="geometry" />
        </mxCell>
        <mxCell id="20" value="✓" style="text;html=1;strokeColor=none;fillColor=#43a047;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontColor=#ffffff;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="85" y="330" width="25" height="25" as="geometry" />
        </mxCell>
        <mxCell id="21" value="自动进行,无需强制转换" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#1b5e20;" vertex="1" parent="1">
          <mxGeometry x="120" y="330" width="200" height="25" as="geometry" />
        </mxCell>
        <mxCell id="22" value="✓" style="text;html=1;strokeColor=none;fillColor=#43a047;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontColor=#ffffff;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="85" y="360" width="25" height="25" as="geometry" />
        </mxCell>
        <mxCell id="23" value="安全,不会失败" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#1b5e20;" vertex="1" parent="1">
          <mxGeometry x="120" y="360" width="150" height="25" as="geometry" />
        </mxCell>
        <mxCell id="24" value="✓" style="text;html=1;strokeColor=none;fillColor=#43a047;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontColor=#ffffff;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="85" y="390" width="25" height="25" as="geometry" />
        </mxCell>
        <mxCell id="25" value="只能访问父类成员,不能访问子类特有成员" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#1b5e20;" vertex="1" parent="1">
          <mxGeometry x="120" y="390" width="350" height="25" as="geometry" />
        </mxCell>
        
        <!-- 向下转型容器 -->
        <mxCell id="26" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#fb8c00;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="50" y="460" width="620" height="370" as="geometry" />
        </mxCell>
        
        <!-- 向下转型标题 -->
        <mxCell id="27" value="向下转型" style="text;html=1;strokeColor=none;fillColor=#fb8c00;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=20;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="50" y="460" width="620" height="45" as="geometry" />
        </mxCell>
        <mxCell id="28" value="(Downcasting)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#e65100;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="50" y="505" width="620" height="25" as="geometry" />
        </mxCell>
        
        <!-- 向下箭头图标 -->
        <mxCell id="29" value="⬇️" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=36;" vertex="1" parent="1">
          <mxGeometry x="600" y="465" width="50" height="50" as="geometry" />
        </mxCell>
        
        <!-- 向下转型代码示例 -->
        <mxCell id="30" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#ffcc80;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="70" y="540" width="580" height="120" as="geometry" />
        </mxCell>
        
        <!-- 第一行代码 -->
        <mxCell id="31" value="Animal animal = new Dog(&quot;旺财&quot;);" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontFamily=Consolas;" vertex="1" parent="1">
          <mxGeometry x="90" y="555" width="300" height="25" as="geometry" />
        </mxCell>
        
        <!-- 第二行代码 -->
        <mxCell id="32" value="Dog dog =" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontFamily=Consolas;" vertex="1" parent="1">
          <mxGeometry x="90" y="585" width="80" height="25" as="geometry" />
        </mxCell>
        <mxCell id="33" value="(Dog)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e91e63;strokeColor=none;fontSize=14;fontFamily=Consolas;fontColor=#ffffff;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="170" y="585" width="55" height="25" as="geometry" />
        </mxCell>
        <mxCell id="34" value="animal;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontFamily=Consolas;" vertex="1" parent="1">
          <mxGeometry x="230" y="585" width="70" height="25" as="geometry" />
        </mxCell>
        <mxCell id="35" value="// 强制转换" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontFamily=Consolas;fontColor=#757575;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="310" y="585" width="100" height="25" as="geometry" />
        </mxCell>
        
        <!-- 标注箭头 -->
        <mxCell id="36" value="" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#e91e63;" edge="1" parent="1">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="197" y="635" as="sourcePoint" />
            <mxPoint x="197" y="612" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="37" value="必须显式转换" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#e91e63;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="155" y="635" width="90" height="20" as="geometry" />
        </mxCell>
        
        <!-- 向下转型特点 -->
        <mxCell id="38" value="特点:" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontStyle=1;fontColor=#e65100;" vertex="1" parent="1">
          <mxGeometry x="70" y="670" width="60" height="30" as="geometry" />
        </mxCell>
        
        <mxCell id="39" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffe0b2;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="70" y="700" width="580" height="120" as="geometry" />
        </mxCell>
        <mxCell id="40" value="!" style="text;html=1;strokeColor=none;fillColor=#fb8c00;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontColor=#ffffff;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="85" y="710" width="25" height="25" as="geometry" />
        </mxCell>
        <mxCell id="41" value="需要显式强制转换" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#e65100;" vertex="1" parent="1">
          <mxGeometry x="120" y="710" width="180" height="25" as="geometry" />
        </mxCell>
        <mxCell id="42" value="⚠" style="text;html=1;strokeColor=none;fillColor=#f44336;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontColor=#ffffff;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="85" y="740" width="25" height="25" as="geometry" />
        </mxCell>
        <mxCell id="43" value="可能失败,抛出" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#e65100;" vertex="1" parent="1">
          <mxGeometry x="120" y="740" width="120" height="25" as="geometry" />
        </mxCell>
        <mxCell id="44" value="ClassCastException" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f44336;strokeColor=none;fontSize=12;fontFamily=Consolas;fontColor=#ffffff;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="240" y="742" width="150" height="22" as="geometry" />
        </mxCell>
        <mxCell id="45" value="✓" style="text;html=1;strokeColor=none;fillColor=#43a047;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontColor=#ffffff;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="85" y="775" width="25" height="25" as="geometry" />
        </mxCell>
        <mxCell id="46" value="转换后可以访问子类特有成员" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#e65100;" vertex="1" parent="1">
          <mxGeometry x="120" y="775" width="250" height="25" as="geometry" />
        </mxCell>
        
        <!-- 中间对比箭头 -->
        <mxCell id="47" value="" style="shape=flexArrow;endArrow=classic;startArrow=classic;html=1;rounded=0;fillColor=#7c4dff;strokeColor=none;width=15;endSize=5;startSize=5;" edge="1" parent="1">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="350" y="440" as="sourcePoint" />
            <mxPoint x="350" y="460" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        </root>
    </mxGraphModel>
  </diagram>
</mxfile>

代码示例

public class CastDemo {
    public static void main(String[] args) {
        // ===== 向上转型 =====
        Animal animal = new Dog("旺财");  // 自动向上转型
        animal.makeSound();    // ✅ 可以调用
        animal.eat();          // ✅ 可以调用
        // animal.guardHouse(); // ❌ 编译错误!父类引用看不到子类特有方法
        
        // ===== 向下转型 =====
        if (animal instanceof Dog) {  // 先判断类型
            Dog dog = (Dog) animal;   // 强制向下转型
            dog.guardHouse();         // ✅ 现在可以调用子类方法
        }
        
        // ===== 错误的向下转型 =====
        Animal animal2 = new Cat("小花");
        // Dog dog2 = (Dog) animal2;  // ❌ 运行时异常:ClassCastException
        
        // ===== 安全的向下转型 =====
        if (animal2 instanceof Dog) {
            Dog dog2 = (Dog) animal2;
            dog2.guardHouse();
        } else {
            System.out.println("animal2 不是 Dog 类型");
        }
        
        // ===== Java 16+ 新语法:模式匹配 =====
        if (animal instanceof Dog dog) {  // 判断 + 转换一步完成
            dog.guardHouse();
        }
    }
}

3.6 instanceof 关键字

<mxfile>
  <diagram id="instanceof-usage" name="instanceof 用法">
    <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="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="680" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔍 instanceof 用法" vertex="1">
          <mxGeometry height="55" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#6366F1;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="90" width="820" x="40" y="90" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="📝 语法说明" vertex="1">
          <mxGeometry height="30" width="110" x="40" y="90" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#312E81;align=center;verticalAlign=middle;fontSize=13;fontColor=#A5B4FC;rounded=1;fontStyle=1;" value="语法" vertex="1">
          <mxGeometry height="28" width="50" x="60" y="130" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=#0F172A;align=center;verticalAlign=middle;fontSize=16;fontColor=#C4B5FD;fontFamily=Courier New;rounded=1;strokeColor=#4C1D95;strokeWidth=1;" value="对象 instanceof 类型" vertex="1">
          <mxGeometry height="32" width="250" x="120" y="128" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#312E81;align=center;verticalAlign=middle;fontSize=13;fontColor=#A5B4FC;rounded=1;fontStyle=1;" value="返回" vertex="1">
          <mxGeometry height="28" width="50" x="400" y="130" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=#7C3AED;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontFamily=Courier New;rounded=1;fontStyle=1;" value="boolean" vertex="1">
          <mxGeometry height="28" width="90" x="460" y="130" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=#059669;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="true" vertex="1">
          <mxGeometry height="28" width="50" x="560" y="130" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#64748B;" value="/" vertex="1">
          <mxGeometry height="28" width="20" x="610" y="130" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#DC2626;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="false" vertex="1">
          <mxGeometry height="28" width="50" x="630" y="130" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="340" width="820" x="40" y="200" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="💻 示例演示" vertex="1">
          <mxGeometry height="30" width="110" x="40" y="200" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#78350F;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="380" x="60" y="245" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontColor=#FCD34D;fontFamily=Courier New;" value="Animal animal = new Dog(&quot;旺财&quot;);" vertex="1">
          <mxGeometry height="45" width="380" x="60" y="245" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="🐕" vertex="1">
          <mxGeometry height="40" width="50" x="450" y="250" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#052E16;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="280" x="60" y="305" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontColor=#86EFAC;fontFamily=Courier New;" value="animal instanceof Dog" vertex="1">
          <mxGeometry height="45" width="280" x="60" y="305" as="geometry" />
        </mxCell>
        <mxCell id="20" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#22C55E;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="350" y="327" as="sourcePoint" />
            <mxPoint x="420" y="327" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=#059669;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="true ✅" vertex="1">
          <mxGeometry height="35" width="80" x="430" y="310" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#052E16;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="280" x="60" y="365" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontColor=#86EFAC;fontFamily=Courier New;" value="animal instanceof Animal" vertex="1">
          <mxGeometry height="45" width="280" x="60" y="365" as="geometry" />
        </mxCell>
        <mxCell id="24" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#22C55E;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="350" y="387" as="sourcePoint" />
            <mxPoint x="420" y="387" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#059669;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="true ✅" vertex="1">
          <mxGeometry height="35" width="80" x="430" y="370" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=#14532D;align=center;verticalAlign=middle;fontSize=12;fontColor=#86EFAC;rounded=1;" value="Dog 是 Animal 的子类" vertex="1">
          <mxGeometry height="25" width="150" x="520" y="375" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#450A0A;strokeColor=#DC2626;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="280" x="60" y="425" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontColor=#FCA5A5;fontFamily=Courier New;" value="animal instanceof Cat" vertex="1">
          <mxGeometry height="45" width="280" x="60" y="425" as="geometry" />
        </mxCell>
        <mxCell id="29" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#DC2626;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="350" y="447" as="sourcePoint" />
            <mxPoint x="420" y="447" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=#DC2626;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="false ❌" vertex="1">
          <mxGeometry height="35" width="80" x="430" y="430" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#7F1D1D;align=center;verticalAlign=middle;fontSize=12;fontColor=#FCA5A5;rounded=1;" value="Dog 不是 Cat" vertex="1">
          <mxGeometry height="25" width="100" x="520" y="435" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#052E16;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="280" x="60" y="485" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontColor=#86EFAC;fontFamily=Courier New;" value="animal instanceof Object" vertex="1">
          <mxGeometry height="45" width="280" x="60" y="485" as="geometry" />
        </mxCell>
        <mxCell id="34" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#22C55E;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="350" y="507" as="sourcePoint" />
            <mxPoint x="420" y="507" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=#059669;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="true ✅" vertex="1">
          <mxGeometry height="35" width="80" x="430" y="490" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#14532D;align=center;verticalAlign=middle;fontSize=12;fontColor=#86EFAC;rounded=1;" value="所有类都继承 Object" vertex="1">
          <mxGeometry height="25" width="140" x="520" y="495" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#172554;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="225" width="160" x="680" y="305" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="继承关系" vertex="1">
          <mxGeometry height="25" width="80" x="680" y="305" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E40AF;strokeColor=#60A5FA;strokeWidth=2;fontSize=13;fontColor=#BFDBFE;fontStyle=1;" value="Object" vertex="1">
          <mxGeometry height="35" width="80" x="720" y="340" as="geometry" />
        </mxCell>
        <mxCell id="40" edge="1" parent="1" style="endArrow=block;html=1;strokeColor=#60A5FA;strokeWidth=2;endFill=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="760" y="415" as="sourcePoint" />
            <mxPoint x="760" y="380" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="41" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E40AF;strokeColor=#60A5FA;strokeWidth=2;fontSize=13;fontColor=#BFDBFE;fontStyle=1;" value="Animal" vertex="1">
          <mxGeometry height="35" width="80" x="720" y="420" as="geometry" />
        </mxCell>
        <mxCell id="42" edge="1" parent="1" style="endArrow=block;html=1;strokeColor=#F97316;strokeWidth=2;endFill=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="730" y="490" as="sourcePoint" />
            <mxPoint x="760" y="460" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="43" edge="1" parent="1" style="endArrow=block;html=1;strokeColor=#94A3B8;strokeWidth=2;endFill=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="790" y="490" as="sourcePoint" />
            <mxPoint x="760" y="460" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="44" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#C2410C;strokeColor=#F97316;strokeWidth=2;fontSize=13;fontColor=#FED7AA;fontStyle=1;" value="Dog" vertex="1">
          <mxGeometry height="30" width="60" x="695" y="495" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#475569;strokeColor=#94A3B8;strokeWidth=2;fontSize=13;fontColor=#CBD5E1;fontStyle=1;" value="Cat" vertex="1">
          <mxGeometry height="30" width="60" x="765" y="495" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#10B981;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="105" width="820" x="40" y="560" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="🎯 使用场景" vertex="1">
          <mxGeometry height="30" width="110" x="40" y="560" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontColor=#6EE7B7;fontStyle=1;" value="向下转型前进行安全检查" vertex="1">
          <mxGeometry height="30" width="280" x="60" y="600" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#059669;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="25" width="780" x="60" y="630" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;fontFamily=Courier New;" value="if (animal instanceof Dog) { Dog dog = (Dog) animal; }" vertex="1">
          <mxGeometry height="25" width="780" x="60" y="630" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=#064E3B;align=center;verticalAlign=middle;fontSize=12;fontColor=#A7F3D0;rounded=1;" value="⚡ 避免 ClassCastException" vertex="1">
          <mxGeometry height="25" width="200" x="350" y="600" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

3.7 多态的实际应用

应用1:统一处理不同类型

public class Zoo {
    private List<Animal> animals = new ArrayList<>();
    
    public void addAnimal(Animal animal) {
        animals.add(animal);  // 可以添加任何 Animal 的子类
    }
    
    // 统一喂食
    public void feedAll() {
        for (Animal animal : animals) {
            animal.eat();  // 多态调用
        }
    }
    
    // 统一演出
    public void performAll() {
        for (Animal animal : animals) {
            animal.makeSound();  // 多态调用
        }
    }
}

// 使用
Zoo zoo = new Zoo();
zoo.addAnimal(new Dog("旺财"));
zoo.addAnimal(new Cat("咪咪"));
zoo.addAnimal(new Bird("鹦鹉"));
zoo.performAll();  // 每个动物发出自己的声音

应用2:策略模式

// 支付策略接口
interface PaymentStrategy {
    void pay(double amount);
}

// 具体策略
class WeChatPay implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        System.out.println("微信支付:" + amount + " 元");
    }
}

class AliPay implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        System.out.println("支付宝支付:" + amount + " 元");
    }
}

class CreditCardPay implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        System.out.println("信用卡支付:" + amount + " 元");
    }
}

// 购物车(使用多态)
class ShoppingCart {
    private PaymentStrategy paymentStrategy;
    
    public void setPaymentStrategy(PaymentStrategy strategy) {
        this.paymentStrategy = strategy;  // 可以接收任何支付策略
    }
    
    public void checkout(double amount) {
        paymentStrategy.pay(amount);  // 多态调用
    }
}

// 使用
ShoppingCart cart = new ShoppingCart();
cart.setPaymentStrategy(new WeChatPay());
cart.checkout(100);  // 微信支付:100 元

cart.setPaymentStrategy(new AliPay());
cart.checkout(200);  // 支付宝支付:200 元

3.8 多态的优缺点

<mxfile>
  <diagram id="polymorphism-pros-cons" 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="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=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="720" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="⚖️ 多态的优缺点" vertex="1">
          <mxGeometry height="55" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#052E16;strokeColor=#22C55E;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="370" width="820" x="40" y="90" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=20;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="✅ 优点" vertex="1">
          <mxGeometry height="45" width="120" x="40" y="90" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#4ADE80;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="380" x="60" y="150" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=none;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="1" vertex="1">
          <mxGeometry height="35" width="35" x="75" y="160" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontColor=#4ADE80;fontStyle=1;" value="可扩展性" vertex="1">
          <mxGeometry height="35" width="120" x="120" y="160" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;" value="新增子类不需要修改已有代码" vertex="1">
          <mxGeometry height="25" width="300" x="120" y="195" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="🔌" vertex="1">
          <mxGeometry height="50" width="50" x="380" y="165" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#4ADE80;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="380" x="460" y="150" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=none;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="2" vertex="1">
          <mxGeometry height="35" width="35" x="475" y="160" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontColor=#4ADE80;fontStyle=1;" value="灵活性" vertex="1">
          <mxGeometry height="35" width="100" x="520" y="160" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;" value="运行时决定具体行为" vertex="1">
          <mxGeometry height="25" width="200" x="520" y="195" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="⚡" vertex="1">
          <mxGeometry height="50" width="50" x="780" y="165" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#4ADE80;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="380" x="60" y="245" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=none;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="3" vertex="1">
          <mxGeometry height="35" width="35" x="75" y="255" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontColor=#4ADE80;fontStyle=1;" value="可替换性" vertex="1">
          <mxGeometry height="35" width="120" x="120" y="255" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;" value="父类引用可指向任意子类对象" vertex="1">
          <mxGeometry height="25" width="300" x="120" y="290" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="🔄" vertex="1">
          <mxGeometry height="50" width="50" x="380" y="260" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#4ADE80;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="380" x="460" y="245" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=none;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="4" vertex="1">
          <mxGeometry height="35" width="35" x="475" y="255" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontColor=#4ADE80;fontStyle=1;" value="接口统一" vertex="1">
          <mxGeometry height="35" width="120" x="520" y="255" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;" value="对外提供一致的调用方式" vertex="1">
          <mxGeometry height="25" width="250" x="520" y="290" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="🎯" vertex="1">
          <mxGeometry height="50" width="50" x="780" y="260" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#4ADE80;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="780" x="60" y="340" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=none;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="5" vertex="1">
          <mxGeometry height="35" width="35" x="75" y="350" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontColor=#4ADE80;fontStyle=1;" value="解耦" vertex="1">
          <mxGeometry height="35" width="80" x="120" y="350" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;" value="调用方不依赖具体实现类,降低模块间耦合度" vertex="1">
          <mxGeometry height="25" width="400" x="120" y="385" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="🔗" vertex="1">
          <mxGeometry height="50" width="50" x="780" y="355" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#172554;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="60" width="220" x="550" y="350" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#93C5FD;fontStyle=1;" value="符合开闭原则" vertex="1">
          <mxGeometry height="25" width="120" x="560" y="355" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#60A5FA;" value="对扩展开放,对修改关闭" vertex="1">
          <mxGeometry height="25" width="200" x="560" y="378" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;" value="💡" vertex="1">
          <mxGeometry height="35" width="35" x="730" y="360" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#450A0A;strokeColor=#DC2626;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="235" width="820" x="40" y="480" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#DC2626;align=center;verticalAlign=middle;fontSize=20;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="❌ 缺点" vertex="1">
          <mxGeometry height="45" width="120" x="40" y="480" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7F1D1D;strokeColor=#F87171;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="780" x="60" y="540" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#DC2626;strokeColor=none;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="1" vertex="1">
          <mxGeometry height="35" width="35" x="75" y="550" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontColor=#F87171;fontStyle=1;" value="无法直接访问子类特有成员" vertex="1">
          <mxGeometry height="35" width="300" x="120" y="550" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#FCA5A5;" value="需要向下转型才能访问子类独有的属性和方法" vertex="1">
          <mxGeometry height="25" width="400" x="120" y="585" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="🔒" vertex="1">
          <mxGeometry height="50" width="50" x="780" y="555" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#172554;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="60" width="230" x="530" y="550" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="解决方案" vertex="1">
          <mxGeometry height="22" width="70" x="530" y="550" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontColor=#93C5FD;fontFamily=Courier New;" value="instanceof + 强制转型" vertex="1">
          <mxGeometry height="30" width="220" x="530" y="575" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7F1D1D;strokeColor=#F87171;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="65" width="780" x="60" y="635" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#DC2626;strokeColor=none;fontSize=16;fontColor=#FFFFFF;fontStyle=1;" value="2" vertex="1">
          <mxGeometry height="35" width="35" x="75" y="645" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontColor=#F87171;fontStyle=1;" value="运行时才确定调用方法" vertex="1">
          <mxGeometry height="35" width="260" x="120" y="645" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#FCA5A5;" value="略有性能开销(动态绑定)" vertex="1">
          <mxGeometry height="20" width="250" x="120" y="675" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="⏱️" vertex="1">
          <mxGeometry height="50" width="50" x="780" y="645" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="360" x="400" y="645" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;fontStyle=1;" value="✅ 现代 JVM 优化后可忽略不计" vertex="1">
          <mxGeometry height="45" width="360" x="400" y="645" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

四、三大特征综合对比与总结

4.1 对比表

特征 核心思想 关键字/机制 主要作用
封装 隐藏细节 private/getter/setter 保护数据安全
继承 代码复用 extends/super 建立类层次结构
多态 统一接口 重写/向上转型 灵活扩展

4.2 三者关系图

<mxfile>
  <diagram id="three-features-relationship" 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="800" 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=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="780" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔺 三大特征的关系" vertex="1">
          <mxGeometry height="55" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="480" width="820" x="40" y="90" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#172554;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="90" width="240" x="330" y="110" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;fontColor=#60A5FA;fontStyle=1;" value="📦 封装" vertex="1">
          <mxGeometry height="45" width="240" x="330" y="115" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="基础" vertex="1">
          <mxGeometry height="28" width="70" x="415" y="160" as="geometry" />
        </mxCell>
        <mxCell id="8" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#60A5FA;strokeWidth=4;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="450" y="200" as="sourcePoint" />
            <mxPoint x="450" y="270" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0C4A6E;strokeColor=#0EA5E9;strokeWidth=2;dashed=1;" value="" vertex="1">
          <mxGeometry height="35" width="380" x="260" y="225" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#7DD3FC;" value="隐藏实现细节,提供公开接口" vertex="1">
          <mxGeometry height="35" width="380" x="260" y="225" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#22C55E;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="90" width="240" x="330" y="280" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;fontColor=#4ADE80;fontStyle=1;" value="🔗 继承" vertex="1">
          <mxGeometry height="45" width="240" x="330" y="285" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="手段" vertex="1">
          <mxGeometry height="28" width="70" x="415" y="330" as="geometry" />
        </mxCell>
        <mxCell id="14" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#4ADE80;strokeWidth=4;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="450" y="370" as="sourcePoint" />
            <mxPoint x="450" y="440" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#052E16;strokeColor=#16A34A;strokeWidth=2;dashed=1;" value="" vertex="1">
          <mxGeometry height="35" width="420" x="240" y="395" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;" value="子类复用父类代码,建立 is-a 关系" vertex="1">
          <mxGeometry height="35" width="420" x="240" y="395" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#581C87;strokeColor=#A855F7;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="90" width="240" x="330" y="450" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;fontColor=#C084FC;fontStyle=1;" value="✨ 多态" vertex="1">
          <mxGeometry height="45" width="240" x="330" y="455" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="目的" vertex="1">
          <mxGeometry height="28" width="70" x="415" y="500" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B0764;strokeColor=#9333EA;strokeWidth=2;dashed=1;" value="" vertex="1">
          <mxGeometry height="35" width="420" x="240" y="550" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#D8B4FE;" value="同一接口不同实现,实现灵活扩展" vertex="1">
          <mxGeometry height="35" width="420" x="240" y="550" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="170" width="820" x="40" y="595" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="⚠️ 缺一不可" vertex="1">
          <mxGeometry height="35" width="130" x="40" y="595" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#172554;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="55" width="250" x="60" y="645" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="没有封装" vertex="1">
          <mxGeometry height="22" width="70" x="60" y="645" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontColor=#93C5FD;" value="❌ 数据不安全,类设计混乱" vertex="1">
          <mxGeometry height="25" width="250" x="60" y="670" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="55" width="250" x="325" y="645" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="没有继承" vertex="1">
          <mxGeometry height="22" width="70" x="325" y="645" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontColor=#86EFAC;" value="❌ 多态无法实现(需父子关系)" vertex="1">
          <mxGeometry height="25" width="250" x="325" y="670" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#581C87;strokeColor=#A855F7;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="55" width="250" x="590" y="645" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="没有多态" vertex="1">
          <mxGeometry height="22" width="70" x="590" y="645" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontColor=#D8B4FE;" value="❌ 继承的价值大打折扣" vertex="1">
          <mxGeometry height="25" width="250" x="590" y="670" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#10B981;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="35" width="780" x="60" y="715" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#6EE7B7;fontStyle=1;" value="💡 封装是基石,继承是桥梁,多态是目标 —— 三者相辅相成,共同构建面向对象编程体系" vertex="1">
          <mxGeometry height="35" width="780" x="60" y="715" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="shape=curlyBracket;whiteSpace=wrap;html=1;rounded=1;flipH=1;labelPosition=right;verticalLabelPosition=middle;align=left;verticalAlign=middle;strokeColor=#F59E0B;strokeWidth=3;size=0.5;" value="" vertex="1">
          <mxGeometry height="420" width="30" x="580" y="120" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="100" width="220" x="620" y="280" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="🎯 层层递进" vertex="1">
          <mxGeometry height="28" width="100" x="620" y="280" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#93C5FD;" value="封装 → 保护数据" vertex="1">
          <mxGeometry height="20" width="180" x="635" y="315" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#86EFAC;" value="继承 → 复用代码" vertex="1">
          <mxGeometry height="20" width="180" x="635" y="335" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#D8B4FE;" value="多态 → 灵活扩展" vertex="1">
          <mxGeometry height="20" width="180" x="635" y="355" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="shape=curlyBracket;whiteSpace=wrap;html=1;rounded=1;labelPosition=right;verticalLabelPosition=middle;align=left;verticalAlign=middle;strokeColor=#F59E0B;strokeWidth=3;size=0.5;" value="" vertex="1">
          <mxGeometry height="420" width="30" x="90" y="120" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#EC4899;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="100" width="160" x="60" y="280" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=#EC4899;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="🏗️ 设计原则" vertex="1">
          <mxGeometry height="25" width="90" x="60" y="280" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#F9A8D4;" value="高内聚" vertex="1">
          <mxGeometry height="20" width="60" x="70" y="310" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#F9A8D4;" value="低耦合" vertex="1">
          <mxGeometry height="20" width="60" x="130" y="310" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#F9A8D4;" value="开闭原则" vertex="1">
          <mxGeometry height="20" width="70" x="70" y="335" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#F9A8D4;" value="里氏替换" vertex="1">
          <mxGeometry height="20" width="70" x="140" y="335" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#F9A8D4;" value="依赖倒置" vertex="1">
          <mxGeometry height="20" width="70" x="100" y="358" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

4.3 综合代码示例

/**
 * 综合示例:图形绘制系统
 */

// ========== 封装:隐藏属性,提供接口 ==========
abstract class Shape {
    private String color;      // 封装:私有属性
    private boolean filled;
    
    public Shape(String color, boolean filled) {
        this.color = color;
        this.filled = filled;
    }
    
    // 封装:公开接口
    public String getColor() { return color; }
    public void setColor(String color) { this.color = color; }
    public boolean isFilled() { return filled; }
    
    // 抽象方法
    public abstract double getArea();
    public abstract double getPerimeter();
    
    @Override
    public String toString() {
        return "Shape[color=" + color + ", filled=" + filled + "]";
    }
}

// ========== 继承:复用父类代码 ==========
class Circle extends Shape {
    private double radius;
    
    public Circle(String color, boolean filled, double radius) {
        super(color, filled);  // 调用父类构造
        this.radius = radius;
    }
    
    // 重写抽象方法
    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }
    
    @Override
    public double getPerimeter() {
        return 2 * Math.PI * radius;
    }
}

class Rectangle extends Shape {
    private double width;
    private double height;
    
    public Rectangle(String color, boolean filled, double width, double height) {
        super(color, filled);
        this.width = width;
        this.height = height;
    }
    
    @Override
    public double getArea() {
        return width * height;
    }
    
    @Override
    public double getPerimeter() {
        return 2 * (width + height);
    }
}

// ========== 多态:统一处理不同图形 ==========
class Canvas {
    private List<Shape> shapes = new ArrayList<>();
    
    // 多态参数
    public void addShape(Shape shape) {
        shapes.add(shape);
    }
    
    // 多态调用
    public double getTotalArea() {
        double total = 0;
        for (Shape shape : shapes) {
            total += shape.getArea();  // 调用各自的实现
        }
        return total;
    }
    
    public void printAllShapes() {
        for (Shape shape : shapes) {
            System.out.println(shape + ", Area=" + shape.getArea());
        }
    }
}

// ========== 使用 ==========
public class Main {
    public static void main(String[] args) {
        Canvas canvas = new Canvas();
        
        // 多态:父类引用指向子类对象
        canvas.addShape(new Circle("红色", true, 5));
        canvas.addShape(new Rectangle("蓝色", false, 3, 4));
        canvas.addShape(new Circle("绿色", true, 3));
        
        canvas.printAllShapes();
        System.out.println("总面积:" + canvas.getTotalArea());
    }
}

4.4 速查表

特征 核心要点 实现方式
封装 隐藏数据,暴露接口 private + getter/setter
继承 子类复用父类 extends + super
多态 同一行为多种形态 继承 + 重写 + 向上转型

4.5 记忆口诀

┌─────────────────────────────────────────────────────────────────┐
│                        记忆口诀                                 │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   封装:藏好属性露接口,getter/setter 来帮忙                    │
│                                                                 │
│   继承:extends 连接父与子,super 调用不能忘                    │
│                                                                 │
│   多态:父类引用指向子,编译看左运行右                          │
│                                                                 │
│   ─────────────────────────────────────────────────             │
│                                                                 │
│   多态三要素:                                                  │
│   ① 有继承  ② 有重写  ③ 父类引用指向子类对象                   │
│                                                                 │
│   方法重写规则(两同两小一大):                                │
│   • 两同:方法名、参数列表相同                                  │
│   • 两小:返回类型 ≤ 父类、异常 ≤ 父类                          │
│   • 一大:访问权限 ≥ 父类                                       │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘