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="子类与抽象类是 "is-a" 关系(是一种)" 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="实现类与接口是 "can-do" 关系(能做什么)" 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 {
private String name;
protected int age;
public static int count = 0;
public Animal() {
count++;
}
public Animal(String name, int age) {
this.name = name;
this.age = age;
count++;
}
public void sleep() {
System.out.println(name + " 正在睡觉");
}
public abstract void makeSound();
public abstract void eat();
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
2.2 接口语法
public interface Flyable {
int MAX_HEIGHT = 10000;
void fly();
void land();
default void glide() {
System.out.println("滑翔中...");
}
static void showInfo() {
System.out.println("这是飞行接口");
}
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();<br>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 {<br>&nbsp; &nbsp; abstract void eat();<br>}" 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 {<br>&nbsp; &nbsp; void fly(); // 默认抽象<br>}" 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 {<br>&nbsp; &nbsp; void sleep() { ... }<br>}" 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 {<br>&nbsp; &nbsp; default void glide() { ... }<br>}" 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="• 定义统一的行为规范<br>• 隐藏具体实现细节<br>• 提供抽象层" 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="• 面向抽象编程<br>• 依赖倒置原则<br>• 便于扩展和维护" 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;<br>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;<br>String TYPE = "animal";" 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="• 单继承<br>• 有构造方法<br>• 可有普通属性<br>• 表示 "是什么"" 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="• 多实现<br>• 无构造方法<br>• 只有常量<br>• 表示 "能做什么"" 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="• 共享代码 → 抽象类<br>• 定义能力 → 接口<br>• 多重继承 → 接口<br>• 紧密关系 → 抽象类" 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 { }
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>
<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>
<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>
<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>
<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>
<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>
<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>
<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();
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();
default void glide() {
System.out.println("滑翔中...");
}
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 + " 可以飞、游、走");
}
@Override
public void fly() {
System.out.println(name + " 展翅高飞,最高 " + MAX_ALTITUDE + " 米");
}
@Override
public void land() {
System.out.println(name + " 平稳降落");
}
@Override
public void swim() {
System.out.println(name + " 在水中畅游");
}
@Override
public String getHowToEat() {
return name + " 可以烤着吃";
}
}
public class Main {
public static void main(String[] args) {
WildDuck duck = new WildDuck("唐老鸭");
duck.showInfo();
duck.sleep();
duck.fly();
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<br># 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<br>+ showInfo(): void<br><i>+ sing(): void {abstract}</i><br><i>+ move(): void {abstract}</i>" 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<br>+ 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<br>+ 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<br>+ move(): void<br>+ fly(): void<br>+ swim(): void<br>+ 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&nbsp; &nbsp;+ public&nbsp; &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="<font face="Courier New"><span style="font-size: 14px;"><font color="#c084fc">interface</font> <font color="#22d3ee">MyInterface</font> {</span></font>" 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="<font face="Courier New"><font color="#f472b6">int</font> <font color="#fbbf24">CONSTANT</font> = <font color="#a78bfa">1</font>;</font>" 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="<font face="Courier New"><font color="#f472b6">void</font> <font color="#4ade80">method</font>();</font>" 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="<font face="Courier New"><span style="font-size: 14px;"><font color="#c084fc">interface</font> <font color="#22d3ee">MyInterface</font> {</span></font>" 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="<font face="Courier New" color="#64748b">// ... 原有内容 ...</font>" 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="<font face="Courier New"><font color="#4ade80">default</font> <font color="#f472b6">void</font> <font color="#fbbf24">defaultMethod</font>() { }</font>" 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="<font face="Courier New"><font color="#4ade80">static</font> <font color="#f472b6">void</font> <font color="#fbbf24">staticMethod</font>() { }</font>" 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="<font face="Courier New"><span style="font-size: 14px;"><font color="#c084fc">interface</font> <font color="#22d3ee">MyInterface</font> {</span></font>" 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="<font face="Courier New" color="#64748b">// ... 原有内容 ...</font>" 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="<font face="Courier New"><font color="#fbbf24">private</font> <font color="#f472b6">void</font> <font color="#a78bfa">helper</font>() { }</font>" 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="<font face="Courier New"><font color="#fbbf24">private static</font> <font color="#f472b6">void</font> <font color="#a78bfa">util</font>() { }</font>" 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();
default boolean isEmpty() {
return size() == 0;
}
}
public class MyList implements Collection {
private int count = 0;
@Override
public int size() {
return count;
}
}
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() {
System.out.println("Hello from C");
}
}
冲突解决规则:
<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="<font face="Courier New"><font color="#c084fc">class</font> <font color="#22d3ee">MyClass</font> <font color="#c084fc">implements</font> <font color="#fbbf24">InterfaceA</font> {<br>&nbsp; &nbsp; <font color="#f472b6">void</font> <font color="#4ade80">method</font>() { ... }&nbsp; <font color="#64748b">// ✅ 类方法优先,忽略接口默认方法</font><br>}</font>" 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="<font face="Courier New"><font color="#c084fc">interface</font> <font color="#fbbf24">B</font> <font color="#c084fc">extends</font> <font color="#94a3b8">A</font> { <font color="#4ade80">default</font> <font color="#f472b6">void</font> method() { ... } }<br><font color="#c084fc">class</font> <font color="#22d3ee">MyClass</font> <font color="#c084fc">implements</font> <font color="#fbbf24">B</font> { }&nbsp; <font color="#64748b">// ✅ 使用 B 的默认方法</font></font>" 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="<font face="Courier New"><font color="#c084fc">class</font> <font color="#22d3ee">MyClass</font> <font color="#c084fc">implements</font> <font color="#fbbf24">InterfaceA</font>, <font color="#fbbf24">InterfaceB</font> {<br>&nbsp; &nbsp; <font color="#fb7185">@Override</font><br>&nbsp; &nbsp; <font color="#f472b6">void</font> <font color="#4ade80">method</font>() {<br>&nbsp; &nbsp; &nbsp; &nbsp; <font color="#fbbf24">InterfaceA</font>.<font color="#c084fc">super</font>.method();&nbsp; <font color="#64748b">// ⚠️ 必须显式选择或自定义实现</font><br>&nbsp; &nbsp; }<br>}</font>" 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»
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»
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="<font face="Courier New"><font color="#64748b">// 功能接口 - 定义比较能力</font></font>" 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="<font face="Courier New"><font color="#c084fc">interface</font> <font color="#22d3ee">Comparable</font><font color="#fbbf24">&lt;T&gt;</font> {<br>&nbsp; &nbsp; <font color="#f472b6">int</font> <font color="#4ade80">compareTo</font>(<font color="#fbbf24">T</font> o);&nbsp; <font color="#64748b">// 定义比较能力</font><br>}</font>" 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="<font face="Courier New"><font color="#64748b">// 标记接口 - 表示类具有某种特性</font></font>" 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="<font face="Courier New"><font color="#c084fc">interface</font> <font color="#22d3ee">Serializable</font> {<br>}&nbsp; <font color="#64748b">// 可序列化</font></font>" 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="<font face="Courier New"><font color="#c084fc">interface</font> <font color="#22d3ee">Cloneable</font> {<br>}&nbsp; <font color="#64748b">// 可克隆</font></font>" 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="<font face="Courier New"><font color="#64748b">// 1. 定义接口</font></font>" 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="<font face="Courier New"><font color="#c084fc">interface</font> <font color="#22d3ee">UserRepository</font> {<br>&nbsp; &nbsp; <font color="#fbbf24">User</font> <font color="#4ade80">findById</font>(<font color="#f472b6">Long</font> id);<br>&nbsp; &nbsp; <font color="#f472b6">void</font> <font color="#4ade80">save</font>(<font color="#fbbf24">User</font> user);<br>}</font>" 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="<font face="Courier New"><font color="#64748b">// 2. 多种实现</font></font>" 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="<font face="Courier New"><font color="#22d3ee">MySqlUserRepository</font></font>" 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="<font face="Courier New"><font color="#22d3ee">MongoUserRepository</font></font>" 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="<font face="Courier New"><font color="#22d3ee">MockUserRepository</font></font>" 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="<font face="Courier New"><font color="#64748b">// 3. 使用方只依赖接口</font></font>" 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="<font face="Courier New"><font color="#c084fc">class</font> <font color="#22d3ee">UserService</font> {<br>&nbsp; &nbsp; <font color="#c084fc">private</font> <font color="#fbbf24">UserRepository</font> repo;&nbsp; <font color="#64748b">// ✅ 面向接口编程,可注入任意实现</font><br>}</font>" 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="<font face="Courier New"><font color="#64748b">// 定义回调接口</font></font>" 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="<font face="Courier New"><font color="#c084fc">interface</font> <font color="#22d3ee">OnClickListener</font> {<br>&nbsp; &nbsp; <font color="#f472b6">void</font> <font color="#4ade80">onClick</font>(<font color="#fbbf24">View</font> v);<br>}</font>" 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="<font face="Courier New"><font color="#64748b">// 匿名内部类方式</font></font>" 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="<font face="Courier New">button.<font color="#4ade80">setOnClickListener</font>(<font color="#c084fc">new</font> <font color="#22d3ee">OnClickListener</font>() {<br>&nbsp; &nbsp; <font color="#fb7185">@Override</font><br>&nbsp; &nbsp; <font color="#c084fc">public</font> <font color="#f472b6">void</font> <font color="#4ade80">onClick</font>(<font color="#fbbf24">View</font> v) {<br>&nbsp; &nbsp; &nbsp; &nbsp; <font color="#64748b">// 处理点击事件</font><br>&nbsp; &nbsp; }<br>});</font>" 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="<font face="Courier New"><font color="#64748b">// Java 8 Lambda 简化</font></font>" 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="<font face="Courier New">button.<font color="#4ade80">setOnClickListener</font>(v -&gt; { <font color="#64748b">/* 处理点击 */</font> });</font>" 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="需要定义一组类的
通用行为?" 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="这些类有没有
共同的父类?" 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="需要共享
代码/状态?" 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="• 定义行为规范
• 可多实现
• 用于不相关的类" 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="• 模板方法模式
• 共享状态/代码
• 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="• 只需规范行为
• 无需共享代码" 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="抽象类提供
骨架实现" 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="具体类
完成实现" 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="<font face="Courier New"><font color="#22d3ee">List</font></font>" 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="<font face="Courier New"><font color="#fbbf24">AbstractList</font></font>" 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="<font face="Courier New"><font color="#4ade80">ArrayList</font></font>" 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»
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»
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»
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=""有构造,能存变量"" 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=""单继承,可有具体"" 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=""模板用,共享态"" 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=""无构造,只常量"" 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=""多实现,定规范"" 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=""Java8 后,有默认"" 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=""要模板用抽象类,定规范用接口"" 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=""要共享用抽象类,要解耦用接口"" 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=""优先接口,抽象补充"" 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 关系
模板 + 共享" 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 能力
规范 + 解耦" 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="接口 > 抽象类
组合 > 继承" 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;
💬 评论