面向对象与面向过程编程对比
一、核心概念定义
1.1 概念对比表
| 维度 | 面向过程 (POP) | 面向对象 (OOP) |
|---|---|---|
| 全称 | Procedure Oriented Programming | Object Oriented Programming |
| 核心单元 | 函数(过程) | 对象(类的实例) |
| 思维方式 | 做事的步骤 | 做事的角色 |
| 数据与操作 | 分离 | 封装在一起 |
| 程序结构 | 函数调用链 | 对象协作网 |
1.2 结构对比图
<mxfile>
<diagram id="pop-vs-oop-fixed" name="面向过程 vs 面向对象">
<mxGraphModel dx="1665" dy="796" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="750" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#1E293B;" value="编程范式对比:面向过程 (POP) vs 面向对象 (OOP)" vertex="1">
<mxGeometry height="35" width="500" x="200" y="15" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="280" width="820" x="40" y="70" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="面向过程 (POP - Procedure Oriented Programming)" vertex="1">
<mxGeometry height="35" width="460" x="220" y="85" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#FDE68A;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#92400E;rounded=1;" value="📦 数据散落各处" vertex="1">
<mxGeometry height="26" width="140" x="60" y="140" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FCD34D;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="50" width="100" x="80" y="175" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#92400E;" value="数据 A" vertex="1">
<mxGeometry height="30" width="100" x="80" y="185" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FCD34D;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="50" width="100" x="200" y="175" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#92400E;" value="数据 B" vertex="1">
<mxGeometry height="30" width="100" x="200" y="185" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FCD34D;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="50" width="100" x="320" y="175" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#92400E;" value="数据 C" vertex="1">
<mxGeometry height="30" width="100" x="320" y="185" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#FDE68A;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#92400E;rounded=1;" value="⚡ 函数顺序调用" vertex="1">
<mxGeometry height="26" width="140" x="60" y="245" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#D97706;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="50" width="100" x="80" y="280" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#B45309;fontFamily=Courier New;" value="函数 1()" vertex="1">
<mxGeometry height="30" width="100" x="80" y="290" as="geometry" />
</mxCell>
<mxCell id="15" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#F59E0B;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="190" y="305" as="sourcePoint" />
<mxPoint x="250" y="305" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#D97706;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="50" width="100" x="260" y="280" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#B45309;fontFamily=Courier New;" value="函数 2()" vertex="1">
<mxGeometry height="30" width="100" x="260" y="290" as="geometry" />
</mxCell>
<mxCell id="18" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#F59E0B;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="370" y="305" as="sourcePoint" />
<mxPoint x="430" y="305" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#D97706;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="50" width="100" x="440" y="280" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#B45309;fontFamily=Courier New;" value="函数 3()" vertex="1">
<mxGeometry height="30" width="100" x="440" y="290" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#94A3B8;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="190" width="260" x="580" y="140" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#64748B;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📋 特点" vertex="1">
<mxGeometry height="24" width="70" x="600" y="155" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#475569;spacingLeft=5;" value="• 以函数为核心组织代码" vertex="1">
<mxGeometry height="22" width="220" x="600" y="185" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#475569;spacingLeft=5;" value="• 数据与操作分离" vertex="1">
<mxGeometry height="22" width="220" x="600" y="210" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#475569;spacingLeft=5;" value="• 自顶向下,逐步细化" vertex="1">
<mxGeometry height="22" width="220" x="600" y="235" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#475569;spacingLeft=5;" value="• 适合简单、线性流程" vertex="1">
<mxGeometry height="22" width="220" x="600" y="260" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#E2E8F0;align=center;verticalAlign=middle;fontSize=11;fontColor=#64748B;rounded=1;fontStyle=2;" value="代表:C 语言" vertex="1">
<mxGeometry height="22" width="90" x="600" y="295" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="360" width="820" x="40" y="370" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="面向对象 (OOP - Object Oriented Programming)" vertex="1">
<mxGeometry height="35" width="460" x="220" y="385" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=#93C5FD;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="🔗 对象协作" vertex="1">
<mxGeometry height="26" width="100" x="60" y="435" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="100" width="160" x="80" y="475" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="对象 A" vertex="1">
<mxGeometry height="26" width="90" x="115" y="485" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;" value="- 数据 (属性)" vertex="1">
<mxGeometry height="22" width="140" x="90" y="518" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;" value="- 行为 (方法)" vertex="1">
<mxGeometry height="22" width="140" x="90" y="543" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="100" width="160" x="320" y="475" as="geometry" />
</mxCell>
<mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="对象 B" vertex="1">
<mxGeometry height="26" width="90" x="355" y="485" as="geometry" />
</mxCell>
<mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;" value="- 数据 (属性)" vertex="1">
<mxGeometry height="22" width="140" x="330" y="518" as="geometry" />
</mxCell>
<mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;" value="- 行为 (方法)" vertex="1">
<mxGeometry height="22" width="140" x="330" y="543" as="geometry" />
</mxCell>
<mxCell id="39" edge="1" parent="1" style="endArrow=blockThin;startArrow=blockThin;html=1;strokeWidth=3;strokeColor=#3B82F6;endFill=1;startFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="250" y="525" as="sourcePoint" />
<mxPoint x="310" y="525" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="40" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="100" width="160" x="200" y="610" as="geometry" />
</mxCell>
<mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="对象 C" vertex="1">
<mxGeometry height="26" width="90" x="235" y="620" as="geometry" />
</mxCell>
<mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;" value="- 数据 (属性)" vertex="1">
<mxGeometry height="22" width="140" x="210" y="653" as="geometry" />
</mxCell>
<mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;" value="- 行为 (方法)" vertex="1">
<mxGeometry height="22" width="140" x="210" y="678" as="geometry" />
</mxCell>
<mxCell id="44" edge="1" parent="1" source="31" style="endArrow=blockThin;startArrow=blockThin;html=1;strokeWidth=3;strokeColor=#3B82F6;endFill=1;startFill=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.25;exitY=1;exitDx=0;exitDy=0;" target="40" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="160" y="590" as="sourcePoint" />
<mxPoint x="280" y="600" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="45" edge="1" parent="1" source="35" style="endArrow=blockThin;startArrow=blockThin;html=1;strokeWidth=3;strokeColor=#3B82F6;endFill=1;startFill=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.75;exitY=1;exitDx=0;exitDy=0;" target="40" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="400" y="590" as="sourcePoint" />
<mxPoint x="290" y="600" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=#93C5FD;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="📦 数据与行为封装在一起" vertex="1">
<mxGeometry height="24" width="180" x="380" y="660" as="geometry" />
</mxCell>
<mxCell id="47" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#94A3B8;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="275" width="260" x="580" y="435" as="geometry" />
</mxCell>
<mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=#64748B;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📋 特点" vertex="1">
<mxGeometry height="24" width="70" x="600" y="450" as="geometry" />
</mxCell>
<mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#475569;spacingLeft=5;" value="• 以对象为核心组织代码" vertex="1">
<mxGeometry height="22" width="220" x="600" y="480" as="geometry" />
</mxCell>
<mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#475569;spacingLeft=5;" value="• 数据与操作封装在一起" vertex="1">
<mxGeometry height="22" width="220" x="600" y="505" as="geometry" />
</mxCell>
<mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#475569;spacingLeft=5;" value="• 通过对象间消息传递协作" vertex="1">
<mxGeometry height="22" width="220" x="600" y="530" as="geometry" />
</mxCell>
<mxCell id="52" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#475569;spacingLeft=5;" value="• 适合复杂、可扩展系统" vertex="1">
<mxGeometry height="22" width="220" x="600" y="555" as="geometry" />
</mxCell>
<mxCell id="53" 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="90" x="600" y="590" as="geometry" />
</mxCell>
<mxCell id="54" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontColor=#1E40AF;rounded=1;fontStyle=1;" value="封装" vertex="1">
<mxGeometry height="22" width="55" x="610" y="620" as="geometry" />
</mxCell>
<mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontColor=#1E40AF;rounded=1;fontStyle=1;" value="继承" vertex="1">
<mxGeometry height="22" width="55" x="675" y="620" as="geometry" />
</mxCell>
<mxCell id="56" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontColor=#1E40AF;rounded=1;fontStyle=1;" value="多态" vertex="1">
<mxGeometry height="22" width="55" x="740" y="620" as="geometry" />
</mxCell>
<mxCell id="57" parent="1" style="text;html=1;strokeColor=none;fillColor=#E2E8F0;align=center;verticalAlign=middle;fontSize=11;fontColor=#64748B;rounded=1;fontStyle=2;" value="代表:Java / C++ / Python" vertex="1">
<mxGeometry height="22" width="170" x="600" y="655" as="geometry" />
</mxCell>
<mxCell id="58" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#92400E;rounded=1;" value="💡 核心区别:POP 关注"怎么做"(步骤),OOP 关注"谁来做"(对象职责)" vertex="1">
<mxGeometry height="30" width="600" x="150" y="740" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
二、思维方式对比
2.1 生活案例:把大象装进冰箱
<mxfile>
<diagram id="elephant-in-fridge" name="把大象装进冰箱">
<mxGraphModel dx="1665" dy="796" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="650" width="820" x="40" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🐘 把大象装进冰箱" vertex="1">
<mxGeometry height="45" width="340" x="280" y="40" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="540" width="370" x="56" y="111" as="geometry" />
</mxCell>
<mxCell id="5" 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="35" width="210" x="136" y="126" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#FDE68A;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#92400E;rounded=1;" value="🎯 关注:做事的【步骤】" vertex="1">
<mxGeometry height="30" width="200" x="141" y="176" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#D97706;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="190" x="146" y="231" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Step 1" vertex="1">
<mxGeometry height="20" width="55" x="161" y="241" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#92400E;fontFamily=Courier New;" value="开门()" vertex="1">
<mxGeometry height="35" width="100" x="221" y="241" as="geometry" />
</mxCell>
<mxCell id="10" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#F59E0B;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="241" y="296" as="sourcePoint" />
<mxPoint x="241" y="336" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#D97706;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="190" x="146" y="346" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Step 2" vertex="1">
<mxGeometry height="20" width="55" x="161" y="356" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#92400E;fontFamily=Courier New;" value="装大象()" vertex="1">
<mxGeometry height="35" width="100" x="221" y="356" as="geometry" />
</mxCell>
<mxCell id="14" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#F59E0B;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="241" y="411" as="sourcePoint" />
<mxPoint x="241" y="451" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#D97706;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="190" x="146" y="461" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Step 3" vertex="1">
<mxGeometry height="20" width="55" x="161" y="471" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#92400E;fontFamily=Courier New;" value="关门()" vertex="1">
<mxGeometry height="35" width="100" x="221" y="471" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#92400E;rounded=1;" value="✅ 完成!" vertex="1">
<mxGeometry height="26" width="90" x="196" y="536" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=#FFFBEB;align=center;verticalAlign=middle;fontSize=12;fontColor=#B45309;rounded=1;fontStyle=2;" value="执行者视角:我要做什么" vertex="1">
<mxGeometry height="24" width="200" x="141" y="581" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=#FFFBEB;align=center;verticalAlign=middle;fontSize=12;fontColor=#B45309;rounded=1;fontStyle=2;" value="数据和操作是分离的" vertex="1">
<mxGeometry height="24" width="200" x="141" y="611" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="540" width="370" x="474" y="110" as="geometry" />
</mxCell>
<mxCell id="22" 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="35" width="210" x="554" y="125" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#93C5FD;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="🎯 关注:做事的【角色】" vertex="1">
<mxGeometry height="30" width="200" x="559" y="175" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="130" width="140" x="499" y="225" as="geometry" />
</mxCell>
<mxCell id="25" 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="524" y="235" as="geometry" />
</mxCell>
<mxCell id="26" 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=#93C5FD;" value="" vertex="1">
<mxGeometry height="8" width="120" x="509" y="270" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;fontFamily=Courier New;" value="开门()" vertex="1">
<mxGeometry height="22" width="120" x="509" y="280" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;fontFamily=Courier New;" value="关门()" vertex="1">
<mxGeometry height="22" width="120" x="509" y="302" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;fontFamily=Courier New;" value="存放()" vertex="1">
<mxGeometry height="22" width="120" x="509" y="324" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="100" width="140" x="669" y="225" as="geometry" />
</mxCell>
<mxCell id="31" 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="694" y="235" as="geometry" />
</mxCell>
<mxCell id="32" 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=#93C5FD;" value="" vertex="1">
<mxGeometry height="8" width="120" x="679" y="270" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=10;fontFamily=Courier New;" value="进入()" vertex="1">
<mxGeometry height="22" width="120" x="679" y="285" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#93C5FD;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="📝 调用方式" vertex="1">
<mxGeometry height="26" width="100" x="499" y="375" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#60A5FA;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="120" width="320" x="499" y="415" as="geometry" />
</mxCell>
<mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#BFDBFE;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;rounded=1;spacingLeft=10;fontFamily=Courier New;" value="冰箱.开门()" vertex="1">
<mxGeometry height="28" width="180" x="519" y="430" as="geometry" />
</mxCell>
<mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#BFDBFE;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;rounded=1;spacingLeft=10;fontFamily=Courier New;" value="冰箱.存放(大象)" vertex="1">
<mxGeometry height="28" width="180" x="519" y="465" as="geometry" />
</mxCell>
<mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=#BFDBFE;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;rounded=1;spacingLeft=10;fontFamily=Courier New;" value="冰箱.关门()" vertex="1">
<mxGeometry height="28" width="180" x="519" y="500" as="geometry" />
</mxCell>
<mxCell id="39" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;fontColor=#FFFFFF;fontSize=11;fontStyle=1;" value="1" vertex="1">
<mxGeometry height="22" width="22" x="704" y="432" as="geometry" />
</mxCell>
<mxCell id="40" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;fontColor=#FFFFFF;fontSize=11;fontStyle=1;" value="2" vertex="1">
<mxGeometry height="22" width="22" x="704" y="467" as="geometry" />
</mxCell>
<mxCell id="41" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;fontColor=#FFFFFF;fontSize=11;fontStyle=1;" value="3" vertex="1">
<mxGeometry height="22" width="22" x="704" y="502" as="geometry" />
</mxCell>
<mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#EFF6FF;align=center;verticalAlign=middle;fontSize=12;fontColor=#1D4ED8;rounded=1;fontStyle=2;" value="指挥者视角:谁来做什么" vertex="1">
<mxGeometry height="24" width="200" x="559" y="555" as="geometry" />
</mxCell>
<mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=#EFF6FF;align=center;verticalAlign=middle;fontSize=12;fontColor=#1D4ED8;rounded=1;fontStyle=2;" value="数据和行为封装在对象中" vertex="1">
<mxGeometry height="24" width="200" x="559" y="585" as="geometry" />
</mxCell>
<mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="VS" vertex="1">
<mxGeometry height="40" width="40" x="430" y="365" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
2.2 案例伪代码对比
// ================== 面向过程 ==================
public class ProcedureStyle {
public static void main(String[] args) {
openDoor(); // 第一步
putElephant(); // 第二步
closeDoor(); // 第三步
}
static void openDoor() { System.out.println("打开冰箱门"); }
static void putElephant() { System.out.println("把大象装进去"); }
static void closeDoor() { System.out.println("关上冰箱门"); }
}
// ================== 面向对象 ==================
class Refrigerator {
public void open() { System.out.println("冰箱门打开了"); }
public void store(Object item) { System.out.println(item + " 被存放"); }
public void close() { System.out.println("冰箱门关上了"); }
}
class Elephant {
private String name = "大象";
@Override
public String toString() { return name; }
}
public class OOPStyle {
public static void main(String[] args) {
Refrigerator fridge = new Refrigerator();
Elephant elephant = new Elephant();
fridge.open();
fridge.store(elephant);
fridge.close();
}
}
三、完整代码对比示例
3.1 需求:计算圆的面积和周长
┌─────────────────────────────────────────────────────────────────┐
│ 需求分析 │
├─────────────────────────────────────────────────────────────────┤
│ 输入:圆的半径 radius │
│ 输出:面积 area = π × r² │
│ 周长 circumference = 2 × π × r │
└─────────────────────────────────────────────────────────────────┘
3.2 面向过程实现
public class ProcedureDemo {
public static void main(String[] args) {
// ===== 数据定义 =====
double radius = 5.0;
double pi = 3.14;
// ===== 计算过程(函数调用)=====
double area = calculateArea(radius, pi);
double circumference = calculateCircumference(radius, pi);
// ===== 输出结果 =====
printResult(area, circumference);
}
// 计算面积的函数
static double calculateArea(double r, double pi) {
return pi * r * r;
}
// 计算周长的函数
static double calculateCircumference(double r, double pi) {
return 2 * pi * r;
}
// 打印结果的函数
static void printResult(double area, double circumference) {
System.out.println("面积:" + area);
System.out.println("周长:" + circumference);
}
}
执行流程图:
<mxfile>
<diagram id="function-call-flow" name="函数调用流程">
<mxGraphModel dx="900" dy="500" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="500">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" value="函数调用流程图" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="320" y="30" width="260" height="40" as="geometry" />
</mxCell>
<mxCell id="3" value="main()" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#1E40AF;" vertex="1" parent="1">
<mxGeometry x="60" y="120" width="120" height="50" as="geometry" />
</mxCell>
<mxCell id="4" value="calculateArea()" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#166534;" vertex="1" parent="1">
<mxGeometry x="250" y="120" width="150" height="50" as="geometry" />
</mxCell>
<mxCell id="5" value="calculateCircumference()" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#92400E;" vertex="1" parent="1">
<mxGeometry x="470" y="120" width="190" height="50" as="geometry" />
</mxCell>
<mxCell id="6" value="printResult()" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3E8FF;strokeColor=#A855F7;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#7C3AED;" vertex="1" parent="1">
<mxGeometry x="730" y="120" width="130" height="50" as="geometry" />
</mxCell>
<mxCell id="7" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#64748B;endFill=1;" edge="1" parent="1" source="3" target="4">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="180" y="145" as="sourcePoint" />
<mxPoint x="250" y="145" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="8" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#64748B;endFill=1;" edge="1" parent="1" source="4" target="5">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="400" y="145" as="sourcePoint" />
<mxPoint x="470" y="145" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="9" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#64748B;endFill=1;" edge="1" parent="1" source="5" target="6">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="660" y="145" as="sourcePoint" />
<mxPoint x="730" y="145" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="10" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#EC4899;endFill=1;dashed=1;dashPattern=8 4;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="120" y="170" as="sourcePoint" />
<mxPoint x="795" y="170" as="targetPoint" />
<Array as="points">
<mxPoint x="120" y="230" />
<mxPoint x="795" y="230" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="11" value="数据在函数间传递" style="text;html=1;strokeColor=none;fillColor=#FCE7F3;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#BE185D;rounded=1;" vertex="1" parent="1">
<mxGeometry x="360" y="215" width="140" height="30" as="geometry" />
</mxCell>
<mxCell id="12" value="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=none;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="60" y="95" width="24" height="24" as="geometry" />
</mxCell>
<mxCell id="13" value="2" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=none;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="250" y="95" width="24" height="24" as="geometry" />
</mxCell>
<mxCell id="14" value="3" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=none;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="470" y="95" width="24" height="24" as="geometry" />
</mxCell>
<mxCell id="15" value="4" style="ellipse;whiteSpace=wrap;html=1;fillColor=#A855F7;strokeColor=none;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="730" y="95" width="24" height="24" as="geometry" />
</mxCell>
<mxCell id="16" value="入口函数" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#64748B;" vertex="1" parent="1">
<mxGeometry x="70" y="175" width="60" height="20" as="geometry" />
</mxCell>
<mxCell id="17" value="计算面积" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#64748B;" vertex="1" parent="1">
<mxGeometry x="285" y="175" width="60" height="20" as="geometry" />
</mxCell>
<mxCell id="18" value="计算周长" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#64748B;" vertex="1" parent="1">
<mxGeometry x="530" y="175" width="60" height="20" as="geometry" />
</mxCell>
<mxCell id="19" value="输出结果" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#64748B;" vertex="1" parent="1">
<mxGeometry x="760" y="175" width="60" height="20" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
3.3 面向对象实现
public class Circle {
// ===== 属性(封装数据)=====
private double radius; // 半径(私有,外部不能直接访问)
private static final double PI = Math.PI; // 常量
// ===== 构造方法(初始化对象)=====
public Circle(double radius) {
if (radius <= 0) {
throw new IllegalArgumentException("半径必须大于0");
}
this.radius = radius;
}
// ===== 行为(方法)=====
public double getArea() {
return PI * radius * radius;
}
public double getCircumference() {
return 2 * PI * radius;
}
// Getter 方法
public double getRadius() {
return radius;
}
// Setter 方法(带验证)
public void setRadius(double radius) {
if (radius <= 0) {
throw new IllegalArgumentException("半径必须大于0");
}
this.radius = radius;
}
// 重写 toString 方法
@Override
public String toString() {
return String.format("Circle[radius=%.2f, area=%.2f, circumference=%.2f]",
radius, getArea(), getCircumference());
}
}
// ===== 使用类 =====
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(5.0);
System.out.println("半径:" + circle.getRadius());
System.out.println("面积:" + circle.getArea());
System.out.println("周长:" + circle.getCircumference());
System.out.println(circle); // 调用 toString()
}
}
对象内存结构图:
<mxfile>
<diagram id="memory-structure" name="内存结构图">
<mxGraphModel dx="1665" dy="796" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="720" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Java 内存结构图" vertex="1">
<mxGeometry height="40" width="260" x="320" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;opacity=50;" value="" vertex="1">
<mxGeometry height="340" width="740" x="80" y="80" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="堆内存 (Heap)" vertex="1">
<mxGeometry height="35" width="740" x="80" y="80" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#D97706;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="250" width="420" x="140" y="140" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#D97706;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Circle 对象 (0x1234)" vertex="1">
<mxGeometry height="32" width="420" x="140" y="140" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="line;strokeWidth=2;html=1;strokeColor=#D97706;" value="" vertex="1">
<mxGeometry height="10" width="420" x="140" y="205" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="line;strokeWidth=2;html=1;strokeColor=#D97706;" value="" vertex="1">
<mxGeometry height="10" width="420" x="140" y="260" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#166534;rounded=1;spacingLeft=10;" value="radius = 5.0" vertex="1">
<mxGeometry height="30" width="280" x="160" y="176" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#64748B;" value="← 属性" vertex="1">
<mxGeometry height="30" width="80" x="460" y="176" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=left;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#1E40AF;rounded=1;spacingLeft=10;" value="getArea()" vertex="1">
<mxGeometry height="28" width="120" x="160" y="225" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#64748B;" value="→ 返回 78.54" vertex="1">
<mxGeometry height="28" width="100" x="290" y="225" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=left;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#1E40AF;rounded=1;spacingLeft=10;" value="getCircumference()" vertex="1">
<mxGeometry height="28" width="160" x="160" y="275" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#64748B;" value="→ 返回 31.42" vertex="1">
<mxGeometry height="28" width="100" x="330" y="275" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=left;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#1E40AF;rounded=1;spacingLeft=10;" value="getRadius()" vertex="1">
<mxGeometry height="28" width="120" x="160" y="310" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#64748B;" value="→ 返回 5.0" vertex="1">
<mxGeometry height="28" width="100" x="290" y="310" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=left;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#1E40AF;rounded=1;spacingLeft=10;" value="setRadius(r)" vertex="1">
<mxGeometry height="28" width="120" x="160" y="345" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#64748B;" value="→ 修改 radius" vertex="1">
<mxGeometry height="28" width="100" x="290" y="345" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#64748B;" value="← 方法" vertex="1">
<mxGeometry height="30" width="80" x="460" y="305" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;opacity=50;" value="" vertex="1">
<mxGeometry height="140" width="740" x="80" y="500" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="栈内存 (Stack)" vertex="1">
<mxGeometry height="35" width="740" x="80" y="500" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#2563EB;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#1E40AF;" value="circle = 0x1234" vertex="1">
<mxGeometry height="50" width="160" x="280" y="560" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#64748B;" value="← 引用变量(存储对象地址)" vertex="1">
<mxGeometry height="30" width="200" x="460" y="570" as="geometry" />
</mxCell>
<mxCell id="24" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#EC4899;endFill=1;startArrow=none;startFill=0;dashed=1;dashPattern=8 4;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="360" y="560" as="sourcePoint" />
<mxPoint x="360" y="390" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#FCE7F3;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#BE185D;rounded=1;" value="引用" vertex="1">
<mxGeometry height="26" width="50" x="370" y="460" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=#F1F5F9;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#475569;rounded=1;" value="💡 说明" vertex="1">
<mxGeometry height="24" width="70" x="660" y="206" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#64748B;" value="堆:存储对象实例" vertex="1">
<mxGeometry height="20" width="120" x="640" y="236" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#64748B;" value="栈:存储引用变量" vertex="1">
<mxGeometry height="20" width="120" x="640" y="261" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#64748B;" value="引用指向堆中对象" vertex="1">
<mxGeometry height="20" width="120" x="640" y="286" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=none;" value="" vertex="1">
<mxGeometry height="8" width="8" x="630" y="242" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=none;" value="" vertex="1">
<mxGeometry height="8" width="8" x="630" y="267" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EC4899;strokeColor=none;" value="" vertex="1">
<mxGeometry height="8" width="8" x="630" y="292" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
3.4 两种实现对比分析表
| 对比维度 | 面向过程 | 面向对象 |
|---|---|---|
| 数据存放 | 分散在各处(局部变量/参数) | 封装在对象内部(属性) |
| 数据安全 | 任何函数都能修改 | private 保护,需通过方法访问 |
| 数据验证 | 需在每个函数中验证 | 集中在 setter/构造方法验证 |
| 代码复用 | 复制函数 | 创建新对象 / 继承 |
| 扩展能力 | 修改原函数 | 子类重写 / 新增类 |
| 可读性 | 需追踪函数调用链 | 对象职责清晰 |
四、OOP 三大优势详解
4.1 优势总览图
<mxfile>
<diagram id="oop-advantages" name="OOP三大核心优势">
<mxGraphModel dx="900" dy="600" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="600">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#94A3B8;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="60" y="40" width="780" height="500" as="geometry" />
</mxCell>
<mxCell id="3" value="OOP 三大核心优势" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="60" y="40" width="780" height="50" as="geometry" />
</mxCell>
<mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" vertex="1" parent="1">
<mxGeometry x="100" y="120" width="200" height="160" as="geometry" />
</mxCell>
<mxCell id="5" value="易维护" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#1E40AF;" vertex="1" parent="1">
<mxGeometry x="100" y="135" width="200" height="35" as="geometry" />
</mxCell>
<mxCell id="6" value="Maintainable" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontColor=#3B82F6;" vertex="1" parent="1">
<mxGeometry x="100" y="165" width="200" height="25" as="geometry" />
</mxCell>
<mxCell id="7" value="" style="line;strokeWidth=2;html=1;strokeColor=#3B82F6;" vertex="1" parent="1">
<mxGeometry x="120" y="195" width="160" height="10" as="geometry" />
</mxCell>
<mxCell id="8" value="封装性" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="130" y="215" width="140" height="32" as="geometry" />
</mxCell>
<mxCell id="9" value="Encapsulation" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#64748B;" vertex="1" parent="1">
<mxGeometry x="130" y="250" width="140" height="20" as="geometry" />
</mxCell>
<mxCell id="10" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;shadow=1;" vertex="1" parent="1">
<mxGeometry x="350" y="120" width="200" height="160" as="geometry" />
</mxCell>
<mxCell id="11" value="易复用" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#166534;" vertex="1" parent="1">
<mxGeometry x="350" y="135" width="200" height="35" as="geometry" />
</mxCell>
<mxCell id="12" value="Reusable" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontColor=#22C55E;" vertex="1" parent="1">
<mxGeometry x="350" y="165" width="200" height="25" as="geometry" />
</mxCell>
<mxCell id="13" value="" style="line;strokeWidth=2;html=1;strokeColor=#22C55E;" vertex="1" parent="1">
<mxGeometry x="370" y="195" width="160" height="10" as="geometry" />
</mxCell>
<mxCell id="14" value="继承性" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="380" y="215" width="140" height="32" as="geometry" />
</mxCell>
<mxCell id="15" value="Inheritance" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#64748B;" vertex="1" parent="1">
<mxGeometry x="380" y="250" width="140" height="20" as="geometry" />
</mxCell>
<mxCell id="16" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3E8FF;strokeColor=#A855F7;strokeWidth=3;shadow=1;" vertex="1" parent="1">
<mxGeometry x="600" y="120" width="200" height="160" as="geometry" />
</mxCell>
<mxCell id="17" value="易扩展" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#7C3AED;" vertex="1" parent="1">
<mxGeometry x="600" y="135" width="200" height="35" as="geometry" />
</mxCell>
<mxCell id="18" value="Extensible" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontColor=#A855F7;" vertex="1" parent="1">
<mxGeometry x="600" y="165" width="200" height="25" as="geometry" />
</mxCell>
<mxCell id="19" value="" style="line;strokeWidth=2;html=1;strokeColor=#A855F7;" vertex="1" parent="1">
<mxGeometry x="620" y="195" width="160" height="10" as="geometry" />
</mxCell>
<mxCell id="20" value="多态性" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="630" y="215" width="140" height="32" as="geometry" />
</mxCell>
<mxCell id="21" value="Polymorphism" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#64748B;" vertex="1" parent="1">
<mxGeometry x="630" y="250" width="140" height="20" as="geometry" />
</mxCell>
<mxCell id="22" value="" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#3B82F6;endFill=1;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="200" y="290" as="sourcePoint" />
<mxPoint x="200" y="350" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="23" value="" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#22C55E;endFill=1;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="450" y="290" as="sourcePoint" />
<mxPoint x="450" y="350" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="24" value="" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#A855F7;endFill=1;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="700" y="290" as="sourcePoint" />
<mxPoint x="700" y="350" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="25" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#3B82F6;strokeWidth=2;strokeDasharray=3 3;" vertex="1" parent="1">
<mxGeometry x="100" y="360" width="200" height="80" as="geometry" />
</mxCell>
<mxCell id="26" value="修改局部不<br>影响整体" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#1E40AF;" vertex="1" parent="1">
<mxGeometry x="100" y="375" width="200" height="50" as="geometry" />
</mxCell>
<mxCell id="27" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;strokeDasharray=3 3;" vertex="1" parent="1">
<mxGeometry x="350" y="360" width="200" height="80" as="geometry" />
</mxCell>
<mxCell id="28" value="代码无需重复<br>编写" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#166534;" vertex="1" parent="1">
<mxGeometry x="350" y="375" width="200" height="50" as="geometry" />
</mxCell>
<mxCell id="29" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#A855F7;strokeWidth=2;strokeDasharray=3 3;" vertex="1" parent="1">
<mxGeometry x="600" y="360" width="200" height="80" as="geometry" />
</mxCell>
<mxCell id="30" value="新增功能无需<br>修改原代码" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#7C3AED;" vertex="1" parent="1">
<mxGeometry x="600" y="375" width="200" height="50" as="geometry" />
</mxCell>
<mxCell id="31" value="🛡️" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" vertex="1" parent="1">
<mxGeometry x="100" y="460" width="200" height="50" as="geometry" />
</mxCell>
<mxCell id="32" value="♻️" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" vertex="1" parent="1">
<mxGeometry x="350" y="460" width="200" height="50" as="geometry" />
</mxCell>
<mxCell id="33" value="🚀" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" vertex="1" parent="1">
<mxGeometry x="600" y="460" width="200" height="50" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
4.2 易维护(封装带来的好处)
问题场景:修改 π 的精度
面向过程的困境:
// 问题:π 值散落在各处,修改需要全局搜索替换
public class ProcedureProblem {
public static void main(String[] args) {
double area1 = 3.14 * 5 * 5; // 这里用了 3.14
double area2 = 3.14 * 10 * 10; // 这里也用了 3.14
double circ = 2 * 3.14 * 5; // 这里还有 3.14
// 如果要改成 3.14159,需要改 N 处,容易遗漏!
}
}
面向对象的解决方案:
public class Circle {
private double radius;
// ✅ 常量集中定义,修改一处即可
private static final double PI = 3.14159265358979;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return PI * radius * radius; // 自动使用新精度
}
public double getCircumference() {
return 2 * PI * radius; // 自动使用新精度
}
}
<mxfile>
<diagram id="encapsulation-benefits" name="封装的好处">
<mxGraphModel dx="1665" dy="796" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="580" 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=#F8FAFC;strokeColor=#94A3B8;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="490" width="740" x="80" y="40" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="封装的好处" vertex="1">
<mxGeometry height="50" width="740" x="80" y="40" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#EF4444;strokeWidth=2;dashed=1;" value="" vertex="1">
<mxGeometry height="70" width="300" x="120" y="115" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="修改前" vertex="1">
<mxGeometry height="24" width="60" x="130" y="125" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#DC2626;rounded=1;strokeWidth=1;" value="PI = 3.14" vertex="1">
<mxGeometry height="50" width="200" x="200" y="125" as="geometry" />
</mxCell>
<mxCell id="7" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#F59E0B;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="270" y="195" as="sourcePoint" />
<mxPoint x="270" y="245" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#B45309;rounded=1;" value="只需修改一处" vertex="1">
<mxGeometry height="30" width="120" x="290" y="205" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="70" width="300" x="120" y="255" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="修改后" vertex="1">
<mxGeometry height="24" width="60" x="130" y="265" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#166534;rounded=1;strokeWidth=1;" value="PI = 3.14159265358979" vertex="1">
<mxGeometry height="50" width="210" x="200" y="265" as="geometry" />
</mxCell>
<mxCell id="12" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#22C55E;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="270" y="335" as="sourcePoint" />
<mxPoint x="270" y="385" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#166534;rounded=1;" value="所有方法自动生效" vertex="1">
<mxGeometry height="30" width="140" x="290" y="345" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="50" width="180" x="120" y="395" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#1E40AF;" value="getArea()" vertex="1">
<mxGeometry height="50" width="180" x="120" y="395" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="50" width="180" x="120" y="460" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#1E40AF;" value="getCircumference()" vertex="1">
<mxGeometry height="50" width="180" x="120" y="460" as="geometry" />
</mxCell>
<mxCell id="18" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#3B82F6;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="310" y="420" as="sourcePoint" />
<mxPoint x="420" y="420" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="19" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#3B82F6;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="310" y="485" as="sourcePoint" />
<mxPoint x="420" y="485" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="50" width="140" x="430" y="395" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#166534;" value="使用新精度 ✓" vertex="1">
<mxGeometry height="50" width="140" x="430" y="395" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="50" width="140" x="430" y="460" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#166534;" value="使用新精度 ✓" vertex="1">
<mxGeometry height="50" width="140" x="430" y="460" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="210" width="280" x="500" y="115" as="geometry" />
</mxCell>
<mxCell id="25" 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="28" width="100" x="510" y="125" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#92400E;spacingLeft=10;" value="1. 集中管理" vertex="1">
<mxGeometry height="30" width="120" x="510" y="155" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#78716C;spacingLeft=20;" value="常量定义在一处" vertex="1">
<mxGeometry height="25" width="150" x="510" y="180" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#92400E;spacingLeft=10;" value="2. 一改全改" vertex="1">
<mxGeometry height="30" width="120" x="510" y="210" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#78716C;spacingLeft=20;" value="修改一处,全局生效" vertex="1">
<mxGeometry height="25" width="150" x="510" y="235" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#92400E;spacingLeft=10;" value="3. 减少错误" vertex="1">
<mxGeometry height="30" width="120" x="510" y="265" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#78716C;spacingLeft=20;" value="避免遗漏修改" vertex="1">
<mxGeometry height="25" width="150" x="510" y="290" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=36;" value="🔄" vertex="1">
<mxGeometry height="60" width="60" x="700" y="200" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="封装 = 易维护" vertex="1">
<mxGeometry height="90" width="180" x="600" y="420" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
4.3 易复用(继承带来的好处)
代码示例:形状类继承体系
// ===== 父类:通用形状 =====
abstract class Shape {
protected String color;
protected String name;
public Shape(String name, String color) {
this.name = name;
this.color = color;
}
// 通用方法(子类自动继承)
public void displayInfo() {
System.out.println("图形:" + name + ",颜色:" + color);
}
// 抽象方法(子类必须实现)
public abstract double getArea();
}
// ===== 子类:圆形 =====
class Circle extends Shape {
private double radius;
public Circle(String color, double radius) {
super("圆形", color); // 调用父类构造
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
// ===== 子类:矩形 =====
class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(String color, double width, double height) {
super("矩形", color);
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
}
继承关系图:
<mxfile>
<diagram id="shape-inheritance" name="Shape继承关系">
<mxGraphModel dx="1388" dy="663" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="600" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="swimlane;fontStyle=1;align=center;verticalAlign=top;childLayout=stackLayout;horizontal=1;startSize=30;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=0;marginBottom=0;fillColor=#6366F1;strokeColor=#4F46E5;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;swimlaneFillColor=#EEF2FF;" value="Shape" vertex="1">
<mxGeometry height="160" width="250" x="325" y="40" as="geometry" />
</mxCell>
<mxCell id="3" parent="2" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=10;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#4338CA;fontSize=14;fontStyle=0;" value="# color: String
# name: String" vertex="1">
<mxGeometry height="50" width="250" y="30" as="geometry" />
</mxCell>
<mxCell id="4" parent="2" style="line;strokeWidth=1;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#4F46E5;" value="" vertex="1">
<mxGeometry height="8" width="250" y="80" as="geometry" />
</mxCell>
<mxCell id="5" parent="2" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=10;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#4338CA;fontSize=14;fontStyle=2;" value="+ displayInfo()
+ getArea(): double" vertex="1">
<mxGeometry height="72" width="250" y="88" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#C7D2FE;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#4338CA;rounded=1;" value="(父类)" vertex="1">
<mxGeometry height="20" width="50" x="515" y="44" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=left;verticalAlign=middle;fontSize=12;fontStyle=0;fontColor=#B45309;rounded=1;spacingLeft=5;" value="← 子类自动继承" vertex="1">
<mxGeometry height="24" width="110" x="585" y="128" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=left;verticalAlign=middle;fontSize=12;fontStyle=0;fontColor=#DC2626;rounded=1;spacingLeft=5;" value="← 抽象方法" vertex="1">
<mxGeometry height="24" width="90" x="585" y="158" as="geometry" />
</mxCell>
<mxCell id="9" edge="1" parent="1" style="endArrow=none;html=1;strokeWidth=2;strokeColor=#6366F1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="450" y="260" as="sourcePoint" />
<mxPoint x="450" y="200" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="10" edge="1" parent="1" style="endArrow=none;html=1;strokeWidth=2;strokeColor=#6366F1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="220" y="260" as="sourcePoint" />
<mxPoint x="680" y="260" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="11" edge="1" parent="1" style="endArrow=block;html=1;strokeWidth=2;strokeColor=#22C55E;endFill=0;endSize=12;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="220" y="310" as="sourcePoint" />
<mxPoint x="220" y="260" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="12" edge="1" parent="1" style="endArrow=block;html=1;strokeWidth=2;strokeColor=#3B82F6;endFill=0;endSize=12;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="680" y="310" as="sourcePoint" />
<mxPoint x="680" y="260" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="13" parent="1" style="swimlane;fontStyle=1;align=center;verticalAlign=top;childLayout=stackLayout;horizontal=1;startSize=30;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=0;marginBottom=0;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;swimlaneFillColor=#F0FDF4;" value="Circle" vertex="1">
<mxGeometry height="160" width="250" x="95" y="310" as="geometry" />
</mxCell>
<mxCell id="14" parent="13" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=10;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#166534;fontSize=14;" value="- radius: double" vertex="1">
<mxGeometry height="35" width="250" y="30" as="geometry" />
</mxCell>
<mxCell id="15" parent="13" style="line;strokeWidth=1;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#16A34A;" value="" vertex="1">
<mxGeometry height="8" width="250" y="65" as="geometry" />
</mxCell>
<mxCell id="16" parent="13" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=10;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#166534;fontSize=14;" value="+ getArea(): double" vertex="1">
<mxGeometry height="35" width="250" y="73" as="geometry" />
</mxCell>
<mxCell id="17" parent="13" style="text;strokeColor=none;fillColor=#DCFCE7;align=left;verticalAlign=top;spacingLeft=20;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#166534;fontSize=14;fontStyle=2;" value="→ π × r²" vertex="1">
<mxGeometry height="52" width="250" y="108" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#BBF7D0;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#166534;rounded=1;" value="(子类)" vertex="1">
<mxGeometry height="20" width="50" x="288" y="314" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="swimlane;fontStyle=1;align=center;verticalAlign=top;childLayout=stackLayout;horizontal=1;startSize=30;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=0;marginBottom=0;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;swimlaneFillColor=#EFF6FF;" value="Rectangle" vertex="1">
<mxGeometry height="180" width="250" x="555" y="310" as="geometry" />
</mxCell>
<mxCell id="20" parent="19" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=10;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#1E40AF;fontSize=14;" value="- width: double
- height: double" vertex="1">
<mxGeometry height="50" width="250" y="30" as="geometry" />
</mxCell>
<mxCell id="21" parent="19" style="line;strokeWidth=1;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#2563EB;" value="" vertex="1">
<mxGeometry height="8" width="250" y="80" as="geometry" />
</mxCell>
<mxCell id="22" parent="19" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=10;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#1E40AF;fontSize=14;" value="+ getArea(): double" vertex="1">
<mxGeometry height="35" width="250" y="88" as="geometry" />
</mxCell>
<mxCell id="23" parent="19" style="text;strokeColor=none;fillColor=#DBEAFE;align=left;verticalAlign=top;spacingLeft=20;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#1E40AF;fontSize=14;fontStyle=2;" value="→ width × height" vertex="1">
<mxGeometry height="57" width="250" y="123" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#BFDBFE;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="(子类)" vertex="1">
<mxGeometry height="20" width="50" x="745" y="314" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#F3E8FF;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#7C3AED;rounded=1;" value="继承" vertex="1">
<mxGeometry height="20" width="40" x="160" y="270" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=#F3E8FF;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#7C3AED;rounded=1;" value="继承" vertex="1">
<mxGeometry height="20" width="40" x="700" y="270" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="60" width="710" x="95" y="510" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#92400E;" value="💡 继承优势:子类自动获得父类的属性(color, name)和方法(displayInfo),只需实现抽象方法 getArea()" vertex="1">
<mxGeometry height="60" width="710" x="95" y="510" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
复用统计:
| 类 | 自己编写的代码 | 从父类继承的代码 |
|---|---|---|
| Shape | 2个属性 + 2个方法 | - |
| Circle | 1个属性 + 1个方法 | 2个属性 + 1个方法 |
| Rectangle | 2个属性 + 1个方法 | 2个属性 + 1个方法 |
✅ 子类无需重复编写
color、name属性和displayInfo()方法
4.4 易扩展(多态带来的好处)
场景:新增三角形,无需修改已有代码
// ===== 新增子类:三角形 =====
class Triangle extends Shape {
private double base;
private double height;
public Triangle(String color, double base, double height) {
super("三角形", color);
this.base = base;
this.height = height;
}
@Override
public double getArea() {
return 0.5 * base * height;
}
}
// ===== 使用多态统一处理 =====
public class ShapeDemo {
public static void main(String[] args) {
// 父类引用指向子类对象(多态)
Shape[] shapes = {
new Circle("红色", 5),
new Rectangle("蓝色", 4, 6),
new Triangle("绿色", 3, 4) // ✅ 新增的三角形
};
// 统一处理所有形状
printAllAreas(shapes);
}
// ✅ 这个方法完全不需要修改!
public static void printAllAreas(Shape[] shapes) {
for (Shape shape : shapes) {
shape.displayInfo();
System.out.println("面积:" + shape.getArea());
System.out.println("---");
}
}
}
多态执行流程图:
<mxfile>
<diagram id="polymorphism-process" name="多态调用过程">
<mxGraphModel dx="1665" dy="796" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="680" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#6366F1;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="600" width="740" x="80" y="40" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="多态调用过程" vertex="1">
<mxGeometry height="50" width="740" x="80" y="40" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EEF2FF;strokeColor=#A5B4FC;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="60" width="660" x="120" y="110" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#4338CA;fontFamily=Courier New;" value="Shape shape = new Circle("红色", 5);" vertex="1">
<mxGeometry height="60" width="660" x="120" y="110" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="50" width="660" x="120" y="190" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#B45309;fontFamily=Courier New;spacingLeft=20;" value="shape.getArea();" vertex="1">
<mxGeometry height="50" width="300" x="155" y="190" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=2;fontColor=#92400E;spacingLeft=10;" value="// 调用哪个方法?" vertex="1">
<mxGeometry height="50" width="200" x="450" y="190" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="❓" vertex="1">
<mxGeometry height="50" width="50" x="720" y="190" as="geometry" />
</mxCell>
<mxCell id="10" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#6366F1;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="450" y="250" as="sourcePoint" />
<mxPoint x="450" y="300" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="90" width="620" x="140" y="310" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="编译时" vertex="1">
<mxGeometry height="28" width="70" x="155" y="320" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=0;fontColor=#1E40AF;" value="检查 Shape 类是否有 getArea() 方法" vertex="1">
<mxGeometry height="35" width="400" x="240" y="320" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="✓" vertex="1">
<mxGeometry height="35" width="50" x="690" y="320" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=#EFF6FF;align=center;verticalAlign=middle;fontSize=13;fontStyle=2;fontColor=#3B82F6;rounded=1;" value="🔍 静态类型检查:编译器只看声明类型 Shape" vertex="1">
<mxGeometry height="30" width="500" x="200" y="360" as="geometry" />
</mxCell>
<mxCell id="16" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#6366F1;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="450" y="410" as="sourcePoint" />
<mxPoint x="450" y="460" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#C7D2FE;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#4338CA;rounded=1;" value="STEP 1" vertex="1">
<mxGeometry height="20" width="50" x="460" y="270" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#C7D2FE;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#4338CA;rounded=1;" value="STEP 2" vertex="1">
<mxGeometry height="20" width="50" x="460" y="425" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="160" width="620" x="140" y="470" as="geometry" />
</mxCell>
<mxCell id="20" 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="70" x="155" y="480" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontStyle=0;fontColor=#166534;spacingLeft=10;" value="查看实际对象类型是" vertex="1">
<mxGeometry height="35" width="180" x="240" y="478" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Circle" vertex="1">
<mxGeometry height="28" width="80" x="420" y="483" as="geometry" />
</mxCell>
<mxCell id="23" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#166534;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="280" y="530" as="sourcePoint" />
<mxPoint x="330" y="530" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#166534;rounded=1;strokeWidth=1;strokeColor=#22C55E;" value="调用 Circle.getArea()" vertex="1">
<mxGeometry height="30" width="200" x="340" y="518" as="geometry" />
</mxCell>
<mxCell id="25" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#166534;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="280" y="575" as="sourcePoint" />
<mxPoint x="330" y="575" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontStyle=0;fontColor=#166534;" value="返回" vertex="1">
<mxGeometry height="30" width="50" x="340" y="560" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#B45309;rounded=1;" value="π × 5² = 78.54" vertex="1">
<mxGeometry height="35" width="150" x="400" y="558" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0FDF4;align=center;verticalAlign=middle;fontSize=13;fontStyle=2;fontColor=#166534;rounded=1;" value="🎯 动态绑定:JVM 根据实际对象类型决定调用哪个方法" vertex="1">
<mxGeometry height="25" width="500" x="200" y="598" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="🚀" vertex="1">
<mxGeometry height="50" width="50" x="700" y="555" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
开闭原则示意图:
<mxfile>
<diagram id="open-closed-principle" name="开闭原则">
<mxGraphModel dx="1148" dy="549" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="600" 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=#F8FAFC;strokeColor=#6366F1;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="520" width="780" x="60" y="40" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="开闭原则 (Open-Closed Principle)" vertex="1">
<mxGeometry height="55" width="780" x="60" y="40" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="420" width="300" x="100" y="120" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="对扩展开放" vertex="1">
<mxGeometry height="50" width="300" x="100" y="120" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=2;fontColor=#166534;" value="Open for Extension" vertex="1">
<mxGeometry height="30" width="300" x="100" y="170" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#22C55E;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#166534;align=center;" value="✨ 新增 Triangle" vertex="1">
<mxGeometry height="50" width="240" x="130" y="220" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#22C55E;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#166534;align=center;" value="✨ 新增 Pentagon" vertex="1">
<mxGeometry height="50" width="240" x="130" y="285" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#22C55E;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#166534;align=center;" value="✨ 新增 Hexagon" vertex="1">
<mxGeometry height="50" width="240" x="130" y="350" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#22C55E;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#166534;align=center;" value="✨ ......" vertex="1">
<mxGeometry height="50" width="240" x="130" y="415" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=#BBF7D0;align=center;verticalAlign=middle;fontSize=13;fontStyle=2;fontColor=#166534;rounded=1;" value="🚀 可以自由添加新的形状类" vertex="1">
<mxGeometry height="40" width="270" x="115" y="480" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="420" width="300" x="500" y="120" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="对修改关闭" vertex="1">
<mxGeometry height="50" width="300" x="500" y="120" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=2;fontColor=#DC2626;" value="Closed for Modification" vertex="1">
<mxGeometry height="30" width="300" x="500" y="170" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#EF4444;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="200" width="240" x="530" y="220" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#B45309;rounded=1;" value="printAllAreas" vertex="1">
<mxGeometry height="35" width="170" x="565" y="235" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#DC2626;" value="方法无需修改" vertex="1">
<mxGeometry height="30" width="240" x="530" y="280" as="geometry" />
</mxCell>
<mxCell id="18" 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=#FECACA;" value="" vertex="1">
<mxGeometry height="10" width="200" x="550" y="315" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=0;fontColor=#991B1B;" value="Shape[] 处理" vertex="1">
<mxGeometry height="30" width="240" x="530" y="330" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=0;fontColor=#991B1B;" value="逻辑不变" vertex="1">
<mxGeometry height="30" width="240" x="530" y="360" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=36;" value="🔒" vertex="1">
<mxGeometry height="40" width="60" x="620" y="385" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#FECACA;align=center;verticalAlign=middle;fontSize=13;fontStyle=2;fontColor=#DC2626;rounded=1;" value="🛡️ 现有代码保持稳定不变" vertex="1">
<mxGeometry height="40" width="270" x="515" y="480" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="但" vertex="1">
<mxGeometry height="40" width="60" x="420" y="260" as="geometry" />
</mxCell>
<mxCell id="24" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=4;strokeColor=#6366F1;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<Array as="points">
<mxPoint x="420" y="315" />
<mxPoint x="440" y="315" />
<mxPoint x="480" y="315" />
</Array>
<mxPoint x="380" y="315" as="sourcePoint" />
<mxPoint x="510" y="315" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="25" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=2;fontColor=#FFFFFF;fontSize=24;fontStyle=1;" value="+" vertex="1">
<mxGeometry height="40" width="40" x="345" y="140" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontColor=#FFFFFF;fontSize=28;fontStyle=1;" value="−" vertex="1">
<mxGeometry height="40" width="40" x="515" y="140" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
五、性能误区澄清
5.1 常见错误认知
┌─────────────────────────────────────────────────────────────────┐
│ ❌ 错误观点 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ "面向过程性能更高,面向对象性能差" │
│ │
│ "游戏/嵌入式开发必须用面向过程" │
│ │
│ "OOP 的对象创建开销太大" │
│ │
└─────────────────────────────────────────────────────────────────┘
5.2 事实分析表
| 观点 | 事实 | 说明 |
|---|---|---|
| OOP 有额外开销 | ⚠️ 部分正确 | 对象创建、方法调用有微小开销 |
| 开销影响性能 | ❌ 通常错误 | 现代 JVM 优化后几乎可忽略 |
| POP 一定更快 | ❌ 错误 | 取决于具体实现,而非范式 |
| 性能瓶颈在 OOP | ❌ 错误 | 99% 瓶颈在算法和 I/O |
5.3 JVM 优化技术
<mxfile>
<diagram id="jvm-oop-optimization" name="JVM对OOP的优化">
<mxGraphModel dx="900" dy="700" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#6366F1;strokeWidth=3;" vertex="1" parent="1">
<mxGeometry x="50" y="30" width="800" height="640" as="geometry" />
</mxCell>
<mxCell id="3" value="JVM 对 OOP 的优化" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="50" y="30" width="800" height="55" as="geometry" />
</mxCell>
<mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;" vertex="1" parent="1">
<mxGeometry x="80" y="105" width="740" height="165" as="geometry" />
</mxCell>
<mxCell id="5" value="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;strokeWidth=2;fontColor=#FFFFFF;fontSize=18;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="100" y="115" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="6" value="方法内联 (Method Inlining)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#1E40AF;spacingLeft=10;" vertex="1" parent="1">
<mxGeometry x="150" y="115" width="300" height="40" as="geometry" />
</mxCell>
<mxCell id="7" value="⚡" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" vertex="1" parent="1">
<mxGeometry x="750" y="115" width="50" height="40" as="geometry" />
</mxCell>
<mxCell id="8" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#93C5FD;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="100" y="165" width="700" height="90" as="geometry" />
</mxCell>
<mxCell id="9" value="优化前" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#DC2626;rounded=1;" vertex="1" parent="1">
<mxGeometry x="120" y="175" width="70" height="28" as="geometry" />
</mxCell>
<mxCell id="10" value="circle.getArea()" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#B45309;rounded=1;fontFamily=Courier New;" vertex="1" parent="1">
<mxGeometry x="200" y="173" width="160" height="32" as="geometry" />
</mxCell>
<mxCell id="11" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#9CA3AF;endFill=1;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="370" y="189" as="sourcePoint" />
<mxPoint x="420" y="189" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="12" value="方法调用开销" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#DC2626;rounded=1;" vertex="1" parent="1">
<mxGeometry x="430" y="175" width="120" height="28" as="geometry" />
</mxCell>
<mxCell id="13" value="❌" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;" vertex="1" parent="1">
<mxGeometry x="560" y="173" width="40" height="32" as="geometry" />
</mxCell>
<mxCell id="14" value="优化后" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#16A34A;rounded=1;" vertex="1" parent="1">
<mxGeometry x="120" y="215" width="70" height="28" as="geometry" />
</mxCell>
<mxCell id="15" value="直接执行 PI * r * r" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#B45309;rounded=1;fontFamily=Courier New;" vertex="1" parent="1">
<mxGeometry x="200" y="213" width="180" height="32" as="geometry" />
</mxCell>
<mxCell id="16" value="" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#9CA3AF;endFill=1;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="390" y="229" as="sourcePoint" />
<mxPoint x="440" y="229" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="17" value="无调用开销" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#16A34A;rounded=1;" vertex="1" parent="1">
<mxGeometry x="450" y="215" width="100" height="28" as="geometry" />
</mxCell>
<mxCell id="18" value="✓" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="560" y="215" width="35" height="28" as="geometry" />
</mxCell>
<mxCell id="19" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;" vertex="1" parent="1">
<mxGeometry x="80" y="290" width="740" height="165" as="geometry" />
</mxCell>
<mxCell id="20" value="2" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=2;fontColor=#FFFFFF;fontSize=18;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="100" y="300" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="21" value="逃逸分析 (Escape Analysis)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#B45309;spacingLeft=10;" vertex="1" parent="1">
<mxGeometry x="150" y="300" width="300" height="40" as="geometry" />
</mxCell>
<mxCell id="22" value="🔍" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" vertex="1" parent="1">
<mxGeometry x="750" y="300" width="50" height="40" as="geometry" />
</mxCell>
<mxCell id="23" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FCD34D;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="100" y="350" width="700" height="90" as="geometry" />
</mxCell>
<mxCell id="24" value="如果对象不会逃逸出方法" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#1E40AF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="120" y="370" width="200" height="35" as="geometry" />
</mxCell>
<mxCell id="25" value="" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#F59E0B;endFill=1;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="330" y="387" as="sourcePoint" />
<mxPoint x="380" y="387" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="26" value="可在栈上分配" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#B45309;rounded=1;" vertex="1" parent="1">
<mxGeometry x="390" y="370" width="130" height="35" as="geometry" />
</mxCell>
<mxCell id="27" value="" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#F59E0B;endFill=1;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="530" y="387" as="sourcePoint" />
<mxPoint x="580" y="387" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="28" value="无 GC 开销" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#16A34A;rounded=1;" vertex="1" parent="1">
<mxGeometry x="590" y="370" width="110" height="35" as="geometry" />
</mxCell>
<mxCell id="29" value="✓" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="710" y="373" width="35" height="28" as="geometry" />
</mxCell>
<mxCell id="30" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;" vertex="1" parent="1">
<mxGeometry x="80" y="475" width="740" height="175" as="geometry" />
</mxCell>
<mxCell id="31" value="3" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=2;fontColor=#FFFFFF;fontSize=18;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="100" y="485" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="32" value="JIT 编译 (Just-In-Time Compilation)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#166534;spacingLeft=10;" vertex="1" parent="1">
<mxGeometry x="150" y="485" width="350" height="40" as="geometry" />
</mxCell>
<mxCell id="33" value="🚀" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" vertex="1" parent="1">
<mxGeometry x="750" y="485" width="50" height="40" as="geometry" />
</mxCell>
<mxCell id="34" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#86EFAC;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="100" y="535" width="700" height="100" as="geometry" />
</mxCell>
<mxCell id="35" value="热点代码" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#DC2626;rounded=1;" vertex="1" parent="1">
<mxGeometry x="130" y="565" width="100" height="35" as="geometry" />
</mxCell>
<mxCell id="36" value="" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#22C55E;endFill=1;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="240" y="582" as="sourcePoint" />
<mxPoint x="290" y="582" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="37" value="编译为本地机器码" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#B45309;rounded=1;" vertex="1" parent="1">
<mxGeometry x="300" y="565" width="160" height="35" as="geometry" />
</mxCell>
<mxCell id="38" value="" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#22C55E;endFill=1;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="470" y="582" as="sourcePoint" />
<mxPoint x="520" y="582" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="39" value="性能接近 C/C++" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#166534;rounded=1;" vertex="1" parent="1">
<mxGeometry x="530" y="565" width="150" height="35" as="geometry" />
</mxCell>
<mxCell id="40" value="🏆" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" vertex="1" parent="1">
<mxGeometry x="700" y="560" width="50" height="45" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
5.4 真正的性能瓶颈
<mxfile>
<diagram id="performance-bottleneck" name="性能瓶颈分布">
<mxGraphModel dx="900" dy="680" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="680">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#6366F1;strokeWidth=3;" vertex="1" parent="1">
<mxGeometry x="50" y="30" width="800" height="620" as="geometry" />
</mxCell>
<mxCell id="3" value="性能瓶颈分布" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="50" y="30" width="800" height="60" as="geometry" />
</mxCell>
<mxCell id="4" value="I/O 操作" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#1F2937;" vertex="1" parent="1">
<mxGeometry x="90" y="115" width="100" height="40" as="geometry" />
</mxCell>
<mxCell id="5" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=none;" vertex="1" parent="1">
<mxGeometry x="200" y="120" width="540" height="35" as="geometry" />
</mxCell>
<mxCell id="6" value="90%" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;" vertex="1" parent="1">
<mxGeometry x="430" y="120" width="60" height="35" as="geometry" />
</mxCell>
<mxCell id="7" value="🔥" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=22;" vertex="1" parent="1">
<mxGeometry x="750" y="117" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="8" value="网络请求" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#1F2937;" vertex="1" parent="1">
<mxGeometry x="90" y="175" width="100" height="40" as="geometry" />
</mxCell>
<mxCell id="9" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F97316;strokeColor=none;" vertex="1" parent="1">
<mxGeometry x="200" y="180" width="420" height="35" as="geometry" />
</mxCell>
<mxCell id="10" value="70%" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;" vertex="1" parent="1">
<mxGeometry x="370" y="180" width="60" height="35" as="geometry" />
</mxCell>
<mxCell id="11" value="⚠️" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=22;" vertex="1" parent="1">
<mxGeometry x="630" y="177" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="12" value="数据库查询" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#1F2937;" vertex="1" parent="1">
<mxGeometry x="90" y="235" width="100" height="40" as="geometry" />
</mxCell>
<mxCell id="13" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=none;" vertex="1" parent="1">
<mxGeometry x="200" y="240" width="330" height="35" as="geometry" />
</mxCell>
<mxCell id="14" value="55%" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;" vertex="1" parent="1">
<mxGeometry x="330" y="240" width="60" height="35" as="geometry" />
</mxCell>
<mxCell id="15" value="算法效率" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#1F2937;" vertex="1" parent="1">
<mxGeometry x="90" y="295" width="100" height="40" as="geometry" />
</mxCell>
<mxCell id="16" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=none;" vertex="1" parent="1">
<mxGeometry x="200" y="300" width="210" height="35" as="geometry" />
</mxCell>
<mxCell id="17" value="35%" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;" vertex="1" parent="1">
<mxGeometry x="270" y="300" width="60" height="35" as="geometry" />
</mxCell>
<mxCell id="18" value="OOP 开销" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#1F2937;" vertex="1" parent="1">
<mxGeometry x="90" y="355" width="100" height="40" as="geometry" />
</mxCell>
<mxCell id="19" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=none;" vertex="1" parent="1">
<mxGeometry x="200" y="360" width="24" height="35" as="geometry" />
</mxCell>
<mxCell id="20" value="2%" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#22C55E;" vertex="1" parent="1">
<mxGeometry x="230" y="360" width="50" height="35" as="geometry" />
</mxCell>
<mxCell id="21" value="✓" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="280" y="363" width="30" height="28" as="geometry" />
</mxCell>
<mxCell id="22" value="" style="line;strokeWidth=2;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=#E5E7EB;" vertex="1" parent="1">
<mxGeometry x="200" y="415" width="540" height="10" as="geometry" />
</mxCell>
<mxCell id="23" value="0%" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#6B7280;" vertex="1" parent="1">
<mxGeometry x="180" y="425" width="40" height="25" as="geometry" />
</mxCell>
<mxCell id="24" value="50%" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#6B7280;" vertex="1" parent="1">
<mxGeometry x="450" y="425" width="40" height="25" as="geometry" />
</mxCell>
<mxCell id="25" value="100%" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#6B7280;" vertex="1" parent="1">
<mxGeometry x="710" y="425" width="50" height="25" as="geometry" />
</mxCell>
<mxCell id="26" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=3;" vertex="1" parent="1">
<mxGeometry x="90" y="470" width="720" height="160" as="geometry" />
</mxCell>
<mxCell id="27" value="💡 结论" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="90" y="470" width="100" height="40" as="geometry" />
</mxCell>
<mxCell id="28" value="OOP 的开销在整体性能中占比极小" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#166534;" vertex="1" parent="1">
<mxGeometry x="90" y="520" width="720" height="40" as="geometry" />
</mxCell>
<mxCell id="29" value="优化应聚焦于" style="text;html=1;strokeColor=none;fillColor=none;align=right;verticalAlign=middle;fontSize=18;fontStyle=0;fontColor=#374151;" vertex="1" parent="1">
<mxGeometry x="150" y="565" width="140" height="40" as="geometry" />
</mxCell>
<mxCell id="30" value="I/O" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="300" y="570" width="60" height="35" as="geometry" />
</mxCell>
<mxCell id="31" value="网络" style="text;html=1;strokeColor=none;fillColor=#F97316;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="380" y="570" width="70" height="35" as="geometry" />
</mxCell>
<mxCell id="32" value="数据库" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="470" y="570" width="90" height="35" as="geometry" />
</mxCell>
<mxCell id="33" value="和" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontStyle=0;fontColor=#374151;" vertex="1" parent="1">
<mxGeometry x="570" y="565" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="34" value="算法" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="620" y="570" width="70" height="35" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
5.5 现代语言的实践
| 语言 | 范式支持 | 说明 |
|---|---|---|
| Java | OOP 为主 + 函数式 | 纯 OOP 设计,Java 8 引入函数式 |
| Python | 多范式 | 同时支持 POP、OOP、函数式 |
| C++ | 多范式 | 兼容 C 的 POP,同时支持 OOP |
| JavaScript | 多范式 | 基于原型的 OOP + 函数式 |
| Go | 多范式 | 简化的 OOP(无继承)+ POP |
✅ 结论:现代语言普遍支持多范式,按需选择而非非此即彼
六、选择指南
6.1 场景选择表
| 场景 | 推荐范式 | 原因 |
|---|---|---|
| Shell 脚本 | POP | 简单任务,顺序执行即可 |
| 数据处理脚本 | POP | 一次性任务,无需复用 |
| 小型工具函数 | POP | 逻辑简单,函数组织足够 |
| 中大型项目 | OOP | 便于维护、测试、团队协作 |
| 复杂业务系统 | OOP | 对象建模更贴近业务领域 |
| 框架/库开发 | OOP | 需要高度可扩展和可复用 |
| GUI 应用 | OOP | 界面组件天然适合对象建模 |
6.2 决策流程图
<mxfile>
<diagram id="programming-paradigm-decision" name="如何选择编程范式">
<mxGraphModel dx="1665" dy="796" 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=#F8FAFC;strokeColor=#6366F1;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="710" width="800" x="50" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="如何选择编程范式?" vertex="1">
<mxGeometry height="60" width="800" x="50" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rhombus;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;fontSize=18;fontStyle=1;fontColor=#1E40AF;" value="项目规模?" vertex="1">
<mxGeometry height="80" width="150" x="350" y="100" as="geometry" />
</mxCell>
<mxCell id="5" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#3B82F6;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="350" y="140" as="sourcePoint" />
<mxPoint x="230" y="140" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="6" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#3B82F6;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="425" y="180" as="sourcePoint" />
<mxPoint x="425" y="220" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="7" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#3B82F6;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="500" y="140" as="sourcePoint" />
<mxPoint x="620" y="140" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#B45309;" value="< 500 行" vertex="1">
<mxGeometry height="50" width="110" x="120" y="115" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#B45309;" value="500 ~ 5000 行" vertex="1">
<mxGeometry height="50" width="140" x="355" y="220" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#B45309;" value="> 5000 行" vertex="1">
<mxGeometry height="50" width="110" x="620" y="115" as="geometry" />
</mxCell>
<mxCell id="11" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#22C55E;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="175" y="165" as="sourcePoint" />
<mxPoint x="175" y="210" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="12" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#22C55E;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="675" y="165" as="sourcePoint" />
<mxPoint x="675" y="210" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="13" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#F59E0B;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="425" y="270" as="sourcePoint" />
<mxPoint x="425" y="310" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;fontSize=18;fontStyle=1;fontColor=#166534;" value="POP 优先" vertex="1">
<mxGeometry height="60" width="130" x="110" y="210" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;fontSize=18;fontStyle=1;fontColor=#B45309;" value="视情况而定" vertex="1">
<mxGeometry height="60" width="140" x="355" y="310" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;fontSize=18;fontStyle=1;fontColor=#1E40AF;" value="OOP 优先" vertex="1">
<mxGeometry height="60" width="130" x="610" y="210" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#6B7280;" value="⚡ 简单快速" vertex="1">
<mxGeometry height="30" width="130" x="110" y="275" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#6B7280;" value="🏗️ 可维护性" vertex="1">
<mxGeometry height="30" width="130" x="610" y="275" as="geometry" />
</mxCell>
<mxCell id="19" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#F59E0B;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="425" y="370" as="sourcePoint" />
<mxPoint x="425" y="410" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="20" parent="1" style="rhombus;whiteSpace=wrap;html=1;fillColor=#F3E8FF;strokeColor=#A855F7;strokeWidth=3;fontSize=16;fontStyle=1;fontColor=#7C3AED;" value="需要代码复用吗?" vertex="1">
<mxGeometry height="90" width="210" x="320" y="410" as="geometry" />
</mxCell>
<mxCell id="21" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#22C55E;endFill=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="27" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<Array as="points">
<mxPoint x="175" y="455" />
</Array>
<mxPoint x="320" y="455" as="sourcePoint" />
<mxPoint x="230" y="455" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#166534;rounded=1;" value="是" vertex="1">
<mxGeometry height="25" width="40" x="250" y="440" as="geometry" />
</mxCell>
<mxCell id="23" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#EF4444;endFill=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="28" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<Array as="points">
<mxPoint x="675" y="455" />
</Array>
<mxPoint x="530" y="455" as="sourcePoint" />
<mxPoint x="620" y="455" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#DC2626;rounded=1;" value="否" vertex="1">
<mxGeometry height="25" width="40" x="560" y="440" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1D4ED8;strokeWidth=3;fontSize=20;fontStyle=1;fontColor=#FFFFFF;" value="OOP" vertex="1">
<mxGeometry height="60" width="130" x="110" y="520" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=3;fontSize=20;fontStyle=1;fontColor=#FFFFFF;" value="POP" vertex="1">
<mxGeometry height="60" width="130" x="610" y="520" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#6B7280;" value="🔄 继承 & 多态" vertex="1">
<mxGeometry height="30" width="130" x="110" y="585" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#6B7280;" value="📝 函数式" vertex="1">
<mxGeometry height="30" width="130" x="610" y="585" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;dashed=1;" value="" vertex="1">
<mxGeometry height="90" width="290" x="280" y="620" as="geometry" />
</mxCell>
<mxCell id="32" 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="30" width="70" x="280" y="620" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=0;fontColor=#92400E;whiteSpace=wrap;" value="实际项目中,两种范式往往混合使用
根据具体场景灵活选择最合适的方式" vertex="1">
<mxGeometry height="50" width="270" x="290" y="655" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
七、核心总结
7.1 一句话记忆
┌─────────────────────────────────────────────────────────────────┐
│ │
│ 📌 面向过程关注「怎么做」(步骤) │
│ │
│ 📌 面向对象关注「谁来做」(角色) │
│ │
└─────────────────────────────────────────────────────────────────┘
7.2 核心对比速查表
| 维度 | 面向过程 (POP) | 面向对象 (OOP) |
|---|---|---|
| 核心 | 函数 | 对象 |
| 数据 | 分散 | 封装 |
| 复用 | 复制函数 | 继承/组合 |
| 扩展 | 改原代码 | 加新类 |
| 适合 | 小脚本 | 大项目 |
💬 评论