Java 接口与抽象类对比

一、概念总览

1.1 什么是抽象类和接口

<mxfile>
  <diagram id="abstract-vs-interface" name="抽象类与接口的定位">
    <mxGraphModel dx="900" dy="750" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="750">
      <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="710" as="geometry" />
        </mxCell>
        <mxCell id="3" value="🎯 抽象类与接口的定位" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="20" y="20" width="860" height="55" as="geometry" />
        </mxCell>
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#172554;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="40" y="90" width="820" height="295" as="geometry" />
        </mxCell>
        <mxCell id="5" value="📦 抽象类 (Abstract Class)" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="40" y="90" width="820" height="45" as="geometry" />
        </mxCell>
        <mxCell id="6" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#60A5FA;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="150" width="780" height="60" as="geometry" />
        </mxCell>
        <mxCell id="7" value="定义" style="text;html=1;strokeColor=none;fillColor=#60A5FA;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontStyle=1;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="75" y="160" width="60" height="28" as="geometry" />
        </mxCell>
        <mxCell id="8" value="不能被实例化的类,可以包含抽象方法和具体方法" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontColor=#BFDBFE;" vertex="1" parent="1">
          <mxGeometry x="150" y="155" width="670" height="50" as="geometry" />
        </mxCell>
        <mxCell id="9" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#60A5FA;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="220" width="780" height="60" as="geometry" />
        </mxCell>
        <mxCell id="10" value="本质" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontStyle=1;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="75" y="230" width="60" height="28" as="geometry" />
        </mxCell>
        <mxCell id="11" value="是一种【不完整的类】,需要子类来完善" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontColor=#FDE68A;" vertex="1" parent="1">
          <mxGeometry x="150" y="225" width="500" height="50" as="geometry" />
        </mxCell>
        <mxCell id="12" value="🧩" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=32;" vertex="1" parent="1">
          <mxGeometry x="770" y="225" width="50" height="50" as="geometry" />
        </mxCell>
        <mxCell id="13" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#60A5FA;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="290" width="780" height="80" as="geometry" />
        </mxCell>
        <mxCell id="14" value="关系" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontStyle=1;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="75" y="300" width="60" height="28" as="geometry" />
        </mxCell>
        <mxCell id="15" value="子类与抽象类是 &quot;is-a&quot; 关系(是一种)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontColor=#86EFAC;" vertex="1" parent="1">
          <mxGeometry x="150" y="295" width="400" height="35" as="geometry" />
        </mxCell>
        <mxCell id="16" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#4ADE80;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="150" y="330" width="250" height="32" as="geometry" />
        </mxCell>
        <mxCell id="17" value="🐕 Dog is an Animal" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#4ADE80;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="150" y="330" width="250" height="32" as="geometry" />
        </mxCell>
        <mxCell id="18" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0C4A6E;strokeColor=#0EA5E9;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="560" y="295" width="260" height="65" as="geometry" />
        </mxCell>
        <mxCell id="19" value="is-a" style="text;html=1;strokeColor=none;fillColor=#0EA5E9;align=center;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="575" y="305" width="70" height="30" as="geometry" />
        </mxCell>
        <mxCell id="20" value="是一种..." style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#7DD3FC;" vertex="1" parent="1">
          <mxGeometry x="660" y="305" width="150" height="30" as="geometry" />
        </mxCell>
        <mxCell id="21" value="强调身份和本质" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#38BDF8;" vertex="1" parent="1">
          <mxGeometry x="575" y="335" width="230" height="20" as="geometry" />
        </mxCell>
        <mxCell id="22" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#581C87;strokeColor=#A855F7;strokeWidth=3;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="40" y="400" width="820" height="295" as="geometry" />
        </mxCell>
        <mxCell id="23" value="📋 接口 (Interface)" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="40" y="400" width="820" height="45" as="geometry" />
        </mxCell>
        <mxCell id="24" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4C1D95;strokeColor=#C084FC;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="460" width="780" height="60" as="geometry" />
        </mxCell>
        <mxCell id="25" value="定义" style="text;html=1;strokeColor=none;fillColor=#C084FC;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontStyle=1;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="75" y="470" width="60" height="28" as="geometry" />
        </mxCell>
        <mxCell id="26" value="完全抽象的类型,定义行为规范(契约)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontColor=#E9D5FF;" vertex="1" parent="1">
          <mxGeometry x="150" y="465" width="670" height="50" as="geometry" />
        </mxCell>
        <mxCell id="27" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4C1D95;strokeColor=#C084FC;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="530" width="780" height="60" as="geometry" />
        </mxCell>
        <mxCell id="28" value="本质" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontStyle=1;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="75" y="540" width="60" height="28" as="geometry" />
        </mxCell>
        <mxCell id="29" value="是一种【能力声明】,表示实现类具备某种能力" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontColor=#FDE68A;" vertex="1" parent="1">
          <mxGeometry x="150" y="535" width="500" height="50" as="geometry" />
        </mxCell>
        <mxCell id="30" value="⚡" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=32;" vertex="1" parent="1">
          <mxGeometry x="770" y="535" width="50" height="50" as="geometry" />
        </mxCell>
        <mxCell id="31" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4C1D95;strokeColor=#C084FC;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="60" y="600" width="780" height="80" as="geometry" />
        </mxCell>
        <mxCell id="32" value="关系" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;fontStyle=1;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="75" y="610" width="60" height="28" as="geometry" />
        </mxCell>
        <mxCell id="33" value="实现类与接口是 &quot;can-do&quot; 关系(能做什么)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontColor=#86EFAC;" vertex="1" parent="1">
          <mxGeometry x="150" y="605" width="400" height="35" as="geometry" />
        </mxCell>
        <mxCell id="34" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#4ADE80;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="150" y="640" width="250" height="32" as="geometry" />
        </mxCell>
        <mxCell id="35" value="🐦 Bird can Fly" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#4ADE80;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="150" y="640" width="250" height="32" as="geometry" />
        </mxCell>
        <mxCell id="36" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#701A75;strokeColor=#E879F9;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="560" y="605" width="260" height="65" as="geometry" />
        </mxCell>
        <mxCell id="37" value="can-do" style="text;html=1;strokeColor=none;fillColor=#E879F9;align=center;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="575" y="615" width="80" height="30" as="geometry" />
        </mxCell>
        <mxCell id="38" value="能做..." style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#F0ABFC;" vertex="1" parent="1">
          <mxGeometry x="670" y="615" width="140" height="30" as="geometry" />
        </mxCell>
        <mxCell id="39" value="强调能力和行为" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#D946EF;" vertex="1" parent="1">
          <mxGeometry x="575" y="645" width="230" height="20" as="geometry" />
        </mxCell>
        <mxCell id="40" value="VS" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;rounded=1;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="420" y="375" width="60" height="35" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

1.2 类比理解

<mxfile>
  <diagram id="life-analogy" name="生活类比">
    <mxGraphModel dx="1665" dy="750" 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=#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=#172554;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="310" width="820" x="40" y="90" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📦 抽象类 → 像【模板/半成品】" vertex="1">
          <mxGeometry height="45" width="820" x="40" y="90" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#60A5FA;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="235" width="380" x="60" y="150" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🏠 毛坯房(抽象类)" vertex="1">
          <mxGeometry height="35" width="200" x="75" y="165" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="340" x="80" y="215" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="✅ 已有" vertex="1">
          <mxGeometry height="22" width="60" x="90" y="222" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;" value="框架结构、水电管道" vertex="1">
          <mxGeometry height="28" width="250" x="160" y="218" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#4ADE80;fontStyle=2;" value="(具体方法)" vertex="1">
          <mxGeometry height="28" width="250" x="90" y="250" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7C2D12;strokeColor=#F97316;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="340" x="80" y="295" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#F97316;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="⏳ 待定" vertex="1">
          <mxGeometry height="22" width="60" x="90" y="302" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#FDBA74;" value="装修风格、家具布置" vertex="1">
          <mxGeometry height="28" width="250" x="160" y="298" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#FB923C;fontStyle=2;" value="(抽象方法)" vertex="1">
          <mxGeometry height="28" width="250" x="90" y="330" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#60A5FA;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="235" width="380" x="460" y="150" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#60A5FA;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔨 继承毛坯房" vertex="1">
          <mxGeometry height="30" width="120" x="475" y="165" as="geometry" />
        </mxCell>
        <mxCell id="18" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#60A5FA;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="650" y="210" as="sourcePoint" />
            <mxPoint x="650" y="260" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0C4A6E;strokeColor=#0EA5E9;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="100" width="150" x="480" y="270" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=36;" value="🏡" vertex="1">
          <mxGeometry height="50" width="100" x="505" y="275" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontColor=#7DD3FC;" value="现代简约风" vertex="1">
          <mxGeometry height="30" width="150" x="480" y="330" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0C4A6E;strokeColor=#0EA5E9;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="100" width="150" x="670" y="270" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=36;" value="🏰" vertex="1">
          <mxGeometry height="50" width="100" x="695" y="275" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontColor=#7DD3FC;" value="欧式古典风" vertex="1">
          <mxGeometry height="30" width="150" x="670" y="330" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#93C5FD;fontStyle=2;" value="装修成你想要的风格" vertex="1">
          <mxGeometry height="25" width="240" x="530" y="220" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#581C87;strokeColor=#A855F7;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="330" width="820" x="40" y="415" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📋 接口 → 像【资格证书/能力认证】" vertex="1">
          <mxGeometry height="45" width="820" x="40" y="415" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4C1D95;strokeColor=#C084FC;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="120" width="380" x="60" y="475" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📜 驾驶证(接口)" vertex="1">
          <mxGeometry height="35" width="170" x="75" y="490" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#701A75;strokeColor=#E879F9;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="40" width="340" x="80" y="540" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#F0ABFC;" value="📢 声明:持有者具备驾驶能力" vertex="1">
          <mxGeometry height="40" width="340" x="80" y="540" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4C1D95;strokeColor=#C084FC;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="255" width="380" x="460" y="475" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=#C084FC;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="👤 一个人可以同时拥有:" vertex="1">
          <mxGeometry height="30" width="200" x="475" y="490" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="55" width="340" x="480" y="535" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="🚗" vertex="1">
          <mxGeometry height="45" width="50" x="490" y="540" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;" value="驾驶证(Drivable)" vertex="1">
          <mxGeometry height="30" width="260" x="545" y="550" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0C4A6E;strokeColor=#0EA5E9;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="55" width="340" x="480" y="600" 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="45" width="50" x="490" y="605" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#7DD3FC;" value="游泳证(Swimmable)" vertex="1">
          <mxGeometry height="30" width="260" x="545" y="615" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7C2D12;strokeColor=#F97316;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="55" width="340" x="480" y="665" 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="45" width="50" x="490" y="670" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#FDBA74;" value="厨师证(Cookable)" vertex="1">
          <mxGeometry height="30" width="260" x="545" y="680" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#701A75;strokeColor=#E879F9;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="120" width="380" x="60" y="610" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=#E879F9;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🎯 关键区别" vertex="1">
          <mxGeometry height="28" width="100" x="75" y="620" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#93C5FD;" value="抽象类:单继承(一个人只能住一套毛坯房)" vertex="1">
          <mxGeometry height="25" width="340" x="80" y="655" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#D8B4FE;" value="接口:多实现(一个人可以考多个证书)" vertex="1">
          <mxGeometry height="25" width="340" x="80" y="685" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

二、基本语法

2.1 抽象类语法

/**
 * 抽象类定义
 */
public abstract class Animal {

    // ===== 1. 可以有各种访问修饰符的属性 =====
    private String name;           // 私有属性
    protected int age;             // 受保护属性
    public static int count = 0;   // 静态属性

    // ===== 2. 可以有构造方法 =====
    public Animal() {
        count++;
    }

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
        count++;
    }

    // ===== 3. 可以有具体方法(有方法体)=====
    public void sleep() {
        System.out.println(name + " 正在睡觉");
    }

    // ===== 4. 可以有抽象方法(无方法体)=====
    public abstract void makeSound();  // 子类必须实现

    public abstract void eat();        // 子类必须实现

    // ===== 5. Getter/Setter =====
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

2.2 接口语法

/**
 * 接口定义
 */
public interface Flyable {

    // ===== 1. 常量(默认 public static final)=====
    int MAX_HEIGHT = 10000;  // 等价于 public static final int MAX_HEIGHT = 10000;

    // ===== 2. 抽象方法(默认 public abstract)=====
    void fly();              // 等价于 public abstract void fly();

    void land();

    // ===== 3. 默认方法(Java 8+)=====
    default void glide() {
        System.out.println("滑翔中...");
    }

    // ===== 4. 静态方法(Java 8+)=====
    static void showInfo() {
        System.out.println("这是飞行接口");
    }

    // ===== 5. 私有方法(Java 9+)=====
    private void prepare() {
        System.out.println("准备飞行");
    }
}

2.3 语法对比表

语法元素 抽象类 接口
关键字 abstract class interface
继承/实现 extends implements
构造方法 ✅ 可以有 ❌ 不能有
普通属性 ✅ 任意修饰符 ❌ 只能是常量
静态属性 ✅ 可以有 ✅ 默认 public static final
具体方法 ✅ 可以有 default/static 方法
抽象方法 ✅ 可以有 ✅ 默认就是抽象的
代码块 ✅ 可以有 ❌ 不能有

三、共同点

3.1 共同点一览

<mxfile>
  <diagram id="common-points" name="抽象类与接口的共同点">
    <mxGraphModel dx="1665" dy="750" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="1050" 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="1040" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;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=#EF4444;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="150" width="820" x="40" y="90" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="1️⃣ 都不能直接实例化" vertex="1">
          <mxGeometry height="35" width="220" x="55" y="105" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#450A0A;strokeColor=#F87171;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="75" width="380" x="60" y="150" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=#DC2626;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="❌ 编译错误" vertex="1">
          <mxGeometry height="22" width="90" x="75" y="158" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#FCA5A5;fontFamily=Courier New;" value="Animal animal = new Animal();&lt;br&gt;Flyable flyable = new Flyable();" vertex="1">
          <mxGeometry height="35" width="350" x="75" y="185" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#172554;strokeColor=#60A5FA;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="75" width="380" x="460" y="150" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#93C5FD;" value="💡 必须通过子类/实现类来创建对象" vertex="1">
          <mxGeometry height="35" width="380" x="460" y="170" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="175" width="820" x="40" y="255" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="2️⃣ 都可以包含抽象方法" vertex="1">
          <mxGeometry height="35" width="230" x="55" y="270" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#172554;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="100" width="380" x="60" y="315" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="抽象类" vertex="1">
          <mxGeometry height="22" width="60" x="75" y="323" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#93C5FD;fontFamily=Courier New;" value="abstract class Animal {&lt;br&gt;&amp;nbsp; &amp;nbsp; abstract void eat();&lt;br&gt;}" vertex="1">
          <mxGeometry height="55" width="350" x="75" y="350" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4C1D95;strokeColor=#A855F7;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="100" width="380" x="460" y="315" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="接口" vertex="1">
          <mxGeometry height="22" width="60" x="475" y="323" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#D8B4FE;fontFamily=Courier New;" value="interface Flyable {&lt;br&gt;&amp;nbsp; &amp;nbsp; void fly(); // 默认抽象&lt;br&gt;}" vertex="1">
          <mxGeometry height="55" width="350" x="475" y="350" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#22C55E;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="175" width="820" x="40" y="445" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="3️⃣ 都可以有具体方法实现" vertex="1">
          <mxGeometry height="35" width="250" x="55" y="460" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#172554;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="100" width="380" x="60" y="505" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="抽象类" vertex="1">
          <mxGeometry height="22" width="60" x="75" y="513" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#93C5FD;fontFamily=Courier New;" value="abstract class Animal {&lt;br&gt;&amp;nbsp; &amp;nbsp; void sleep() { ... }&lt;br&gt;}" vertex="1">
          <mxGeometry height="55" width="350" x="75" y="540" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4C1D95;strokeColor=#A855F7;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="100" width="380" x="460" y="505" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="接口" vertex="1">
          <mxGeometry height="22" width="60" x="475" y="513" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#D8B4FE;fontFamily=Courier New;" value="interface Flyable {&lt;br&gt;&amp;nbsp; &amp;nbsp; default void glide() { ... }&lt;br&gt;}" vertex="1">
          <mxGeometry height="55" width="350" x="475" y="540" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#7C3AED;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Java 8+" vertex="1">
          <mxGeometry height="20" width="55" x="770" y="513" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#06B6D4;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="150" width="820" x="40" y="635" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#06B6D4;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="4️⃣ 都可以作为引用类型(多态)" vertex="1">
          <mxGeometry height="35" width="290" x="55" y="650" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#172554;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="75" width="380" x="60" y="695" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="抽象类作为类型" vertex="1">
          <mxGeometry height="22" width="110" x="75" y="703" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#93C5FD;fontFamily=Courier New;" value="Animal animal = new Dog();" vertex="1">
          <mxGeometry height="30" width="350" x="75" y="730" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4C1D95;strokeColor=#A855F7;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="75" width="380" x="460" y="695" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="接口作为类型" vertex="1">
          <mxGeometry height="22" width="100" x="475" y="703" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#D8B4FE;fontFamily=Courier New;" value="Flyable flyer = new Bird();" vertex="1">
          <mxGeometry height="30" width="350" x="475" y="730" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#8B5CF6;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="215" width="820" x="40" y="800" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="5️⃣ 都用于实现抽象和解耦" vertex="1">
          <mxGeometry height="35" width="260" x="55" y="815" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E1B4B;strokeColor=#A78BFA;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="135" width="380" x="60" y="865" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=#A78BFA;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📋 定义规范" vertex="1">
          <mxGeometry height="28" width="110" x="75" y="880" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#C4B5FD;" value="• 定义统一的行为规范&lt;br&gt;• 隐藏具体实现细节&lt;br&gt;• 提供抽象层" vertex="1">
          <mxGeometry height="70" width="340" x="80" y="915" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E1B4B;strokeColor=#A78BFA;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="135" width="380" x="460" y="865" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#A78BFA;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔗 降低耦合" vertex="1">
          <mxGeometry height="28" width="110" x="475" y="880" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#C4B5FD;" value="• 面向抽象编程&lt;br&gt;• 依赖倒置原则&lt;br&gt;• 便于扩展和维护" vertex="1">
          <mxGeometry height="70" width="340" x="480" y="915" as="geometry" />
        </mxCell>
        <mxCell id="44" 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="780" y="870" as="geometry" />
        </mxCell>
        <mxCell id="45" 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="380" y="870" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

四、核心差异

4.1 差异对比表

特性 抽象类 (Abstract Class) 接口 (Interface)
设计目的 提供通用实现(模板) 定义行为规范(契约)
继承/实现 单继承(只能 extends 一个) 多实现(可 implements 多个)
成员变量 任意修饰符,可以是普通变量 只能是 public static final 常量
构造方法 ✅ 可以有 ❌ 不能有
方法实现 可以有具体方法 Java 8 前只有抽象方法
访问修饰符 任意(public/protected/private) 方法默认 public
静态方法 ✅ 可以有 ✅ Java 8+ 可以有
main 方法 ✅ 可以有 ✅ Java 8+ 可以有
设计层面 代码复用(is-a 关系) 能力扩展(can-do 关系)

4.2 差异图解

<mxfile>
  <diagram id="core-differences" name="核心差异可视化">
    <mxGraphModel dx="1665" dy="750" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="1000" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="990" 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="⚡ 核心差异可视化" 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=#172554;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="420" width="395" x="40" y="90" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📦 抽象类 (Abstract Class)" vertex="1">
          <mxGeometry height="45" width="395" x="40" y="90" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#60A5FA;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="355" x="60" y="150" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=#2563EB;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔹 普通属性" vertex="1">
          <mxGeometry height="24" width="100" x="75" y="158" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#93C5FD;fontFamily=Courier New;" value="private String name;&lt;br&gt;protected int age;" vertex="1">
          <mxGeometry height="30" width="320" x="75" y="185" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#60A5FA;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="355" x="60" y="230" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=#2563EB;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔹 构造方法" vertex="1">
          <mxGeometry height="24" width="100" x="75" y="238" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#93C5FD;fontFamily=Courier New;" value="public Animal(String name) { }" vertex="1">
          <mxGeometry height="30" width="320" x="75" y="265" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#60A5FA;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="355" x="60" y="310" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#2563EB;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔹 具体方法" vertex="1">
          <mxGeometry height="24" width="100" x="75" y="318" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#93C5FD;fontFamily=Courier New;" value="void sleep() { ... }" vertex="1">
          <mxGeometry height="30" width="320" x="75" y="345" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#60A5FA;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="355" x="60" y="390" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=#2563EB;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔹 抽象方法" vertex="1">
          <mxGeometry height="24" width="100" x="75" y="398" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#93C5FD;fontFamily=Courier New;" value="abstract void eat();" vertex="1">
          <mxGeometry height="30" width="320" x="75" y="425" as="geometry" />
        </mxCell>
        <mxCell id="18" 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="375" y="155" as="geometry" />
        </mxCell>
        <mxCell id="19" 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="375" y="235" as="geometry" />
        </mxCell>
        <mxCell id="20" 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="375" y="315" as="geometry" />
        </mxCell>
        <mxCell id="21" 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="375" y="395" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#1D4ED8;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="extends" vertex="1">
          <mxGeometry height="25" width="80" x="60" y="470" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#581C87;strokeColor=#A855F7;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="420" width="395" x="465" y="90" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📋 接口 (Interface)" vertex="1">
          <mxGeometry height="45" width="395" x="465" y="90" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4C1D95;strokeColor=#C084FC;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="355" x="485" y="150" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=#9333EA;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔸 常量 (final)" vertex="1">
          <mxGeometry height="24" width="110" x="500" y="158" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#D8B4FE;fontFamily=Courier New;" value="int MAX_SIZE = 100;&lt;br&gt;String TYPE = &quot;animal&quot;;" vertex="1">
          <mxGeometry height="30" width="320" x="500" y="185" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#450A0A;strokeColor=#F87171;strokeWidth=2;strokeDasharray=3 3;" value="" vertex="1">
          <mxGeometry height="70" width="355" x="485" y="230" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#DC2626;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="✗ 无构造方法" vertex="1">
          <mxGeometry height="24" width="120" x="500" y="238" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#FCA5A5;fontStyle=2;" value="接口不能有构造方法" vertex="1">
          <mxGeometry height="30" width="320" x="500" y="265" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4C1D95;strokeColor=#C084FC;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="355" x="485" y="310" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#9333EA;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔸 默认方法" vertex="1">
          <mxGeometry height="24" width="100" x="500" y="318" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#D8B4FE;fontFamily=Courier New;" value="default void glide() { ... }" vertex="1">
          <mxGeometry height="30" width="320" x="500" y="345" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#7C3AED;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Java 8+" vertex="1">
          <mxGeometry height="20" width="55" x="610" y="318" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4C1D95;strokeColor=#C084FC;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="355" x="485" y="390" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#9333EA;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔸 抽象方法" vertex="1">
          <mxGeometry height="24" width="100" x="500" y="398" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#D8B4FE;fontFamily=Courier New;" value="void fly();  // 默认 public abstract" vertex="1">
          <mxGeometry height="30" width="320" x="500" y="425" as="geometry" />
        </mxCell>
        <mxCell id="38" 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="800" y="155" as="geometry" />
        </mxCell>
        <mxCell id="39" 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="800" y="235" as="geometry" />
        </mxCell>
        <mxCell id="40" 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="800" y="315" as="geometry" />
        </mxCell>
        <mxCell id="41" 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="800" y="395" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#7C3AED;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="implements" vertex="1">
          <mxGeometry height="25" width="95" x="485" y="470" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#3B82F6;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="220" width="395" x="40" y="525" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📊 继承关系(单继承)" vertex="1">
          <mxGeometry height="35" width="200" x="55" y="540" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1D4ED8;strokeColor=#60A5FA;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="Animal" vertex="1">
          <mxGeometry height="45" width="120" x="177" y="590" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0E7490;strokeColor=#22D3EE;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="Dog" vertex="1">
          <mxGeometry height="40" width="100" x="90" y="685" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0E7490;strokeColor=#22D3EE;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="Cat" vertex="1">
          <mxGeometry height="40" width="100" x="285" y="685" as="geometry" />
        </mxCell>
        <mxCell id="48" edge="1" parent="1" source="46" style="endArrow=block;html=1;strokeColor=#60A5FA;strokeWidth=2;endFill=0;exitX=0.5;exitY=0;entryX=0.25;entryY=1;" target="45" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="49" edge="1" parent="1" source="47" style="endArrow=block;html=1;strokeColor=#60A5FA;strokeWidth=2;endFill=0;exitX=0.5;exitY=0;entryX=0.75;entryY=1;" target="45" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#93C5FD;fontStyle=2;" value="Dog 只能继承一个父类" vertex="1">
          <mxGeometry height="35" width="50" x="210" y="660" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#A855F7;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="220" width="395" x="465" y="525" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📊 实现关系(多实现)" vertex="1">
          <mxGeometry height="35" width="200" x="480" y="540" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7C3AED;strokeColor=#C084FC;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="Flyable" vertex="1">
          <mxGeometry height="45" width="100" x="510" y="590" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7C3AED;strokeColor=#C084FC;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="Swimmable" vertex="1">
          <mxGeometry height="45" width="110" x="715" y="590" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0E7490;strokeColor=#22D3EE;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="Duck" vertex="1">
          <mxGeometry height="40" width="100" x="612" y="685" as="geometry" />
        </mxCell>
        <mxCell id="56" edge="1" parent="1" source="55" style="endArrow=block;html=1;strokeColor=#C084FC;strokeWidth=2;endFill=0;dashed=1;exitX=0.5;exitY=0;entryX=0.5;entryY=1;" target="53" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="57" edge="1" parent="1" source="55" style="endArrow=block;html=1;strokeColor=#C084FC;strokeWidth=2;endFill=0;dashed=1;exitX=0.5;exitY=0;entryX=0.5;entryY=1;" target="54" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="58" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#D8B4FE;fontStyle=2;" value="Duck 可实现多个接口" vertex="1">
          <mxGeometry height="35" width="90" x="625" y="630" as="geometry" />
        </mxCell>
        <mxCell id="59" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="205" width="820" x="40" y="760" as="geometry" />
        </mxCell>
        <mxCell id="60" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🎯 关键差异总结" vertex="1">
          <mxGeometry height="45" width="820" x="40" y="760" as="geometry" />
        </mxCell>
        <mxCell id="61" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#172554;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="130" width="250" x="60" y="820" as="geometry" />
        </mxCell>
        <mxCell id="62" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📦 抽象类" vertex="1">
          <mxGeometry height="28" width="90" x="75" y="830" as="geometry" />
        </mxCell>
        <mxCell id="63" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#93C5FD;" value="• 单继承&lt;br&gt;• 有构造方法&lt;br&gt;• 可有普通属性&lt;br&gt;• 表示 &quot;是什么&quot;" vertex="1">
          <mxGeometry height="75" width="220" x="75" y="865" as="geometry" />
        </mxCell>
        <mxCell id="64" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4C1D95;strokeColor=#A855F7;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="130" width="250" x="325" y="820" as="geometry" />
        </mxCell>
        <mxCell id="65" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📋 接口" vertex="1">
          <mxGeometry height="28" width="80" x="340" y="830" as="geometry" />
        </mxCell>
        <mxCell id="66" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#D8B4FE;" value="• 多实现&lt;br&gt;• 无构造方法&lt;br&gt;• 只有常量&lt;br&gt;• 表示 &quot;能做什么&quot;" vertex="1">
          <mxGeometry height="75" width="220" x="340" y="865" as="geometry" />
        </mxCell>
        <mxCell id="67" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="130" width="250" x="590" y="820" as="geometry" />
        </mxCell>
        <mxCell id="68" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💡 选择建议" vertex="1">
          <mxGeometry height="28" width="100" x="605" y="830" as="geometry" />
        </mxCell>
        <mxCell id="69" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#86EFAC;" value="• 共享代码 → 抽象类&lt;br&gt;• 定义能力 → 接口&lt;br&gt;• 多重继承 → 接口&lt;br&gt;• 紧密关系 → 抽象类" vertex="1">
          <mxGeometry height="75" width="220" x="605" y="865" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

4.3 单继承 vs 多实现

// ===== 抽象类:单继承 =====
abstract class Animal { }
abstract class Machine { }

// ❌ 编译错误:Java 不支持多继承
// class Robot extends Animal, Machine { }

// ===== 接口:多实现 =====
interface Flyable { void fly(); }
interface Swimmable { void swim(); }
interface Runnable { void run(); }

// ✅ 正确:可以实现多个接口
class Duck extends Animal implements Flyable, Swimmable {
    @Override
    public void fly() { System.out.println("鸭子飞"); }

    @Override
    public void swim() { System.out.println("鸭子游泳"); }
}

// ✅ 接口可以继承多个接口
interface Amphibious extends Flyable, Swimmable {
    void crawl();  // 额外的方法
}

继承关系图

<mxfile>
  <diagram id="inheritance-implementation" 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="760" pageHeight="640" 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="20" y="20" width="720" height="600" 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="20" y="20" width="720" height="55" as="geometry" />
        </mxCell>

        <!-- 关系图区域 -->
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="40" y="90" width="680" height="280" as="geometry" />
        </mxCell>

        <!-- Flyable 接口 (顶部) -->
        <mxCell id="5" value="«interface»" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#e65100;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="330" y="110" width="100" height="20" as="geometry" />
        </mxCell>
        <mxCell id="6" value="Flyable" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fb8c00;strokeColor=#e65100;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="330" y="130" width="100" height="40" as="geometry" />
        </mxCell>

        <!-- Duck 实现 Flyable 的虚线箭头 -->
        <mxCell id="7" value="" style="endArrow=block;endFill=0;html=1;rounded=0;strokeWidth=2;strokeColor=#fb8c00;dashed=1;dashPattern=8 8;" edge="1" parent="1">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="380" y="240" as="sourcePoint" />
            <mxPoint x="380" y="170" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="8" value="implements" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;fontColor=#e65100;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="385" y="195" width="70" height="20" as="geometry" />
        </mxCell>

        <!-- Animal 抽象类 (左侧) -->
        <mxCell id="9" value="Animal" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=#0d47a1;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="100" y="250" width="110" height="45" as="geometry" />
        </mxCell>
        <mxCell id="10" value="(abstract)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;fontColor=#1565c0;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="100" y="295" width="110" height="18" as="geometry" />
        </mxCell>

        <!-- Duck 类 (中间) -->
        <mxCell id="11" value="Duck" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#43a047;strokeColor=#2e7d32;strokeWidth=2;fontSize=18;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="320" y="245" width="120" height="55" as="geometry" />
        </mxCell>

        <!-- Swimmable 接口 (右侧) -->
        <mxCell id="12" value="«interface»" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#e65100;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="550" y="240" width="100" height="20" as="geometry" />
        </mxCell>
        <mxCell id="13" value="Swimmable" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fb8c00;strokeColor=#e65100;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="540" y="260" width="110" height="40" as="geometry" />
        </mxCell>

        <!-- Duck 继承 Animal 的实线箭头 -->
        <mxCell id="14" value="" style="endArrow=block;endFill=0;html=1;rounded=0;strokeWidth=2;strokeColor=#1976d2;" edge="1" parent="1">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="320" y="272" as="sourcePoint" />
            <mxPoint x="210" y="272" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="15" value="extends" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;fontColor=#1565c0;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="240" y="250" width="60" height="18" as="geometry" />
        </mxCell>

        <!-- Duck 实现 Swimmable 的虚线箭头 -->
        <mxCell id="16" value="" style="endArrow=block;endFill=0;html=1;rounded=0;strokeWidth=2;strokeColor=#fb8c00;dashed=1;dashPattern=8 8;" edge="1" parent="1">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="440" y="272" as="sourcePoint" />
            <mxPoint x="540" y="272" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="17" value="implements" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;fontColor=#e65100;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="455" y="250" width="70" height="18" as="geometry" />
        </mxCell>

        <!-- 说明区域 -->
        <mxCell id="18" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#9e9e9e;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="40" y="390" width="680" height="210" as="geometry" />
        </mxCell>
        <mxCell id="19" value="说明" style="text;html=1;strokeColor=none;fillColor=#7c4dff;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=16;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="40" y="390" width="680" height="35" as="geometry" />
        </mxCell>

        <!-- 说明内容 -->
        <mxCell id="20" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="440" width="310" height="65" as="geometry" />
        </mxCell>
        <mxCell id="21" value="•" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#43a047;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="65" y="445" width="20" height="25" as="geometry" />
        </mxCell>
        <mxCell id="22" value="Duck extends Animal" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontFamily=Consolas;fontColor=#2e7d32;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="85" y="445" width="200" 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=12;fontColor=#558b2f;" vertex="1" parent="1">
          <mxGeometry x="85" y="470" width="150" height="25" as="geometry" />
        </mxCell>
        <mxCell id="24" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffcc80;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="390" y="440" width="310" height="65" as="geometry" />
        </mxCell>
        <mxCell id="25" value="•" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#fb8c00;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="395" y="445" width="20" height="25" as="geometry" />
        </mxCell>
        <mxCell id="26" value="Duck implements Flyable, Swimmable" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontFamily=Consolas;fontColor=#e65100;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="415" y="445" width="280" height="25" as="geometry" />
        </mxCell>
        <mxCell id="27" value="(实现多个接口)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#ef6c00;" vertex="1" parent="1">
          <mxGeometry x="415" y="470" width="150" height="25" as="geometry" />
        </mxCell>

        <!-- 图例 -->
        <mxCell id="28" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#90caf9;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="520" width="310" height="60" as="geometry" />
        </mxCell>
        <mxCell id="29" value="" style="endArrow=block;endFill=0;html=1;rounded=0;strokeWidth=3;strokeColor=#1976d2;" edge="1" parent="1">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="80" y="545" as="sourcePoint" />
            <mxPoint x="140" y="545" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="30" value="继承(实线 + 空心三角)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#1565c0;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="150" y="532" width="200" height="25" as="geometry" />
        </mxCell>
        <mxCell id="31" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ffcc80;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="390" y="520" width="310" height="60" as="geometry" />
        </mxCell>
        <mxCell id="32" value="" style="endArrow=block;endFill=0;html=1;rounded=0;strokeWidth=3;strokeColor=#fb8c00;dashed=1;dashPattern=8 8;" edge="1" parent="1">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="410" y="545" as="sourcePoint" />
            <mxPoint x="470" y="545" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="33" value="实现(虚线 + 空心三角)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#e65100;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="480" y="532" width="200" height="25" as="geometry" />
        </mxCell>

      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

五、完整代码示例

5.1 抽象类示例

/**
 * 抽象类:鸟类
 * 提供通用实现 + 定义抽象行为
 */
public abstract class Bird {

    // ===== 属性(可以有任意类型)=====
    protected String name;
    protected String color;
    private int age;

    // ===== 构造方法 =====
    public Bird() {
        System.out.println("Bird 构造方法执行");
    }

    public Bird(String name, String color) {
        this.name = name;
        this.color = color;
    }

    // ===== 具体方法(通用实现)=====
    public void sleep() {
        System.out.println(name + " 正在睡觉 Zzz...");
    }

    public void showInfo() {
        System.out.println("鸟名:" + name + ",颜色:" + color);
    }

    // ===== 抽象方法(子类必须实现)=====
    public abstract void sing();      // 不同鸟叫声不同

    public abstract void move();      // 不同鸟移动方式不同

    // ===== Getter/Setter =====
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

/**
 * 具体类:麻雀
 */
public class Sparrow extends Bird {

    public Sparrow(String name) {
        super(name, "灰褐色");
    }

    @Override
    public void sing() {
        System.out.println(name + ":叽叽喳喳~");
    }

    @Override
    public void move() {
        System.out.println(name + " 在天空飞翔");
    }
}

/**
 * 具体类:企鹅
 */
public class Penguin extends Bird {

    public Penguin(String name) {
        super(name, "黑白色");
    }

    @Override
    public void sing() {
        System.out.println(name + ":嘎嘎嘎~");
    }

    @Override
    public void move() {
        System.out.println(name + " 在地上摇摆行走");  // 企鹅不会飞
    }
}

5.2 接口示例

/**
 * 接口:飞行能力
 */
public interface Flyable {
    // 常量
    int MAX_ALTITUDE = 10000;  // 最大飞行高度

    // 抽象方法
    void fly();

    void land();

    // 默认方法(Java 8+)
    default void glide() {
        System.out.println("滑翔中...");
    }

    // 静态方法(Java 8+)
    static void showFlightRules() {
        System.out.println("飞行规则:注意安全!");
    }
}

/**
 * 接口:游泳能力
 */
public interface Swimmable {
    void swim();

    default void dive() {
        System.out.println("潜水中...");
    }
}

/**
 * 接口:可食用(标记接口)
 */
public interface Edible {
    String getHowToEat();
}

5.3 综合使用示例

/**
 * 野鸭类:继承抽象类 + 实现多个接口
 */
public class WildDuck extends Bird implements Flyable, Swimmable, Edible {

    public WildDuck(String name) {
        super(name, "绿头色");
    }

    // ===== 实现抽象类的抽象方法 =====
    @Override
    public void sing() {
        System.out.println(name + ":嘎嘎嘎~");
    }

    @Override
    public void move() {
        System.out.println(name + " 可以飞、游、走");
    }

    // ===== 实现 Flyable 接口 =====
    @Override
    public void fly() {
        System.out.println(name + " 展翅高飞,最高 " + MAX_ALTITUDE + " 米");
    }

    @Override
    public void land() {
        System.out.println(name + " 平稳降落");
    }

    // ===== 实现 Swimmable 接口 =====
    @Override
    public void swim() {
        System.out.println(name + " 在水中畅游");
    }

    // ===== 实现 Edible 接口 =====
    @Override
    public String getHowToEat() {
        return name + " 可以烤着吃";
    }
}

/**
 * 测试类
 */
public class Main {
    public static void main(String[] args) {
        WildDuck duck = new WildDuck("唐老鸭");

        // 调用继承的方法
        duck.showInfo();    // 鸟名:唐老鸭,颜色:绿头色
        duck.sleep();       // 唐老鸭 正在睡觉 Zzz...

        // 调用实现的方法
        duck.fly();         // 唐老鸭 展翅高飞,最高 10000 米
        duck.swim();        // 唐老鸭 在水中畅游
        duck.sing();        // 唐老鸭:嘎嘎嘎~

        // 调用默认方法
        duck.glide();       // 滑翔中...
        duck.dive();        // 潜水中...

        // 调用静态方法
        Flyable.showFlightRules();  // 飞行规则:注意安全!

        System.out.println(duck.getHowToEat());  // 唐老鸭 可以烤着吃

        // ===== 多态使用 =====
        System.out.println("\n===== 多态演示 =====");

        Bird bird = duck;        // 作为鸟
        bird.sing();

        Flyable flyer = duck;    // 作为飞行者
        flyer.fly();

        Swimmable swimmer = duck; // 作为游泳者
        swimmer.swim();
    }
}

输出结果

Bird 构造方法执行
鸟名:唐老鸭,颜色:绿头色
唐老鸭 正在睡觉 Zzz...
唐老鸭 展翅高飞,最高 10000 米
唐老鸭 在水中畅游
唐老鸭:嘎嘎嘎~
滑翔中...
潜水中...
飞行规则:注意安全!
唐老鸭 可以烤着吃

===== 多态演示 =====
唐老鸭:嘎嘎嘎~
唐老鸭 展翅高飞,最高 10000 米
唐老鸭 在水中畅游

5.4 类关系图

<mxfile>
  <diagram id="bird-class-diagram" name="完整类图">
    <mxGraphModel dx="1665" dy="750" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="850" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#334155;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="740" width="930" x="30" 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="50" width="860" x="50" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#93C5FD;fontStyle=2;" value="«abstract»" vertex="1">
          <mxGeometry height="20" width="150" x="375" y="85" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E3A8A;strokeColor=#3B82F6;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="180" width="250" x="325" y="105" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;" value="Bird" vertex="1">
          <mxGeometry height="35" width="250" x="325" y="105" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="line;strokeWidth=2;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#3B82F6;" value="" vertex="1">
          <mxGeometry height="8" width="250" x="325" y="140" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#93C5FD;fontFamily=Courier New;spacingLeft=10;" value="# name: String&lt;br&gt;# color: String" vertex="1">
          <mxGeometry height="40" width="250" x="325" y="148" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="line;strokeWidth=2;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#3B82F6;" value="" vertex="1">
          <mxGeometry height="8" width="250" x="325" y="188" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#93C5FD;fontFamily=Courier New;spacingLeft=10;" value="+ sleep(): void&lt;br&gt;+ showInfo(): void&lt;br&gt;&lt;i&gt;+ sing(): void {abstract}&lt;/i&gt;&lt;br&gt;&lt;i&gt;+ move(): void {abstract}&lt;/i&gt;" vertex="1">
          <mxGeometry height="85" width="250" x="325" y="196" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#0E7490;strokeColor=#22D3EE;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="130" width="180" x="60" y="380" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#22D3EE;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#0F172A;" value="Sparrow" vertex="1">
          <mxGeometry height="30" width="180" x="60" y="380" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="line;strokeWidth=2;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#22D3EE;" value="" vertex="1">
          <mxGeometry height="8" width="180" x="60" y="410" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#A5F3FC;fontFamily=Courier New;spacingLeft=10;" value="+ sing(): void&lt;br&gt;+ move(): void" vertex="1">
          <mxGeometry height="90" width="180" x="60" y="418" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#0E7490;strokeColor=#22D3EE;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="130" width="180" x="360" y="380" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=#22D3EE;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#0F172A;" value="Penguin" vertex="1">
          <mxGeometry height="30" width="180" x="360" y="380" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="line;strokeWidth=2;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#22D3EE;" value="" vertex="1">
          <mxGeometry height="8" width="180" x="360" y="410" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#A5F3FC;fontFamily=Courier New;spacingLeft=10;" value="+ sing(): void&lt;br&gt;+ move(): void" vertex="1">
          <mxGeometry height="90" width="180" x="360" y="418" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#0E7490;strokeColor=#22D3EE;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="130" width="180" x="660" y="380" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=#22D3EE;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#0F172A;" value="WildDuck" vertex="1">
          <mxGeometry height="30" width="180" x="660" y="380" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="line;strokeWidth=2;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#22D3EE;" value="" vertex="1">
          <mxGeometry height="8" width="180" x="660" y="410" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#A5F3FC;fontFamily=Courier New;spacingLeft=10;" value="+ sing(): void&lt;br&gt;+ move(): void&lt;br&gt;+ fly(): void&lt;br&gt;+ swim(): void&lt;br&gt;+ beEaten(): void" vertex="1">
          <mxGeometry height="90" width="180" x="660" y="418" as="geometry" />
        </mxCell>
        <mxCell id="23" edge="1" parent="1" source="11" style="endArrow=block;html=1;strokeColor=#3B82F6;strokeWidth=2;endFill=0;exitX=0.5;exitY=0;entryX=0.25;entryY=1;" target="5" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="24" edge="1" parent="1" source="15" style="endArrow=block;html=1;strokeColor=#3B82F6;strokeWidth=2;endFill=0;exitX=0.5;exitY=0;entryX=0.5;entryY=1;" target="5" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="25" edge="1" parent="1" source="19" style="endArrow=block;html=1;strokeColor=#3B82F6;strokeWidth=2;endFill=0;exitX=0.5;exitY=0;entryX=0.75;entryY=1;" target="5" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E3A8A;align=center;verticalAlign=middle;fontSize=11;fontColor=#93C5FD;rounded=1;" value="extends" vertex="1">
          <mxGeometry height="20" width="60" x="420" y="320" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#D8B4FE;fontStyle=2;" value="«interface»" vertex="1">
          <mxGeometry height="18" width="80" x="580" y="590" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#581C87;strokeColor=#A855F7;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="120" x="560" y="608" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="Flyable" vertex="1">
          <mxGeometry height="28" width="120" x="560" y="608" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="line;strokeWidth=2;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#A855F7;" value="" vertex="1">
          <mxGeometry height="8" width="120" x="560" y="636" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#D8B4FE;fontFamily=Courier New;spacingLeft=5;" value="+ fly(): void" vertex="1">
          <mxGeometry height="40" width="120" x="560" y="644" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#D8B4FE;fontStyle=2;" value="«interface»" vertex="1">
          <mxGeometry height="18" width="80" x="710" y="590" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#581C87;strokeColor=#A855F7;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="120" x="690" y="608" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="Swimmable" vertex="1">
          <mxGeometry height="28" width="120" x="690" y="608" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="line;strokeWidth=2;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#A855F7;" value="" vertex="1">
          <mxGeometry height="8" width="120" x="690" y="636" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#D8B4FE;fontFamily=Courier New;spacingLeft=5;" value="+ swim(): void" vertex="1">
          <mxGeometry height="40" width="120" x="690" y="644" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#D8B4FE;fontStyle=2;" value="«interface»" vertex="1">
          <mxGeometry height="18" width="80" x="840" y="590" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#581C87;strokeColor=#A855F7;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="120" x="820" y="608" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="Edible" vertex="1">
          <mxGeometry height="28" width="120" x="820" y="608" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="line;strokeWidth=2;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#A855F7;" value="" vertex="1">
          <mxGeometry height="8" width="120" x="820" y="636" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#D8B4FE;fontFamily=Courier New;spacingLeft=5;" value="+ beEaten(): void" vertex="1">
          <mxGeometry height="40" width="120" x="820" y="644" as="geometry" />
        </mxCell>
        <mxCell id="42" edge="1" parent="1" source="19" style="endArrow=block;html=1;strokeColor=#A855F7;strokeWidth=2;endFill=0;dashed=1;exitX=0.25;exitY=1;entryX=0.5;entryY=0;" target="28" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="43" edge="1" parent="1" source="19" style="endArrow=block;html=1;strokeColor=#A855F7;strokeWidth=2;endFill=0;dashed=1;exitX=0.5;exitY=1;entryX=0.5;entryY=0;" target="33" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="44" edge="1" parent="1" source="19" style="endArrow=block;html=1;strokeColor=#A855F7;strokeWidth=2;endFill=0;dashed=1;exitX=0.75;exitY=1;entryX=0.5;entryY=0;" target="38" value="">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=#581C87;align=center;verticalAlign=middle;fontSize=11;fontColor=#D8B4FE;rounded=1;" value="implements" vertex="1">
          <mxGeometry height="20" width="75" x="845" y="540" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="95" width="400" x="90" y="608" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📝 图例说明" vertex="1">
          <mxGeometry height="25" width="110" x="105" y="618" as="geometry" />
        </mxCell>
        <mxCell id="48" edge="1" parent="1" style="endArrow=block;html=1;strokeColor=#3B82F6;strokeWidth=2;endFill=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="115" y="663" as="sourcePoint" />
            <mxPoint x="165" y="663" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#93C5FD;" value="继承 (extends)" vertex="1">
          <mxGeometry height="20" width="100" x="175" y="653" as="geometry" />
        </mxCell>
        <mxCell id="50" edge="1" parent="1" style="endArrow=block;html=1;strokeColor=#A855F7;strokeWidth=2;endFill=0;dashed=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="115" y="688" as="sourcePoint" />
            <mxPoint x="165" y="688" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#D8B4FE;" value="实现 (implements)" vertex="1">
          <mxGeometry height="20" width="120" x="175" y="678" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#FCD34D;" value="# protected&amp;nbsp; &amp;nbsp;+ public&amp;nbsp; &amp;nbsp;- private" vertex="1">
          <mxGeometry height="20" width="170" x="310" y="653" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#FCD34D;fontStyle=2;" value="{abstract} 抽象方法" vertex="1">
          <mxGeometry height="20" width="150" x="310" y="678" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

六、Java 8+ 接口新特性

6.1 特性演进图

<mxfile>
  <diagram id="interface-evolution" name="接口特性演进">
    <mxGraphModel dx="1665" dy="750" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="900" 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="880" 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="50" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#64748B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="220" width="800" x="50" y="90" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#64748B;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Java 7 及以前" vertex="1">
          <mxGeometry height="35" width="160" x="65" y="105" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#475569;align=center;verticalAlign=middle;fontSize=12;fontColor=#CBD5E1;rounded=1;" value="基础版本" vertex="1">
          <mxGeometry height="25" width="80" x="235" y="110" 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="140" width="770" x="65" y="155" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontColor=#E2E8F0;spacingLeft=15;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;font color=&quot;#c084fc&quot;&gt;interface&lt;/font&gt; &lt;font color=&quot;#22d3ee&quot;&gt;MyInterface&lt;/font&gt; {&lt;/span&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="400" x="75" y="165" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="40" width="720" x="95" y="195" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#f472b6&quot;&gt;int&lt;/font&gt; &lt;font color=&quot;#fbbf24&quot;&gt;CONSTANT&lt;/font&gt; = &lt;font color=&quot;#a78bfa&quot;&gt;1&lt;/font&gt;;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="280" x="105" y="200" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#64748B;fontStyle=2;" value="// 常量 (public static final)" vertex="1">
          <mxGeometry height="30" width="240" x="560" y="200" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="40" width="720" x="95" y="245" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#f472b6&quot;&gt;void&lt;/font&gt; &lt;font color=&quot;#4ade80&quot;&gt;method&lt;/font&gt;();&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="280" x="105" y="250" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#64748B;fontStyle=2;" value="// 抽象方法 (public abstract)" vertex="1">
          <mxGeometry height="30" width="240" x="560" y="250" as="geometry" />
        </mxCell>
        <mxCell id="15" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#8B5CF6;strokeWidth=3;exitX=0.5;exitY=1;entryX=0.5;entryY=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="450" y="310" as="sourcePoint" />
            <mxPoint x="450" y="340" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#22C55E;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="230" width="800" x="50" y="350" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Java 8 新增" vertex="1">
          <mxGeometry height="35" width="140" x="65" y="365" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#166534;align=center;verticalAlign=middle;fontSize=12;fontColor=#BBF7D0;rounded=1;" value="重大更新 🎉" vertex="1">
          <mxGeometry height="25" width="90" x="215" y="370" 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="150" width="770" x="65" y="415" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontColor=#E2E8F0;spacingLeft=15;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;font color=&quot;#c084fc&quot;&gt;interface&lt;/font&gt; &lt;font color=&quot;#22d3ee&quot;&gt;MyInterface&lt;/font&gt; {&lt;/span&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="400" x="75" y="420" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontStyle=2;spacingLeft=35;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#64748b&quot;&gt;// ... 原有内容 ...&lt;/font&gt;" vertex="1">
          <mxGeometry height="20" width="200" x="75" y="445" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="40" width="720" x="95" y="470" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#4ade80&quot;&gt;default&lt;/font&gt; &lt;font color=&quot;#f472b6&quot;&gt;void&lt;/font&gt; &lt;font color=&quot;#fbbf24&quot;&gt;defaultMethod&lt;/font&gt;() { }&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="350" x="105" y="475" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#4ADE80;fontStyle=1;" value="✨ 默认方法(有方法体)" vertex="1">
          <mxGeometry height="30" width="240" x="560" y="475" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="40" width="720" x="95" y="515" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#4ade80&quot;&gt;static&lt;/font&gt; &lt;font color=&quot;#f472b6&quot;&gt;void&lt;/font&gt; &lt;font color=&quot;#fbbf24&quot;&gt;staticMethod&lt;/font&gt;() { }&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="350" x="105" y="520" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#4ADE80;fontStyle=1;" value="✨ 静态方法(接口直接调用)" vertex="1">
          <mxGeometry height="30" width="240" x="560" y="520" as="geometry" />
        </mxCell>
        <mxCell id="28" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#8B5CF6;strokeWidth=3;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="450" y="580" as="sourcePoint" />
            <mxPoint x="450" y="610" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="245" width="800" x="50" y="620" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Java 9 新增" vertex="1">
          <mxGeometry height="35" width="140" x="65" y="635" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#92400E;align=center;verticalAlign=middle;fontSize=12;fontColor=#FDE68A;rounded=1;" value="增强封装 🔒" vertex="1">
          <mxGeometry height="25" width="90" x="215" y="640" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="165" width="770" x="65" y="685" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontColor=#E2E8F0;spacingLeft=15;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;font color=&quot;#c084fc&quot;&gt;interface&lt;/font&gt; &lt;font color=&quot;#22d3ee&quot;&gt;MyInterface&lt;/font&gt; {&lt;/span&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="400" x="75" y="690" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontStyle=2;spacingLeft=35;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#64748b&quot;&gt;// ... 原有内容 ...&lt;/font&gt;" vertex="1">
          <mxGeometry height="20" width="200" x="75" y="715" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#78350F;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="40" width="720" x="95" y="740" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#fbbf24&quot;&gt;private&lt;/font&gt; &lt;font color=&quot;#f472b6&quot;&gt;void&lt;/font&gt; &lt;font color=&quot;#a78bfa&quot;&gt;helper&lt;/font&gt;() { }&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="350" x="105" y="745" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#FBBF24;fontStyle=1;" value="✨ 私有方法(内部复用)" vertex="1">
          <mxGeometry height="30" width="240" x="560" y="745" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#78350F;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="40" width="720" x="95" y="785" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#fbbf24&quot;&gt;private static&lt;/font&gt; &lt;font color=&quot;#f472b6&quot;&gt;void&lt;/font&gt; &lt;font color=&quot;#a78bfa&quot;&gt;util&lt;/font&gt;() { }&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="350" x="105" y="790" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#FBBF24;fontStyle=1;" value="✨ 私有静态方法" vertex="1">
          <mxGeometry height="30" width="240" x="560" y="790" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#E2E8F0;fontFamily=Courier New;spacingLeft=15;" value="}" vertex="1">
          <mxGeometry height="20" width="50" x="75" y="830" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#E2E8F0;fontFamily=Courier New;spacingLeft=15;" value="}" vertex="1">
          <mxGeometry height="20" width="50" x="75" y="540" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#E2E8F0;fontFamily=Courier New;spacingLeft=15;" value="}" vertex="1">
          <mxGeometry height="20" width="50" x="75" y="275" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

6.2 默认方法详解

/**
 * 默认方法示例
 */
public interface Collection {
    // 抽象方法
    int size();

    // Java 8 默认方法:提供默认实现
    default boolean isEmpty() {
        return size() == 0;  // 基于抽象方法实现
    }
}

/**
 * 实现类可以:
 * 1. 直接使用默认方法
 * 2. 重写默认方法
 */
public class MyList implements Collection {
    private int count = 0;

    @Override
    public int size() {
        return count;
    }

    // isEmpty() 直接继承,无需实现

    // 也可以选择重写
    // @Override
    // public boolean isEmpty() {
    //     return count <= 0;
    // }
}

6.3 默认方法冲突解决

interface A {
    default void hello() {
        System.out.println("Hello from A");
    }
}

interface B {
    default void hello() {
        System.out.println("Hello from B");
    }
}

// 实现类必须解决冲突
class C implements A, B {
    @Override
    public void hello() {
        // 方式1:自己实现
        System.out.println("Hello from C");

        // 方式2:选择调用某个接口的默认方法
        // A.super.hello();

        // 方式3:两个都调用
        // A.super.hello();
        // B.super.hello();
    }
}

冲突解决规则

<mxfile>
  <diagram id="default-method-conflict" name="默认方法冲突解决规则">
    <mxGraphModel dx="1665" dy="750" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="750" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" 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=#DC2626;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="⚖️ 默认方法冲突解决规则" vertex="1">
          <mxGeometry height="50" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=center;verticalAlign=middle;fontSize=16;fontColor=#94A3B8;rounded=1;strokeWidth=2;" value="优先级(从高到低)" vertex="1">
          <mxGeometry height="35" width="800" x="50" y="85" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#22C55E;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="175" width="800" x="50" y="135" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=28;fontColor=#FFFFFF;rounded=1;" value="1️⃣" vertex="1">
          <mxGeometry height="60" width="60" x="70" y="155" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#22C55E;" value="类中的方法优先级最高" vertex="1">
          <mxGeometry height="35" width="300" x="145" y="155" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#94A3B8;" value="如果类或父类中有具体方法,忽略接口默认方法" vertex="1">
          <mxGeometry height="25" width="400" x="145" y="190" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="760" x="70" y="225" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#c084fc&quot;&gt;class&lt;/font&gt; &lt;font color=&quot;#22d3ee&quot;&gt;MyClass&lt;/font&gt; &lt;font color=&quot;#c084fc&quot;&gt;implements&lt;/font&gt; &lt;font color=&quot;#fbbf24&quot;&gt;InterfaceA&lt;/font&gt; {&lt;br&gt;&amp;nbsp; &amp;nbsp; &lt;font color=&quot;#f472b6&quot;&gt;void&lt;/font&gt; &lt;font color=&quot;#4ade80&quot;&gt;method&lt;/font&gt;() { ... }&amp;nbsp; &lt;font color=&quot;#64748b&quot;&gt;// ✅ 类方法优先,忽略接口默认方法&lt;/font&gt;&lt;br&gt;}&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="740" x="80" y="230" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#3B82F6;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="175" width="800" x="50" y="325" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=28;fontColor=#FFFFFF;rounded=1;" value="2️⃣" vertex="1">
          <mxGeometry height="60" width="60" x="70" y="345" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#3B82F6;" value="子接口优先级高于父接口" vertex="1">
          <mxGeometry height="35" width="320" x="145" y="345" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#94A3B8;" value="如果 B extends A,则 B 的默认方法优先" vertex="1">
          <mxGeometry height="25" width="350" x="145" y="380" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A8A;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="760" x="70" y="415" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#c084fc&quot;&gt;interface&lt;/font&gt; &lt;font color=&quot;#fbbf24&quot;&gt;B&lt;/font&gt; &lt;font color=&quot;#c084fc&quot;&gt;extends&lt;/font&gt; &lt;font color=&quot;#94a3b8&quot;&gt;A&lt;/font&gt; { &lt;font color=&quot;#4ade80&quot;&gt;default&lt;/font&gt; &lt;font color=&quot;#f472b6&quot;&gt;void&lt;/font&gt; method() { ... } }&lt;br&gt;&lt;font color=&quot;#c084fc&quot;&gt;class&lt;/font&gt; &lt;font color=&quot;#22d3ee&quot;&gt;MyClass&lt;/font&gt; &lt;font color=&quot;#c084fc&quot;&gt;implements&lt;/font&gt; &lt;font color=&quot;#fbbf24&quot;&gt;B&lt;/font&gt; { }&amp;nbsp; &lt;font color=&quot;#64748b&quot;&gt;// ✅ 使用 B 的默认方法&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="740" x="80" y="420" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="200" width="800" x="50" y="515" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=28;fontColor=#FFFFFF;rounded=1;" value="3️⃣" vertex="1">
          <mxGeometry height="60" width="60" x="70" y="535" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#F59E0B;" value="无法判断时,必须显式覆盖" vertex="1">
          <mxGeometry height="35" width="350" x="145" y="535" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#94A3B8;" value="实现多个无继承关系的接口时,必须在类中重写" vertex="1">
          <mxGeometry height="25" width="400" x="145" y="570" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#78350F;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="95" width="760" x="70" y="605" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#c084fc&quot;&gt;class&lt;/font&gt; &lt;font color=&quot;#22d3ee&quot;&gt;MyClass&lt;/font&gt; &lt;font color=&quot;#c084fc&quot;&gt;implements&lt;/font&gt; &lt;font color=&quot;#fbbf24&quot;&gt;InterfaceA&lt;/font&gt;, &lt;font color=&quot;#fbbf24&quot;&gt;InterfaceB&lt;/font&gt; {&lt;br&gt;&amp;nbsp; &amp;nbsp; &lt;font color=&quot;#fb7185&quot;&gt;@Override&lt;/font&gt;&lt;br&gt;&amp;nbsp; &amp;nbsp; &lt;font color=&quot;#f472b6&quot;&gt;void&lt;/font&gt; &lt;font color=&quot;#4ade80&quot;&gt;method&lt;/font&gt;() {&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;font color=&quot;#fbbf24&quot;&gt;InterfaceA&lt;/font&gt;.&lt;font color=&quot;#c084fc&quot;&gt;super&lt;/font&gt;.method();&amp;nbsp; &lt;font color=&quot;#64748b&quot;&gt;// ⚠️ 必须显式选择或自定义实现&lt;/font&gt;&lt;br&gt;&amp;nbsp; &amp;nbsp; }&lt;br&gt;}&lt;/font&gt;" vertex="1">
          <mxGeometry height="90" width="740" x="80" y="608" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#166534;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="最高" vertex="1">
          <mxGeometry height="25" width="50" x="780" y="155" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#1D4ED8;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="中等" vertex="1">
          <mxGeometry height="25" width="50" x="780" y="345" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#B45309;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="手动" vertex="1">
          <mxGeometry height="25" width="50" x="780" y="535" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

七、使用场景

7.1 场景对比表

场景 选择 原因
定义一组相关类的通用模板 抽象类 可共享代码和状态
定义不相关类的共同行为 接口 无继承关系也可实现
需要多重继承能力 接口 类只能单继承,接口可多实现
需要定义常量 接口 接口常量默认 public static final
需要构造方法 抽象类 接口没有构造方法
需要成员变量(非常量) 抽象类 接口只能有常量
API 设计,需要向后兼容 接口+默认方法 可添加新方法不破坏实现类
定义规范(契约) 接口 接口天然就是契约

7.2 抽象类使用场景

<mxfile>
  <diagram id="abstract-class-scenarios" name="抽象类典型场景">
    <mxGraphModel dx="1479" dy="651" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="800" pageHeight="1150" 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="1110" width="760" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#1976d2;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=26;fontStyle=1;fontColor=#ffffff;" value="抽象类典型场景" vertex="1">
          <mxGeometry height="60" width="760" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="480" width="720" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#1976d2;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=18;fontStyle=1;fontColor=#ffffff;spacingLeft=15;" value="1️⃣ 模板方法模式" vertex="1">
          <mxGeometry height="45" width="720" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#bbdefb;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontColor=#1565c0;fontStyle=2;" value="定义算法骨架,子类实现具体步骤" vertex="1">
          <mxGeometry height="30" width="680" x="60" y="155" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="200" width="300" x="250" y="200" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#1976d2;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontStyle=1;fontColor=#ffffff;" value="«abstract»&#xa;DataProcessor" vertex="1">
          <mxGeometry height="50" width="300" x="250" y="200" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="line;strokeWidth=1;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#1976d2;" value="" vertex="1">
          <mxGeometry height="1" width="300" x="250" y="250" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=#e8f5e9;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontFamily=Consolas;fontColor=#2e7d32;spacingLeft=10;" value="+ process() : void «final»" vertex="1">
          <mxGeometry height="35" width="300" x="250" y="251" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="line;strokeWidth=1;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#1976d2;" value="" vertex="1">
          <mxGeometry height="1" width="300" x="250" y="286" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#ffebee;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontFamily=Consolas;fontColor=#c62828;fontStyle=2;spacingLeft=10;" value="# readData() : void «abstract»" vertex="1">
          <mxGeometry height="28" width="300" x="250" y="287" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#ffebee;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontFamily=Consolas;fontColor=#c62828;fontStyle=2;spacingLeft=10;" value="# processData() : void «abstract»" vertex="1">
          <mxGeometry height="28" width="300" x="250" y="315" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=#ffebee;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontFamily=Consolas;fontColor=#c62828;fontStyle=2;spacingLeft=10;" value="# writeData() : void «abstract»" vertex="1">
          <mxGeometry height="28" width="300" x="250" y="343" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=#4caf50;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=11;fontStyle=1;fontColor=#ffffff;" value="模板方法" vertex="1">
          <mxGeometry height="22" width="70" x="555" y="258" as="geometry" />
        </mxCell>
        <mxCell id="16" edge="1" parent="1" style="endArrow=none;html=1;rounded=0;strokeWidth=1;strokeColor=#4caf50;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="550" y="269" as="sourcePoint" />
            <mxPoint x="555" y="269" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#f44336;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=11;fontStyle=1;fontColor=#ffffff;" value="子类实现" vertex="1">
          <mxGeometry height="22" width="70" x="555" y="320" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#bdbdbd;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="145" width="680" x="60" y="420" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=#757575;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=13;fontStyle=1;fontColor=#ffffff;" value="process() 执行流程" vertex="1">
          <mxGeometry height="28" width="680" x="60" y="420" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#e57373;strokeWidth=2;fontSize=13;fontFamily=Consolas;fontColor=#c62828;" value="readData()" vertex="1">
          <mxGeometry height="40" width="120" x="90" y="470" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;fontColor=#757575;" value="步骤1" vertex="1">
          <mxGeometry height="20" width="120" x="90" y="510" as="geometry" />
        </mxCell>
        <mxCell id="22" edge="1" parent="1" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#9e9e9e;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="210" y="490" as="sourcePoint" />
            <mxPoint x="270" y="490" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#e57373;strokeWidth=2;fontSize=13;fontFamily=Consolas;fontColor=#c62828;" value="processData()" vertex="1">
          <mxGeometry height="40" width="130" x="270" y="470" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;fontColor=#757575;" value="步骤2" vertex="1">
          <mxGeometry height="20" width="130" x="270" y="510" as="geometry" />
        </mxCell>
        <mxCell id="25" edge="1" parent="1" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#9e9e9e;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="400" y="490" as="sourcePoint" />
            <mxPoint x="460" y="490" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#e57373;strokeWidth=2;fontSize=13;fontFamily=Consolas;fontColor=#c62828;" value="writeData()" vertex="1">
          <mxGeometry height="40" width="120" x="460" y="470" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;fontColor=#757575;" value="步骤3" vertex="1">
          <mxGeometry height="20" width="120" x="460" y="510" as="geometry" />
        </mxCell>
        <mxCell id="28" edge="1" parent="1" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#4caf50;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="580" y="490" as="sourcePoint" />
            <mxPoint x="640" y="490" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="29" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=#2e7d32;strokeWidth=2;fontSize=18;fontStyle=1;fontColor=#ffffff;" value="✓" vertex="1">
          <mxGeometry height="35" width="35" x="640" y="475" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#fb8c00;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="510" width="720" x="40" y="600" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#fb8c00;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=18;fontStyle=1;fontColor=#ffffff;spacingLeft=15;" value="2️⃣ 提供默认实现 + 部分抽象" vertex="1">
          <mxGeometry height="45" width="720" x="40" y="600" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#ffe0b2;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontColor=#e65100;fontStyle=2;" value="提供通用逻辑,子类按需重写特定方法" vertex="1">
          <mxGeometry height="30" width="680" x="60" y="655" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#fb8c00;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="220" width="400" x="200" y="700" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#fb8c00;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontStyle=1;fontColor=#ffffff;" value="«abstract»&#xa;HttpServlet" vertex="1">
          <mxGeometry height="50" width="400" x="200" y="700" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="line;strokeWidth=1;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#fb8c00;" value="" vertex="1">
          <mxGeometry height="1" width="400" x="200" y="750" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#e8f5e9;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontFamily=Consolas;fontColor=#2e7d32;spacingLeft=10;" value="+ service(req, res) : void" vertex="1">
          <mxGeometry height="32" width="400" x="200" y="751" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#4caf50;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=10;fontStyle=1;fontColor=#ffffff;" value="具体方法(路由逻辑)" vertex="1">
          <mxGeometry height="22" width="130" x="605" y="755" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="line;strokeWidth=1;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#fb8c00;" value="" vertex="1">
          <mxGeometry height="1" width="400" x="200" y="783" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=#e3f2fd;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontFamily=Consolas;fontColor=#1565c0;spacingLeft=10;" value="# doGet(req, res) : void { }" vertex="1">
          <mxGeometry height="32" width="400" x="200" y="784" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=#e3f2fd;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontFamily=Consolas;fontColor=#1565c0;spacingLeft=10;" value="# doPost(req, res) : void { }" vertex="1">
          <mxGeometry height="32" width="400" x="200" y="816" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=#e3f2fd;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontFamily=Consolas;fontColor=#1565c0;spacingLeft=10;" value="# doPut(req, res) : void { }" vertex="1">
          <mxGeometry height="32" width="400" x="200" y="848" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#e3f2fd;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontFamily=Consolas;fontColor=#1565c0;spacingLeft=10;" value="# doDelete(req, res) : void { }" vertex="1">
          <mxGeometry height="32" width="400" x="200" y="880" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=#2196f3;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=10;fontStyle=1;fontColor=#ffffff;" value="默认空实现(子类按需重写)" vertex="1">
          <mxGeometry height="22" width="145" x="605" y="830" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#bdbdbd;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="155" width="680" x="60" y="940" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=#757575;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=13;fontStyle=1;fontColor=#ffffff;" value="service() 路由逻辑" vertex="1">
          <mxGeometry height="28" width="680" x="60" y="940" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7c4dff;strokeColor=#651fff;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#ffffff;" value="Request" vertex="1">
          <mxGeometry height="40" width="90" x="75" y="1005" as="geometry" />
        </mxCell>
        <mxCell id="47" edge="1" parent="1" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#9e9e9e;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="165" y="1025" as="sourcePoint" />
            <mxPoint x="215" y="1025" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="48" parent="1" style="rhombus;whiteSpace=wrap;html=1;fillColor=#ffeb3b;strokeColor=#fbc02d;strokeWidth=2;fontSize=12;fontStyle=1;" value="method?" vertex="1">
          <mxGeometry height="55" width="90" x="215" y="1000" as="geometry" />
        </mxCell>
        <mxCell id="49" edge="1" parent="1" source="48" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#4caf50;exitX=1;exitY=0.25;exitDx=0;exitDy=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="335" y="1014" />
              <mxPoint x="335" y="995" />
            </Array>
            <mxPoint x="365" y="995" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=10;fontColor=#4caf50;fontStyle=1;" value="GET" vertex="1">
          <mxGeometry height="18" width="40" x="310" y="975" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#81c784;strokeWidth=2;fontSize=12;fontFamily=Consolas;fontColor=#2e7d32;" value="doGet()" vertex="1">
          <mxGeometry height="35" width="80" x="365" y="975" as="geometry" />
        </mxCell>
        <mxCell id="52" edge="1" parent="1" source="48" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#2196f3;exitX=1;exitY=0.75;exitDx=0;exitDy=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="335" y="1041" />
              <mxPoint x="335" y="1055" />
            </Array>
            <mxPoint x="365" y="1055" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="53" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=10;fontColor=#2196f3;fontStyle=1;" value="POST" vertex="1">
          <mxGeometry height="18" width="40" x="310" y="1060" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#64b5f6;strokeWidth=2;fontSize=12;fontFamily=Consolas;fontColor=#1565c0;" value="doPost()" vertex="1">
          <mxGeometry height="35" width="80" x="365" y="1035" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#9e9e9e;fontStyle=1;" value="..." vertex="1">
          <mxGeometry height="25" width="40" x="385" y="1070" as="geometry" />
        </mxCell>
        <mxCell id="56" edge="1" parent="1" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#9e9e9e;" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="485" y="993" />
              <mxPoint x="485" y="1025" />
            </Array>
            <mxPoint x="445" y="993" as="sourcePoint" />
            <mxPoint x="525" y="1025" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="57" edge="1" parent="1" style="endArrow=none;html=1;rounded=0;strokeWidth=2;strokeColor=#9e9e9e;" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="485" y="1053" />
            </Array>
            <mxPoint x="445" y="1053" as="sourcePoint" />
            <mxPoint x="485" y="1025" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="58" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7c4dff;strokeColor=#651fff;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#ffffff;" value="Response" vertex="1">
          <mxGeometry height="40" width="90" x="525" y="1005" as="geometry" />
        </mxCell>
        <mxCell id="59" edge="1" parent="1" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#9e9e9e;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="615" y="1025" as="sourcePoint" />
            <mxPoint x="665" y="1025" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="60" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#43a047;strokeColor=#2e7d32;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#ffffff;" value="Client" vertex="1">
          <mxGeometry height="40" width="60" x="665" y="1005" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

7.3 接口使用场景

<mxfile>
  <diagram id="interface-scenarios" name="接口典型场景">
    <mxGraphModel dx="900" dy="1400" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="1400">
      <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="1360" as="geometry" />
        </mxCell>
        <mxCell id="3" value="🎯 接口典型场景" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="20" y="20" width="860" height="50" as="geometry" />
        </mxCell>
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#22C55E;strokeWidth=3;" vertex="1" parent="1">
          <mxGeometry x="50" y="90" width="800" height="330" as="geometry" />
        </mxCell>
        <mxCell id="5" value="1️⃣" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=24;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="70" y="110" width="50" height="50" as="geometry" />
        </mxCell>
        <mxCell id="6" value="定义能力/行为规范" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#22C55E;" vertex="1" parent="1">
          <mxGeometry x="135" y="115" width="250" height="40" as="geometry" />
        </mxCell>
        <mxCell id="7" value="规范类应具备的能力" style="text;html=1;strokeColor=none;fillColor=#166534;align=center;verticalAlign=middle;fontSize=12;fontColor=#BBF7D0;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="700" y="120" width="130" height="30" as="geometry" />
        </mxCell>
        <mxCell id="8" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="70" y="170" width="760" height="235" as="geometry" />
        </mxCell>
        <mxCell id="9" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#64748b&quot;&gt;// 功能接口 - 定义比较能力&lt;/font&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="80" y="180" width="350" height="25" as="geometry" />
        </mxCell>
        <mxCell id="10" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#22C55E;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="90" y="210" width="720" height="55" as="geometry" />
        </mxCell>
        <mxCell id="11" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#c084fc&quot;&gt;interface&lt;/font&gt; &lt;font color=&quot;#22d3ee&quot;&gt;Comparable&lt;/font&gt;&lt;font color=&quot;#fbbf24&quot;&gt;&amp;lt;T&amp;gt;&lt;/font&gt; {&lt;br&gt;&amp;nbsp; &amp;nbsp; &lt;font color=&quot;#f472b6&quot;&gt;int&lt;/font&gt; &lt;font color=&quot;#4ade80&quot;&gt;compareTo&lt;/font&gt;(&lt;font color=&quot;#fbbf24&quot;&gt;T&lt;/font&gt; o);&amp;nbsp; &lt;font color=&quot;#64748b&quot;&gt;// 定义比较能力&lt;/font&gt;&lt;br&gt;}&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="100" y="215" width="700" height="50" as="geometry" />
        </mxCell>
        <mxCell id="12" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#64748b&quot;&gt;// 标记接口 - 表示类具有某种特性&lt;/font&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="80" y="275" width="400" height="25" as="geometry" />
        </mxCell>
        <mxCell id="13" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#3B82F6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="90" y="305" width="350" height="45" as="geometry" />
        </mxCell>
        <mxCell id="14" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#c084fc&quot;&gt;interface&lt;/font&gt; &lt;font color=&quot;#22d3ee&quot;&gt;Serializable&lt;/font&gt; {&lt;br&gt;}&amp;nbsp; &lt;font color=&quot;#64748b&quot;&gt;// 可序列化&lt;/font&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="100" y="308" width="330" height="40" as="geometry" />
        </mxCell>
        <mxCell id="15" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A5F;strokeColor=#3B82F6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="460" y="305" width="350" height="45" as="geometry" />
        </mxCell>
        <mxCell id="16" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#c084fc&quot;&gt;interface&lt;/font&gt; &lt;font color=&quot;#22d3ee&quot;&gt;Cloneable&lt;/font&gt; {&lt;br&gt;}&amp;nbsp; &lt;font color=&quot;#64748b&quot;&gt;// 可克隆&lt;/font&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="470" y="308" width="330" height="40" as="geometry" />
        </mxCell>
        <mxCell id="17" value="💡 标记接口没有方法,仅用于标识类的特性" style="text;html=1;strokeColor=none;fillColor=#1E3A5F;align=center;verticalAlign=middle;fontSize=12;fontColor=#93C5FD;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="90" y="360" width="720" height="30" as="geometry" />
        </mxCell>
        <mxCell id="18" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#3B82F6;strokeWidth=3;" vertex="1" parent="1">
          <mxGeometry x="50" y="440" width="800" height="430" as="geometry" />
        </mxCell>
        <mxCell id="19" value="2️⃣" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=24;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="70" y="460" width="50" height="50" as="geometry" />
        </mxCell>
        <mxCell id="20" value="解耦与依赖注入" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#3B82F6;" vertex="1" parent="1">
          <mxGeometry x="135" y="465" width="220" height="40" as="geometry" />
        </mxCell>
        <mxCell id="21" value="面向接口编程" style="text;html=1;strokeColor=none;fillColor=#1D4ED8;align=center;verticalAlign=middle;fontSize=12;fontColor=#BFDBFE;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="700" y="470" width="130" height="30" as="geometry" />
        </mxCell>
        <mxCell id="22" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="70" y="520" width="760" height="335" as="geometry" />
        </mxCell>
        <mxCell id="23" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#64748b&quot;&gt;// 1. 定义接口&lt;/font&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="80" y="530" width="200" height="25" as="geometry" />
        </mxCell>
        <mxCell id="24" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A8A;strokeColor=#3B82F6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="90" y="560" width="720" height="70" as="geometry" />
        </mxCell>
        <mxCell id="25" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#c084fc&quot;&gt;interface&lt;/font&gt; &lt;font color=&quot;#22d3ee&quot;&gt;UserRepository&lt;/font&gt; {&lt;br&gt;&amp;nbsp; &amp;nbsp; &lt;font color=&quot;#fbbf24&quot;&gt;User&lt;/font&gt; &lt;font color=&quot;#4ade80&quot;&gt;findById&lt;/font&gt;(&lt;font color=&quot;#f472b6&quot;&gt;Long&lt;/font&gt; id);&lt;br&gt;&amp;nbsp; &amp;nbsp; &lt;font color=&quot;#f472b6&quot;&gt;void&lt;/font&gt; &lt;font color=&quot;#4ade80&quot;&gt;save&lt;/font&gt;(&lt;font color=&quot;#fbbf24&quot;&gt;User&lt;/font&gt; user);&lt;br&gt;}&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="100" y="562" width="700" height="68" as="geometry" />
        </mxCell>
        <mxCell id="26" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#64748b&quot;&gt;// 2. 多种实现&lt;/font&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="80" y="640" width="200" height="25" as="geometry" />
        </mxCell>
        <mxCell id="27" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#22C55E;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="90" y="670" width="230" height="40" as="geometry" />
        </mxCell>
        <mxCell id="28" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#22d3ee&quot;&gt;MySqlUserRepository&lt;/font&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;" vertex="1" parent="1">
          <mxGeometry x="90" y="675" width="230" height="30" as="geometry" />
        </mxCell>
        <mxCell id="29" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#78350F;strokeColor=#F59E0B;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="335" y="670" width="230" height="40" as="geometry" />
        </mxCell>
        <mxCell id="30" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#22d3ee&quot;&gt;MongoUserRepository&lt;/font&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;" vertex="1" parent="1">
          <mxGeometry x="335" y="675" width="230" height="30" as="geometry" />
        </mxCell>
        <mxCell id="31" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#581C87;strokeColor=#A855F7;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="580" y="670" width="230" height="40" as="geometry" />
        </mxCell>
        <mxCell id="32" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#22d3ee&quot;&gt;MockUserRepository&lt;/font&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;" vertex="1" parent="1">
          <mxGeometry x="580" y="675" width="230" height="30" as="geometry" />
        </mxCell>
        <mxCell id="33" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#64748b&quot;&gt;// 3. 使用方只依赖接口&lt;/font&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="80" y="720" width="250" height="25" as="geometry" />
        </mxCell>
        <mxCell id="34" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A8A;strokeColor=#3B82F6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="90" y="750" width="720" height="55" as="geometry" />
        </mxCell>
        <mxCell id="35" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#c084fc&quot;&gt;class&lt;/font&gt; &lt;font color=&quot;#22d3ee&quot;&gt;UserService&lt;/font&gt; {&lt;br&gt;&amp;nbsp; &amp;nbsp; &lt;font color=&quot;#c084fc&quot;&gt;private&lt;/font&gt; &lt;font color=&quot;#fbbf24&quot;&gt;UserRepository&lt;/font&gt; repo;&amp;nbsp; &lt;font color=&quot;#64748b&quot;&gt;// ✅ 面向接口编程,可注入任意实现&lt;/font&gt;&lt;br&gt;}&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="100" y="752" width="700" height="53" as="geometry" />
        </mxCell>
        <mxCell id="36" value="💡 切换实现无需修改业务代码,便于测试和扩展" style="text;html=1;strokeColor=none;fillColor=#1E3A5F;align=center;verticalAlign=middle;fontSize=12;fontColor=#93C5FD;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="90" y="815" width="720" height="30" as="geometry" />
        </mxCell>
        <mxCell id="37" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=3;" vertex="1" parent="1">
          <mxGeometry x="50" y="890" width="800" height="475" as="geometry" />
        </mxCell>
        <mxCell id="38" value="3️⃣" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=24;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="70" y="910" width="50" height="50" as="geometry" />
        </mxCell>
        <mxCell id="39" value="回调机制" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#F59E0B;" vertex="1" parent="1">
          <mxGeometry x="135" y="915" width="150" height="40" as="geometry" />
        </mxCell>
        <mxCell id="40" value="事件驱动编程" style="text;html=1;strokeColor=none;fillColor=#B45309;align=center;verticalAlign=middle;fontSize=12;fontColor=#FDE68A;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="700" y="920" width="130" height="30" as="geometry" />
        </mxCell>
        <mxCell id="41" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="70" y="970" width="760" height="380" as="geometry" />
        </mxCell>
        <mxCell id="42" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#64748b&quot;&gt;// 定义回调接口&lt;/font&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="80" y="980" width="200" height="25" as="geometry" />
        </mxCell>
        <mxCell id="43" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#78350F;strokeColor=#F59E0B;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="90" y="1010" width="720" height="55" as="geometry" />
        </mxCell>
        <mxCell id="44" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#c084fc&quot;&gt;interface&lt;/font&gt; &lt;font color=&quot;#22d3ee&quot;&gt;OnClickListener&lt;/font&gt; {&lt;br&gt;&amp;nbsp; &amp;nbsp; &lt;font color=&quot;#f472b6&quot;&gt;void&lt;/font&gt; &lt;font color=&quot;#4ade80&quot;&gt;onClick&lt;/font&gt;(&lt;font color=&quot;#fbbf24&quot;&gt;View&lt;/font&gt; v);&lt;br&gt;}&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="100" y="1012" width="700" height="53" as="geometry" />
        </mxCell>
        <mxCell id="45" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#64748b&quot;&gt;// 匿名内部类方式&lt;/font&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="80" y="1075" width="200" height="25" as="geometry" />
        </mxCell>
        <mxCell id="46" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#78350F;strokeColor=#F59E0B;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="90" y="1105" width="720" height="110" as="geometry" />
        </mxCell>
        <mxCell id="47" value="&lt;font face=&quot;Courier New&quot;&gt;button.&lt;font color=&quot;#4ade80&quot;&gt;setOnClickListener&lt;/font&gt;(&lt;font color=&quot;#c084fc&quot;&gt;new&lt;/font&gt; &lt;font color=&quot;#22d3ee&quot;&gt;OnClickListener&lt;/font&gt;() {&lt;br&gt;&amp;nbsp; &amp;nbsp; &lt;font color=&quot;#fb7185&quot;&gt;@Override&lt;/font&gt;&lt;br&gt;&amp;nbsp; &amp;nbsp; &lt;font color=&quot;#c084fc&quot;&gt;public&lt;/font&gt; &lt;font color=&quot;#f472b6&quot;&gt;void&lt;/font&gt; &lt;font color=&quot;#4ade80&quot;&gt;onClick&lt;/font&gt;(&lt;font color=&quot;#fbbf24&quot;&gt;View&lt;/font&gt; v) {&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;font color=&quot;#64748b&quot;&gt;// 处理点击事件&lt;/font&gt;&lt;br&gt;&amp;nbsp; &amp;nbsp; }&lt;br&gt;});&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="100" y="1107" width="700" height="108" as="geometry" />
        </mxCell>
        <mxCell id="48" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#64748b&quot;&gt;// Java 8 Lambda 简化&lt;/font&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="80" y="1225" width="250" height="25" as="geometry" />
        </mxCell>
        <mxCell id="49" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14532D;strokeColor=#22C55E;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="90" y="1255" width="720" height="40" as="geometry" />
        </mxCell>
        <mxCell id="50" value="&lt;font face=&quot;Courier New&quot;&gt;button.&lt;font color=&quot;#4ade80&quot;&gt;setOnClickListener&lt;/font&gt;(v -&amp;gt; { &lt;font color=&quot;#64748b&quot;&gt;/* 处理点击 */&lt;/font&gt; });&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=15;" vertex="1" parent="1">
          <mxGeometry x="100" y="1260" width="700" height="30" as="geometry" />
        </mxCell>
        <mxCell id="51" value="💡 函数式接口(只有一个抽象方法)可用 Lambda 简化" style="text;html=1;strokeColor=none;fillColor=#14532D;align=center;verticalAlign=middle;fontSize=12;fontColor=#BBF7D0;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="90" y="1305" width="720" height="30" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

7.4 JDK 中的典型例子

类型 例子 说明
抽象类 AbstractList List 的骨架实现,提供通用方法
AbstractMap Map 的骨架实现
InputStream 输入流基类
HttpServlet Servlet 模板类
接口 List / Set / Map 集合规范
Comparable 比较能力
Serializable 序列化标记
Runnable / Callable 任务定义
Iterator 迭代器规范

7.5 选择决策流程图

<mxfile>
  <diagram id="abstract-vs-interface-decision" name="如何选择抽象类vs接口">
    <mxGraphModel dx="1388" dy="625" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="1100" 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="1080" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#7C3AED;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🤔 如何选择抽象类 vs 接口?" vertex="1">
          <mxGeometry height="50" width="860" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E3A8A;strokeColor=#3B82F6;strokeWidth=3;fontSize=14;fontColor=#FFFFFF;fontStyle=1;" value="需要定义一组类的&#xa;通用行为?" vertex="1">
          <mxGeometry height="60" width="200" x="350" y="90" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="YES" vertex="1">
          <mxGeometry height="20" width="40" x="460" y="160" as="geometry" />
        </mxCell>
        <mxCell id="6" edge="1" parent="1" source="4" style="endArrow=classic;html=1;strokeColor=#64748B;strokeWidth=2;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="7" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="450" y="180" as="sourcePoint" />
            <mxPoint x="450" y="210" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="7" parent="1" style="rhombus;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#8B5CF6;strokeWidth=3;fontSize=14;fontColor=#FFFFFF;fontStyle=1;" value="这些类有没有&#xa;共同的父类?" vertex="1">
          <mxGeometry height="100" width="220" x="340" y="190" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="YES 有" vertex="1">
          <mxGeometry height="20" width="50" x="230" y="270" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="NO 无" vertex="1">
          <mxGeometry height="20" width="50" x="620" y="270" as="geometry" />
        </mxCell>
        <mxCell id="10" edge="1" parent="1" source="7" style="endArrow=classic;html=1;strokeColor=#22C55E;strokeWidth=2;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="11" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="200" y="240" />
            </Array>
            <mxPoint x="280" y="240" as="sourcePoint" />
            <mxPoint x="200" y="320" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="11" parent="1" style="rhombus;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#F59E0B;strokeWidth=3;fontSize=14;fontColor=#FFFFFF;fontStyle=1;" value="需要共享&#xa;代码/状态?" vertex="1">
          <mxGeometry height="100" width="200" x="100" y="310" as="geometry" />
        </mxCell>
        <mxCell id="12" edge="1" parent="1" source="7" style="endArrow=classic;html=1;strokeColor=#EF4444;strokeWidth=2;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="13" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="700" y="240" />
            </Array>
            <mxPoint x="560" y="240" as="sourcePoint" />
            <mxPoint x="700" y="320" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="13" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0E7490;strokeColor=#22D3EE;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="150" width="260" x="570" y="310" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=#22D3EE;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#0F172A;rounded=1;" value="使用【接口】" vertex="1">
          <mxGeometry height="35" width="160" x="620" y="325" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#E2E8F0;spacingLeft=10;" value="• 定义行为规范&#xa;• 可多实现&#xa;• 用于不相关的类" vertex="1">
          <mxGeometry height="80" width="220" x="590" y="370" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="YES 是" vertex="1">
          <mxGeometry height="20" width="50" x="80" y="420" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="NO 否" vertex="1">
          <mxGeometry height="20" width="50" x="300" y="420" as="geometry" />
        </mxCell>
        <mxCell id="18" edge="1" parent="1" source="11" style="endArrow=classic;html=1;strokeColor=#22C55E;strokeWidth=2;exitX=0;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="19" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points" />
            <mxPoint x="140" y="410" as="sourcePoint" />
            <mxPoint x="140" y="480" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7C2D12;strokeColor=#F97316;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="150" width="180" x="30" y="480" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=#F97316;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="使用【抽象类】" vertex="1">
          <mxGeometry height="30" width="150" x="45" y="495" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=10;" value="• 模板方法模式&#xa;• 共享状态/代码&#xa;• is-a 关系" vertex="1">
          <mxGeometry height="85" width="160" x="40" y="535" as="geometry" />
        </mxCell>
        <mxCell id="22" edge="1" parent="1" source="11" style="endArrow=classic;html=1;strokeColor=#EF4444;strokeWidth=2;exitX=1;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="23" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points" />
            <mxPoint x="260" y="410" as="sourcePoint" />
            <mxPoint x="330" y="470" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0E7490;strokeColor=#22D3EE;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="150" width="180" x="230" y="480" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#22D3EE;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#0F172A;rounded=1;" value="使用【接口】" vertex="1">
          <mxGeometry height="30" width="150" x="245" y="495" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#E2E8F0;spacingLeft=10;" value="• 只需规范行为&#xa;• 无需共享代码" vertex="1">
          <mxGeometry height="85" width="160" x="240" y="535" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=none;strokeColor=#475569;strokeWidth=2;dashed=1;" value="" vertex="1">
          <mxGeometry height="400" width="800" x="50" y="660" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#4C1D95;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💡 现代 Java 最佳实践" vertex="1">
          <mxGeometry height="40" width="800" x="50" y="660" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#8B5CF6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="60" width="740" x="80" y="720" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#A78BFA;" value="⭐ 优先使用接口,必要时用抽象类补充" vertex="1">
          <mxGeometry height="40" width="740" x="80" y="730" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=#475569;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#E2E8F0;rounded=1;" value="设计模式" vertex="1">
          <mxGeometry height="30" width="740" x="80" y="800" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0E7490;strokeColor=#22D3EE;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="接口定义能力" vertex="1">
          <mxGeometry height="50" width="180" x="100" y="850" as="geometry" />
        </mxCell>
        <mxCell id="32" edge="1" parent="1" source="31" style="endArrow=classic;html=1;strokeColor=#64748B;strokeWidth=3;entryX=0;entryY=0.5;entryDx=0;entryDy=0;exitX=1;exitY=0.5;exitDx=0;exitDy=0;" target="33" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="290" y="875" as="sourcePoint" />
            <mxPoint x="350" y="875" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7C2D12;strokeColor=#F97316;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="抽象类提供&#xa;骨架实现" vertex="1">
          <mxGeometry height="50" width="180" x="330" y="850" as="geometry" />
        </mxCell>
        <mxCell id="34" edge="1" parent="1" source="33" style="endArrow=classic;html=1;strokeColor=#64748B;strokeWidth=3;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">
            <mxPoint x="520" y="875" as="sourcePoint" />
            <mxPoint x="580" y="875" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#166534;strokeColor=#22C55E;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="具体类&#xa;完成实现" vertex="1">
          <mxGeometry height="50" width="180" x="560" y="850" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#475569;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#E2E8F0;rounded=1;" value="实际案例" vertex="1">
          <mxGeometry height="30" width="740" x="80" y="920" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="740" x="80" y="960" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0E7490;strokeColor=#22D3EE;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#22d3ee&quot;&gt;List&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="140" x="110" y="980" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#94A3B8;" value="接口" vertex="1">
          <mxGeometry height="15" width="60" x="150" y="1020" as="geometry" />
        </mxCell>
        <mxCell id="40" edge="1" parent="1" source="38" style="endArrow=classic;html=1;strokeColor=#64748B;strokeWidth=2;entryX=0;entryY=0.5;entryDx=0;entryDy=0;exitX=1;exitY=0.5;exitDx=0;exitDy=0;" target="41" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="260" y="1000" as="sourcePoint" />
            <mxPoint x="320" y="1000" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="41" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7C2D12;strokeColor=#F97316;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#fbbf24&quot;&gt;AbstractList&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="180" x="310" y="980" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#94A3B8;" value="抽象类" vertex="1">
          <mxGeometry height="15" width="60" x="370" y="1020" as="geometry" />
        </mxCell>
        <mxCell id="43" edge="1" parent="1" source="41" style="endArrow=classic;html=1;strokeColor=#64748B;strokeWidth=2;entryX=0;entryY=0.5;entryDx=0;entryDy=0;exitX=1;exitY=0.5;exitDx=0;exitDy=0;" target="44" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="500" y="1000" as="sourcePoint" />
            <mxPoint x="560" y="1000" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="44" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#166534;strokeColor=#22C55E;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#4ade80&quot;&gt;ArrayList&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="160" x="550" y="980" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#94A3B8;" value="实现类" vertex="1">
          <mxGeometry height="15" width="60" x="600" y="1020" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

八、核心总结

8.1 一句话总结

<mxfile>
  <diagram id="core-difference" name="核心区别">
    <mxGraphModel dx="1479" dy="651" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="800" pageHeight="720" 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="690" width="760" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#5c6bc0;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=28;fontStyle=1;fontColor=#ffffff;" value="核心区别" vertex="1">
          <mxGeometry height="60" width="760" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="260" width="720" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#1976d2;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=20;fontStyle=1;fontColor=#ffffff;spacingLeft=20;" value="抽象类:我是什么 (is-a)" vertex="1">
          <mxGeometry height="50" width="720" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#1976d2;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#1976d2;" value="is-a" vertex="1">
          <mxGeometry height="35" width="60" x="680" y="108" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="175" width="400" x="60" y="165" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=#0d47a1;strokeWidth=2;fontSize=15;fontStyle=1;fontColor=#ffffff;" value="«abstract»&#xa;Animal" vertex="1">
          <mxGeometry height="50" width="120" x="200" y="180" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=#2e7d32;strokeWidth=2;fontSize=15;fontStyle=1;fontColor=#ffffff;" value="Dog" vertex="1">
          <mxGeometry height="45" width="120" x="200" y="280" as="geometry" />
        </mxCell>
        <mxCell id="10" edge="1" parent="1" style="endArrow=block;endFill=0;html=1;rounded=0;strokeWidth=3;strokeColor=#1976d2;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="260" y="280" as="sourcePoint" />
            <mxPoint x="260" y="230" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#1565c0;fontStyle=2;" value="extends" vertex="1">
          <mxGeometry height="20" width="60" x="265" y="245" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#e8f5e9;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontFamily=Consolas;fontColor=#2e7d32;fontStyle=1;" value="class Dog extends Animal" vertex="1">
          <mxGeometry height="55" width="100" x="80" y="245" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#bbdefb;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontColor=#1565c0;fontStyle=1;" value="Dog is an Animal" vertex="1">
          <mxGeometry height="30" width="105" x="340" y="255" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#64b5f6;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="175" width="260" x="480" y="165" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=#1976d2;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontStyle=1;fontColor=#ffffff;" value="重点" vertex="1">
          <mxGeometry height="30" width="260" x="480" y="165" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=#4caf50;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontStyle=1;fontColor=#ffffff;" value="✓" vertex="1">
          <mxGeometry height="25" width="25" x="500" y="205" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#1565c0;fontStyle=1;" value="继承关系" vertex="1">
          <mxGeometry height="25" width="180" x="535" y="205" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#4caf50;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontStyle=1;fontColor=#ffffff;" value="✓" vertex="1">
          <mxGeometry height="25" width="25" x="500" y="245" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#1565c0;fontStyle=1;" value="代码复用" vertex="1">
          <mxGeometry height="25" width="180" x="535" y="245" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=#4caf50;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontStyle=1;fontColor=#ffffff;" value="✓" vertex="1">
          <mxGeometry height="25" width="25" x="500" y="285" 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=15;fontColor=#1565c0;fontStyle=1;" value="共享状态" vertex="1">
          <mxGeometry height="25" width="180" x="535" y="285" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#9c27b0;strokeColor=#7b1fa2;strokeWidth=3;fontSize=20;fontStyle=1;fontColor=#ffffff;" value="VS" vertex="1">
          <mxGeometry height="50" width="70" x="365" y="370" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#fb8c00;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="250" width="720" x="40" y="430" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#fb8c00;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=20;fontStyle=1;fontColor=#ffffff;spacingLeft=20;" value="接口:我能做什么 (can-do / has-a)" vertex="1">
          <mxGeometry height="50" width="720" x="40" y="430" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#fb8c00;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#fb8c00;" value="can-do" vertex="1">
          <mxGeometry height="35" width="70" x="670" y="438" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="165" width="400" x="60" y="495" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fb8c00;strokeColor=#e65100;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#ffffff;" value="«interface»&#xa;Flyable" vertex="1">
          <mxGeometry height="45" width="100" x="100" y="510" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fb8c00;strokeColor=#e65100;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#ffffff;" value="«interface»&#xa;Swimmable" vertex="1">
          <mxGeometry height="45" width="100" x="320" y="510" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=#2e7d32;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#ffffff;" value="Duck" vertex="1">
          <mxGeometry height="45" width="130" x="195" y="605" as="geometry" />
        </mxCell>
        <mxCell id="30" edge="1" parent="1" style="endArrow=block;endFill=0;html=1;rounded=0;strokeWidth=2;strokeColor=#fb8c00;dashed=1;dashPattern=8 8;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="230" y="605" as="sourcePoint" />
            <mxPoint x="150" y="555" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=10;fontColor=#e65100;fontStyle=2;" value="implements" vertex="1">
          <mxGeometry height="18" width="65" x="145" y="570" as="geometry" />
        </mxCell>
        <mxCell id="32" edge="1" parent="1" style="endArrow=block;endFill=0;html=1;rounded=0;strokeWidth=2;strokeColor=#fb8c00;dashed=1;dashPattern=8 8;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="290" y="605" as="sourcePoint" />
            <mxPoint x="370" y="555" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=10;fontColor=#e65100;fontStyle=2;" value="implements" vertex="1">
          <mxGeometry height="18" width="65" x="305" y="570" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#c8e6c9;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=12;fontColor=#2e7d32;fontStyle=1;" value="Duck can Fly" vertex="1">
          <mxGeometry height="25" width="90" x="90" y="588" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=#c8e6c9;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=12;fontColor=#2e7d32;fontStyle=1;" value="Duck can Swim" vertex="1">
          <mxGeometry height="25" width="100" x="335" y="588" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffe0b2;strokeColor=#ffb74d;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="165" width="260" x="480" y="495" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#fb8c00;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontStyle=1;fontColor=#ffffff;" value="重点" vertex="1">
          <mxGeometry height="30" width="260" x="480" y="495" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=#4caf50;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontStyle=1;fontColor=#ffffff;" value="✓" vertex="1">
          <mxGeometry height="25" width="25" x="500" y="535" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#e65100;fontStyle=1;" value="能力定义" vertex="1">
          <mxGeometry height="25" width="180" x="535" y="535" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=#4caf50;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontStyle=1;fontColor=#ffffff;" value="✓" vertex="1">
          <mxGeometry height="25" width="25" x="500" y="575" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#e65100;fontStyle=1;" value="行为规范" vertex="1">
          <mxGeometry height="25" width="180" x="535" y="575" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#4caf50;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontStyle=1;fontColor=#ffffff;" value="✓" vertex="1">
          <mxGeometry height="25" width="25" x="500" y="615" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#e65100;fontStyle=1;" value="多态扩展" vertex="1">
          <mxGeometry height="25" width="180" x="535" y="615" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

8.2 速查对比表

对比维度 抽象类 接口
关键字 abstract class interface
继承/实现 单继承 多实现
成员变量 任意 只有常量
构造方法 ✅ 有 ❌ 无
方法类型 具体+抽象 抽象+default+static
设计理念 模板(is-a) 规范(can-do)
典型场景 模板方法、共享代码 定义能力、解耦

8.3 记忆口诀

<mxfile>
  <diagram id="memory-tips" name="记忆口诀">
    <mxGraphModel dx="2145" dy="944" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="800" pageHeight="850" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#424242;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="830" width="760" x="20" y="20" 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=28;fontStyle=1;fontColor=#ffffff;" value="📝 记忆口诀" vertex="1">
          <mxGeometry height="60" width="760" x="20" y="20" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="230" width="340" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#1976d2;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=18;fontStyle=1;fontColor=#ffffff;" value="🔷 抽象类记忆" vertex="1">
          <mxGeometry height="45" width="340" x="40" y="100" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="310" x="55" y="155" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;" value="💡" vertex="1">
          <mxGeometry height="30" width="30" x="60" y="160" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#1565c0;fontStyle=1;" value="&quot;有构造,能存变量&quot;" vertex="1">
          <mxGeometry height="40" width="270" x="90" y="155" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="310" x="55" y="205" 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=18;" value="💡" vertex="1">
          <mxGeometry height="30" width="30" x="60" y="210" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#1565c0;fontStyle=1;" value="&quot;单继承,可有具体&quot;" vertex="1">
          <mxGeometry height="40" width="270" x="90" y="205" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="310" x="55" y="255" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;" value="💡" vertex="1">
          <mxGeometry height="30" width="30" x="60" y="260" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#1565c0;fontStyle=1;" value="&quot;模板用,共享态&quot;" vertex="1">
          <mxGeometry height="40" width="270" x="90" y="255" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=#bbdefb;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=12;fontColor=#1565c0;fontStyle=2;fontFamily=Consolas;" value="abstract" vertex="1">
          <mxGeometry height="22" width="80" x="280" y="300" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#fb8c00;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="230" width="340" x="420" y="100" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#fb8c00;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=18;fontStyle=1;fontColor=#ffffff;" value="🔶 接口记忆" vertex="1">
          <mxGeometry height="45" width="340" x="420" y="100" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="310" x="435" y="155" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;" value="💡" vertex="1">
          <mxGeometry height="30" width="30" x="440" y="160" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#e65100;fontStyle=1;" value="&quot;无构造,只常量&quot;" vertex="1">
          <mxGeometry height="40" width="270" x="470" y="155" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="310" x="435" y="205" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;" value="💡" vertex="1">
          <mxGeometry height="30" width="30" x="440" y="210" 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=16;fontColor=#e65100;fontStyle=1;" value="&quot;多实现,定规范&quot;" vertex="1">
          <mxGeometry height="40" width="270" x="470" y="205" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#ffcc80;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="310" x="435" y="255" 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=18;" value="💡" vertex="1">
          <mxGeometry height="30" width="30" x="440" y="260" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#e65100;fontStyle=1;" value="&quot;Java8 后,有默认&quot;" vertex="1">
          <mxGeometry height="40" width="270" x="470" y="255" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#ffe0b2;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=12;fontColor=#e65100;fontStyle=2;fontFamily=Consolas;" value="interface" vertex="1">
          <mxGeometry height="22" width="80" x="660" y="300" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="280" width="720" x="40" y="350" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#4caf50;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=20;fontStyle=1;fontColor=#ffffff;" value="🎯 选择口诀" vertex="1">
          <mxGeometry height="50" width="720" x="40" y="350" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#a5d6a7;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="680" x="60" y="415" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=20;" value="1️⃣" vertex="1">
          <mxGeometry height="40" width="40" x="70" y="420" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontColor=#2e7d32;fontStyle=1;" value="&quot;要模板用抽象类,定规范用接口&quot;" vertex="1">
          <mxGeometry height="50" width="620" x="110" y="415" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#a5d6a7;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="680" x="60" y="480" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=20;" value="2️⃣" vertex="1">
          <mxGeometry height="40" width="40" x="70" y="485" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontColor=#2e7d32;fontStyle=1;" value="&quot;要共享用抽象类,要解耦用接口&quot;" vertex="1">
          <mxGeometry height="50" width="620" x="110" y="480" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#66bb6a;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="65" width="680" x="60" y="545" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;" value="⭐" vertex="1">
          <mxGeometry height="45" width="40" x="70" y="555" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=22;fontColor=#1b5e20;fontStyle=1;" value="&quot;优先接口,抽象补充&quot;" vertex="1">
          <mxGeometry height="65" width="620" x="110" y="545" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f3e5f5;strokeColor=#9c27b0;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="160" width="720" x="40" y="650" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=#9c27b0;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=18;fontStyle=1;fontColor=#ffffff;" value="💎 快速记忆卡片" vertex="1">
          <mxGeometry height="40" width="720" x="40" y="650" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="85" width="220" x="60" y="705" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#1976d2;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontStyle=1;fontColor=#ffffff;" value="抽象类" vertex="1">
          <mxGeometry height="25" width="220" x="60" y="705" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#1565c0;fontStyle=1;" value="is-a 关系&#xa;模板 + 共享" vertex="1">
          <mxGeometry height="50" width="220" x="60" y="735" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#fb8c00;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="85" width="220" x="300" y="705" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=#fb8c00;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontStyle=1;fontColor=#ffffff;" value="接口" vertex="1">
          <mxGeometry height="25" width="220" x="300" y="705" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#e65100;fontStyle=1;" value="can-do 能力&#xa;规范 + 解耦" vertex="1">
          <mxGeometry height="50" width="220" x="300" y="735" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="85" width="200" x="540" y="705" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=#4caf50;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontStyle=1;fontColor=#ffffff;" value="优先级" vertex="1">
          <mxGeometry height="25" width="200" x="540" y="705" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#2e7d32;fontStyle=1;" value="接口 &gt; 抽象类&#xa;组合 &gt; 继承" vertex="1">
          <mxGeometry height="50" width="200" x="540" y="735" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
%%{init: {
  "theme": "base",
  "themeVariables": {
    "primaryColor": "#e3f2fd",
    "primaryTextColor": "#0d47a1",
    "primaryBorderColor": "#2196f3",
    "lineColor": "#546e7a",
    "fontSize": "14px",
    "tertiaryColor": "#fdfdfd"
  },
  "flowchart": { "curve": "basis", "htmlLabels": true }
}}%%

graph TB
    %% 样式定义
    classDef abstractStyle fill:#e3f2fd,stroke:#2196f3,stroke-width:2px,color:#0d47a1;
    classDef interfaceStyle fill:#f1f8e9,stroke:#4caf50,stroke-width:2px,color:#1b5e20;
    classDef selectStyle fill:#fff3e0,stroke:#ff9800,stroke-width:2px,color:#e65100;
    classDef titleStyle fill:#f5f5f5,stroke:#9e9e9e,stroke-width:1px,color:#333,font-weight:bold;

    MainTitle(["Java 抽象类与接口记忆口诀"])

    subgraph AbstractGroup ["抽象类 (Abstract Class)"]
        A1["有构造,能存变量"]
        A2["单继承,可有具体"]
        A3["模板用,共享状态"]
    end

    subgraph InterfaceGroup ["接口 (Interface)"]
        I1["无构造,只常量"]
        I2["多实现,定规范"]
        I3["Java 8 后,有默认"]
    end

    subgraph StrategyGroup ["选择策略 (Decision Guide)"]
        S1{"场景选择"}
        S2["要模板/共享用抽象类"]
        S3["定规范/解耦用接口"]
        S4(["核心原则:优先接口,抽象补充"])
    end

    %% 连接逻辑
    MainTitle --> AbstractGroup
    MainTitle --> InterfaceGroup

    AbstractGroup ~~~ StrategyGroup
    InterfaceGroup ~~~ StrategyGroup

    S1 ==> S2
    S1 ==> S3
    S2 --> S4
    S3 --> S4

    %% 应用样式
    class AbstractGroup,A1,A2,A3 abstractStyle;
    class InterfaceGroup,I1,I2,I3 interfaceStyle;
    class StrategyGroup,S1,S2,S3,S4 selectStyle;
    class MainTitle titleStyle;