{"id":839,"date":"2025-07-18T12:00:00","date_gmt":"2025-07-18T12:00:00","guid":{"rendered":"https:\/\/ouyangminwei.com\/?p=839"},"modified":"2025-07-18T09:10:25","modified_gmt":"2025-07-18T09:10:25","slug":"composition-vs-inheritance","status":"publish","type":"post","link":"https:\/\/ouyangminwei.com\/index.php\/2025\/07\/18\/composition-vs-inheritance\/","title":{"rendered":"\u4e92\u52d5\u5f0f\u5831\u544a\uff1a\u7d44\u5408 vs. \u7e7c\u627f"},"content":{"rendered":"\n<!DOCTYPE html>\n<html lang=\"zh-Hant\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <title>\u4e92\u52d5\u5f0f\u5831\u544a\uff1a\u7d44\u5408 vs. \u7e7c\u627f &#8211; Rust \u8207 Go \u7684\u9078\u64c7<\/title>\n    <script src=\"https:\/\/cdn.tailwindcss.com\"><\/script>\n    <script src=\"https:\/\/cdn.jsdelivr.net\/npm\/chart.js\"><\/script>\n    <link rel=\"preconnect\" href=\"https:\/\/fonts.googleapis.com\">\n    <link rel=\"preconnect\" href=\"https:\/\/fonts.gstatic.com\" crossorigin>\n    <link href=\"https:\/\/fonts.googleapis.com\/css2?family=Noto+Sans+TC:wght@400;500;700&#038;display=swap\" rel=\"stylesheet\">\n    <!-- Chosen Palette: Warm Neutrals -->\n    <!-- Application Structure Plan: A single-page dashboard with top navigation for thematic sections (Problem, Solution, Deep Dive, Conclusion). The core is an interactive side-by-side comparison of Go and Rust, where users click topic buttons (e.g., Polymorphism, Code Reuse) to dynamically update the content in both columns. This non-linear, comparative structure is more effective for learning than the linear report format, allowing users to directly contrast the languages on specific features. -->\n    <!-- Visualization & Content Choices: \n        - Inheritance Problems: Goal: Inform. Method: HTML\/CSS Accordion. Interaction: Click to expand. Justification: Reduces initial clutter.\n        - Diamond Diagram: Goal: Organize. Method: HTML\/CSS Divs. Interaction: Static visual. Justification: Clearer than text-only.\n        - Go vs. Rust Comparison: Goal: Compare. Method: Dynamic Text Blocks & Code Snippets. Interaction: Topic buttons filter content. Justification: Facilitates direct, focused comparison.\n        - Language Trade-offs: Goal: Compare. Method: Chart.js Radar Chart. Interaction: Hover for details. Justification: Provides a quick, high-level visual summary of complex trade-offs. -->\n    <!-- CONFIRMATION: NO SVG graphics used. NO Mermaid JS used. -->\n    <style>\n        body {\n            font-family: 'Noto Sans TC', sans-serif;\n            background-color: #F5F5F4; \/* stone-100 *\/\n            color: #292524; \/* stone-800 *\/\n        }\n        .nav-button {\n            transition: all 0.3s ease;\n        }\n        .nav-button.active {\n            background-color: #A8A29E; \/* stone-400 *\/\n            color: white;\n        }\n        .content-section {\n            display: none;\n        }\n        .content-section.active {\n            display: block;\n        }\n        .comparison-button.active {\n            background-color: #57534E; \/* stone-600 *\/\n            color: white;\n        }\n        .code-block {\n            background-color: #1C1917; \/* stone-900 *\/\n            color: #E7E5E4; \/* stone-200 *\/\n            padding: 1rem;\n            border-radius: 0.5rem;\n            overflow-x: auto;\n            font-family: monospace;\n        }\n        .chart-container {\n            position: relative;\n            width: 100%;\n            max-width: 600px;\n            margin-left: auto;\n            margin-right: auto;\n            height: 350px;\n            max-height: 450px;\n        }\n         @media (min-width: 768px) { .chart-container { height: 400px; } }\n    <\/style>\n<\/head>\n<body class=\"antialiased\">\n\n    <div class=\"container mx-auto p-4 md:p-8\">\n        \n        <header class=\"text-center mb-8\">\n            <h1 class=\"text-3xl md:text-5xl font-bold text-stone-900\">\u5178\u7bc4\u8f49\u79fb<\/h1>\n            <p class=\"text-lg md:text-xl text-stone-600 mt-2\">\u70ba\u4f55 Rust \u8207 Go \u9078\u64c7\u7d44\u5408\u800c\u975e\u7e7c\u627f\uff1f<\/p>\n        <\/header>\n\n        <nav class=\"flex justify-center mb-8 bg-stone-200 rounded-full p-1 shadow-inner\">\n            <button class=\"nav-button active w-1\/3 py-2 px-4 rounded-full text-sm md:text-base\" data-target=\"problem\">\u7e7c\u627f\u7684\u554f\u984c<\/button>\n            <button class=\"nav-button w-1\/3 py-2 px-4 rounded-full text-sm md:text-base\" data-target=\"solution\">\u7d44\u5408\u7684\u8208\u8d77<\/button>\n            <button class=\"nav-button w-1\/3 py-2 px-4 rounded-full text-sm md:text-base\" data-target=\"deep-dive\">\u8a9e\u8a00\u6df1\u5ea6\u6bd4\u8f03<\/button>\n        <\/nav>\n\n        <main>\n            <section id=\"problem\" class=\"content-section active\">\n                 <div class=\"bg-white p-6 rounded-lg shadow-md\">\n                    <h2 class=\"text-2xl font-bold mb-4 text-center\">\u7e7c\u627f\u7684\u907a\u7522\uff1a\u5c0d\u50b3\u7d71\u7269\u4ef6\u5c0e\u5411\u7684\u6279\u5224\u6027\u91cd\u4f30<\/h2>\n                    <p class=\"text-stone-700 mb-6 text-center max-w-3xl mx-auto\">\u7e7c\u627f\uff08Inheritance\uff09\u66fe\u662f\u7269\u4ef6\u5c0e\u5411\u7684\u57fa\u77f3\uff0c\u627f\u8afe\u7a0b\u5f0f\u78bc\u91cd\u7528\u8207\u591a\u578b\u3002\u7136\u800c\uff0c\u96a8\u8457\u7cfb\u7d71\u65e5\u8da8\u8907\u96dc\uff0c\u5176\u5167\u5728\u7f3a\u9677\u9010\u6f38\u66b4\u9732\uff0c\u4fc3\u4f7f\u73fe\u4ee3\u8a9e\u8a00\u5982 Rust \u548c Go \u5c0b\u6c42\u66f4\u597d\u7684\u66ff\u4ee3\u65b9\u6848\u3002\u672c\u7bc0\u5c07\u63a2\u8a0e\u7e7c\u627f\u5e36\u4f86\u7684\u5e7e\u500b\u6838\u5fc3\u554f\u984c\u3002<\/p>\n                    <div class=\"space-y-4 max-w-4xl mx-auto\">\n                        <details class=\"group bg-stone-50 p-4 rounded-lg\">\n                            <summary class=\"font-semibold cursor-pointer flex justify-between items-center\">\n                                <span>\u8106\u5f31\u57fa\u5e95\u985e\u5225\u554f\u984c (The Fragile Base Class Problem)<\/span>\n                                <span class=\"group-open:rotate-90 transition-transform duration-200\">&#10148;<\/span>\n                            <\/summary>\n                            <p class=\"mt-2 text-stone-600\">\u5c0d\u57fa\u5e95\u985e\u5225\u770b\u4f3c\u7121\u5bb3\u7684\u4fee\u6539\uff0c\u537b\u53ef\u80fd\u610f\u5916\u7834\u58de\u6240\u6709\u884d\u751f\u985e\u5225\u7684\u904b\u4f5c\u3002\u9019\u662f\u56e0\u70ba\u7e7c\u627f\u7834\u58de\u4e86\u300c\u5c01\u88dd\u300d\uff0c\u5b50\u985e\u5225\u5e38\u4e0d\u81ea\u89ba\u5730\u4f9d\u8cf4\u7236\u985e\u5225\u7684\u5167\u90e8\u5be6\u4f5c\u7d30\u7bc0\u3002\u7576\u7236\u985e\u5225\u5be6\u4f5c\u8b8a\u66f4\u6642\uff0c\u5373\u4f7f\u516c\u958b\u4ecb\u9762\u4e0d\u8b8a\uff0c\u4e5f\u53ef\u80fd\u5c0e\u81f4\u5b50\u985e\u5225\u884c\u70ba\u932f\u8aa4\u3002<\/p>\n                        <\/details>\n                        <details class=\"group bg-stone-50 p-4 rounded-lg\">\n                            <summary class=\"font-semibold cursor-pointer flex justify-between items-center\">\n                                <span>\u83f1\u5f62\u554f\u984c (The Diamond Problem)<\/span>\n                                <span class=\"group-open:rotate-90 transition-transform duration-200\">&#10148;<\/span>\n                            <\/summary>\n                            <div class=\"mt-4 flex flex-col md:flex-row gap-4 items-center\">\n                                <div class=\"text-stone-600 flex-1\">\n                                    <p>\u7576\u4e00\u500b\u985e\u5225 D \u540c\u6642\u7e7c\u627f\u81ea B \u548c C\uff0c\u800c B \u548c C \u53c8\u90fd\u7e7c\u627f\u81ea A \u6642\uff0c\u82e5 B \u548c C \u90fd\u8986\u5beb\u4e86 A \u7684\u67d0\u500b\u65b9\u6cd5\uff0cD \u5728\u547c\u53eb\u8a72\u65b9\u6cd5\u6642\u5c31\u6703\u7522\u751f\u6b67\u7fa9\u3002\u9019\u66b4\u9732\u4e86\u55ae\u4e00\u3001\u50f5\u5316\u7684\u300c\u662f\u4e00\u7a2e\u300d(Is-A) \u968e\u5c64\uff0c\u4e0d\u8db3\u4ee5\u63cf\u8ff0\u4e00\u500b\u7269\u4ef6\u300c\u64c1\u6709\u591a\u7a2e\u80fd\u529b\u300d\u7684\u5834\u666f\u3002<\/p>\n                                <\/div>\n                                <div class=\"flex-shrink-0 font-mono text-center text-sm p-4 bg-stone-100 rounded\">\n                                    <div class=\"font-bold\">A<\/div>\n                                    <div class=\"flex justify-center\">\n                                        <div class=\"px-4\">\/<\/div>\n                                        <div class=\"px-4\">\\<\/div>\n                                    <\/div>\n                                    <div class=\"flex justify-between\">\n                                        <div class=\"font-bold\">B<\/div>\n                                        <div class=\"font-bold\">C<\/div>\n                                    <\/div>\n                                    <div class=\"flex justify-center\">\n                                        <div class=\"px-4\">\\<\/div>\n                                        <div class=\"px-4\">\/<\/div>\n                                    <\/div>\n                                    <div class=\"font-bold\">D<\/div>\n                                <\/div>\n                            <\/div>\n                        <\/details>\n                         <details class=\"group bg-stone-50 p-4 rounded-lg\">\n                            <summary class=\"font-semibold cursor-pointer flex justify-between items-center\">\n                                <span>\u7dca\u5bc6\u8026\u5408\u8207\u968e\u5c64\u50f5\u5316<\/span>\n                                <span class=\"group-open:rotate-90 transition-transform duration-200\">&#10148;<\/span>\n                            <\/summary>\n                            <p class=\"mt-2 text-stone-600\">\u7e7c\u627f\u5728\u7236\u5b50\u985e\u5225\u9593\u5efa\u7acb\u4e86\u7a0b\u5f0f\u78bc\u4e2d\u6700\u7dca\u5bc6\u7684\u8026\u5408\u95dc\u4fc2\uff0c\u4f7f\u5f97\u7cfb\u7d71\u975e\u5e38\u50f5\u786c\uff0c\u96e3\u4ee5\u9069\u61c9\u9700\u6c42\u8b8a\u66f4\u3002\u4e00\u65e6\u7e7c\u627f\u9ad4\u7cfb\u5efa\u7acb\uff0c\u91cd\u69cb\u7684\u6210\u672c\u6703\u8b8a\u5f97\u6975\u5176\u9ad8\u6602\uff0c\u9650\u5236\u4e86\u7a0b\u5f0f\u7684\u9748\u6d3b\u6027\u3002<\/p>\n                        <\/details>\n                    <\/div>\n                <\/div>\n            <\/section>\n\n            <section id=\"solution\" class=\"content-section\">\n                <div class=\"bg-white p-6 rounded-lg shadow-md\">\n                    <h2 class=\"text-2xl font-bold mb-4 text-center\">\u7d44\u5408\u7684\u8208\u8d77\uff1a\u8a2d\u8a08\u539f\u5247\u7684\u6f14\u9032<\/h2>\n                    <p class=\"text-stone-700 mb-6 text-center max-w-3xl mx-auto\">\u9762\u5c0d\u7e7c\u627f\u7684\u6311\u6230\uff0c\u8edf\u9ad4\u8a2d\u8a08\u9818\u57df\u8f49\u5411\u4e86\u66f4\u9748\u6d3b\u7684\u300c\u7d44\u5408\u300d(Composition)\u3002\u5176\u6838\u5fc3\u539f\u5247\u300c\u591a\u7528\u7d44\u5408\uff0c\u5c11\u7528\u7e7c\u627f\u300d\u5efa\u8b70\u958b\u767c\u8005\u512a\u5148\u900f\u904e\u7d44\u5408\u4f86\u64f4\u5c55\u529f\u80fd\uff0c\u5c07\u7e7c\u627f\u4fdd\u7559\u7d66\u771f\u6b63\u9700\u8981\u591a\u578b\u7684\u5834\u666f\u3002\u7d44\u5408\u6a21\u578b\u5316\u7684\u662f\u4e00\u7a2e\u300c\u6709\u4e00\u500b\u300d(Has-A) \u7684\u95dc\u4fc2\u3002<\/p>\n                    <div class=\"grid md:grid-cols-3 gap-6 text-center max-w-5xl mx-auto\">\n                        <div class=\"bg-stone-50 p-4 rounded-lg\">\n                            <h3 class=\"font-bold text-lg mb-2\">\u2714\ufe0f \u5f48\u6027 (Flexibility)<\/h3>\n                            <p class=\"text-stone-600 text-sm\">\u7269\u4ef6\u9593\u9b06\u6563\u8026\u5408\uff0c\u5141\u8a31\u5728\u57f7\u884c\u671f\u9593\u52d5\u614b\u6539\u8b8a\u884c\u70ba\uff0c\u907f\u514d\u300c\u5b50\u985e\u5225\u7206\u70b8\u300d\u3002<\/p>\n                        <\/div>\n                        <div class=\"bg-stone-50 p-4 rounded-lg\">\n                            <h3 class=\"font-bold text-lg mb-2\">\u2714\ufe0f \u5c01\u88dd (Encapsulation)<\/h3>\n                            <p class=\"text-stone-600 text-sm\">\u5c0a\u91cd\u4e26\u4fdd\u8b77\u4e86\u5c01\u88dd\uff0c\u900f\u904e\u300c\u9ed1\u7bb1\u300d\u91cd\u7528\uff0c\u5bb9\u5668\u7269\u4ef6\u53ea\u4f9d\u8cf4\u5143\u4ef6\u7684\u516c\u958b\u4ecb\u9762\u3002<\/p>\n                        <\/div>\n                        <div class=\"bg-stone-50 p-4 rounded-lg\">\n                            <h3 class=\"font-bold text-lg mb-2\">\u2714\ufe0f \u53ef\u6e2c\u8a66\u6027 (Testability)<\/h3>\n                            <p class=\"text-stone-600 text-sm\">\u4f9d\u8cf4\u7684\u5143\u4ef6\u660e\u78ba\u53ef\u66ff\u63db\uff0c\u6613\u65bc\u9694\u96e2\u548c\u6a21\u64ec\uff0c\u986f\u8457\u7c21\u5316\u4e86\u55ae\u5143\u6e2c\u8a66\u3002<\/p>\n                        <\/div>\n                    <\/div>\n                <\/div>\n            <\/section>\n\n            <section id=\"deep-dive\" class=\"content-section\">\n                 <div class=\"bg-white p-6 rounded-lg shadow-md\">\n                    <h2 class=\"text-2xl font-bold mb-4 text-center\">Go vs. Rust\uff1a\u6df1\u5ea6\u6bd4\u8f03<\/h2>\n                    <p class=\"text-stone-700 mb-6 text-center max-w-3xl mx-auto\">Go \u548c Rust \u90fd\u6452\u68c4\u4e86\u7e7c\u627f\uff0c\u4f46\u5b83\u5011\u57fa\u65bc\u7d44\u5408\u7684\u66ff\u4ee3\u65b9\u6848\u5728\u54f2\u5b78\u548c\u6a5f\u5236\u4e0a\u5404\u6709\u5343\u79cb\u3002Go \u8ffd\u6c42\u7c21\u6f54\u8207\u958b\u767c\u6548\u7387\uff0c\u800c Rust \u5c07\u6b63\u78ba\u6027\u8207\u6548\u80fd\u63a7\u5236\u7f6e\u65bc\u9996\u4f4d\u3002\u9ede\u64ca\u4e0b\u65b9\u6309\u9215\uff0c\u63a2\u7d22\u5b83\u5011\u7684\u5177\u9ad4\u5be6\u73fe\u5dee\u7570\u3002<\/p>\n                    \n                    <div class=\"flex flex-wrap justify-center gap-2 mb-8\">\n                        <button class=\"comparison-button active py-2 px-4 bg-stone-200 rounded-full text-sm\" data-comparison=\"philosophy\">\u8a2d\u8a08\u54f2\u5b78<\/button>\n                        <button class=\"comparison-button py-2 px-4 bg-stone-200 rounded-full text-sm\" data-comparison=\"reuse\">\u7a0b\u5f0f\u78bc\u91cd\u7528<\/button>\n                        <button class=\"comparison-button py-2 px-4 bg-stone-200 rounded-full text-sm\" data-comparison=\"polymorphism\">\u591a\u578b\u5be6\u73fe<\/button>\n                        <button class=\"comparison-button py-2 px-4 bg-stone-200 rounded-full text-sm\" data-comparison=\"dispatch\">\u5206\u6d3e\u6a5f\u5236<\/button>\n                        <button class=\"comparison-button py-2 px-4 bg-stone-200 rounded-full text-sm\" data-comparison=\"summary\">\u512a\u52a3\u7e3d\u7d50<\/button>\n                    <\/div>\n\n                    <div class=\"grid md:grid-cols-2 gap-6\">\n                        <div class=\"border-2 border-cyan-300 p-4 rounded-lg bg-cyan-50\/50\">\n                            <h3 class=\"text-2xl font-bold text-cyan-800 mb-4\">Go<\/h3>\n                            <div id=\"go-content\"><\/div>\n                        <\/div>\n                        <div class=\"border-2 border-orange-300 p-4 rounded-lg bg-orange-50\/50\">\n                            <h3 class=\"text-2xl font-bold text-orange-800 mb-4\">Rust<\/h3>\n                            <div id=\"rust-content\"><\/div>\n                        <\/div>\n                    <\/div>\n\n                    <h3 class=\"text-xl font-bold mt-12 mb-4 text-center\">\u8a9e\u8a00\u7279\u6027\u6b0a\u8861\u96f7\u9054\u5716<\/h3>\n                    <div class=\"chart-container\">\n                        <canvas id=\"comparisonChart\"><\/canvas>\n                    <\/div>\n                 <\/div>\n            <\/section>\n        <\/main>\n    <\/div>\n\n<script>\n    document.addEventListener('DOMContentLoaded', () => {\n        const contentData = {\n            go: {\n                philosophy: `\n                    <h4 class=\"font-bold text-lg mb-2\">\u7c21\u6f54\u3001\u9ad8\u6548\u8207\u52d9\u5be6<\/h4>\n                    <p class=\"text-stone-700\">Go \u7684\u8a2d\u8a08\u65e8\u5728\u89e3\u6c7a\u5927\u578b\u8edf\u9ad4\u57fa\u790e\u8a2d\u65bd\u958b\u767c\u4e2d\u7684\u7de9\u6162\u548c\u7b28\u62d9\u554f\u984c\u3002\u5b83\u8ffd\u6c42\u8a9e\u8a00\u7684\u6975\u5ea6\u7c21\u6f54\uff08\u50c525\u500b\u95dc\u9375\u5b57\uff09\uff0c\u76ee\u6a19\u662f\u8b93\u958b\u767c\u8005\u80fd\u5feb\u901f\u3001\u9ad8\u6548\u5730\u69cb\u5efa\u7a69\u5065\u7684\u7cfb\u7d71\uff0c\u800c\u4e0d\u662f\u5728\u8a9e\u8a00\u7684\u8907\u96dc\u6027\u4e2d\u6399\u624e\u3002<\/p>\n                `,\n                reuse: `\n                    <h4 class=\"font-bold text-lg mb-2\">\u7d50\u69cb\u9ad4\u5d4c\u5165 (Struct Embedding)<\/h4>\n                    <p class=\"text-stone-700 mb-2\">\u900f\u904e\u5728\u4e00\u500b\u7d50\u69cb\u9ad4\u4e2d\u5d4c\u5165\u53e6\u4e00\u500b\u7d50\u69cb\u9ad4\uff0c\u88ab\u5d4c\u5165\u7d50\u69cb\u9ad4\u7684\u6b04\u4f4d\u548c\u65b9\u6cd5\u6703\u88ab\u300c\u63d0\u5347\u300d\uff0c\u53ef\u4ee5\u76f4\u63a5\u5b58\u53d6\u3002\u9019\u662f\u4e00\u7a2e\u8a9e\u6cd5\u7cd6\uff0c\u6975\u5927\u7c21\u5316\u4e86\u7d44\u5408\u7684\u5beb\u6cd5\uff0c\u4f46\u5176\u672c\u8cea\u4ecd\u662f\u7d44\u5408\uff0c\u800c\u975e\u7e7c\u627f\u3002<\/p>\n                    <div class=\"code-block text-sm\">\n<pre>type Worker struct {\n    Name string\n}\nfunc (w Worker) Work() { ... }\n\ntype Manager struct {\n    Worker \/\/ \u5d4c\u5165 Worker\n    Level  int\n}\n\nm := Manager{...}\nm.Work() \/\/ \u76f4\u63a5\u547c\u53eb\u88ab\u5d4c\u5165\u7684\u65b9\u6cd5\n<\/pre>\n                    <\/div>\n                `,\n                polymorphism: `\n                    <h4 class=\"font-bold text-lg mb-2\">\u96b1\u5f0f\u4ecb\u9762 (Implicit Interfaces)<\/h4>\n                    <p class=\"text-stone-700 mb-2\">\u4e00\u500b\u985e\u578b\u53ea\u8981\u5be6\u4f5c\u4e86\u67d0\u4ecb\u9762\u8981\u6c42\u7684\u6240\u6709\u65b9\u6cd5\uff0c\u5c31\u81ea\u52d5\u6eff\u8db3\u8a72\u4ecb\u9762\uff0c\u7121\u9700\u986f\u5f0f\u8072\u660e (implements)\u3002\u9019\u7a2e\u300c\u7d50\u69cb\u5316\u985e\u578b\u300d\u63d0\u4f9b\u4e86\u6975\u5927\u7684\u9748\u6d3b\u6027\u548c\u89e3\u8026\uff0c\u751a\u81f3\u53ef\u4ee5\u5c0d\u7b2c\u4e09\u65b9\u5eab\u7684\u985e\u578b\u5be6\u73fe\u65b0\u4ecb\u9762\u3002<\/p>\n                    <div class=\"code-block text-sm\">\n<pre>type Shape interface {\n    Area() float64\n}\n\ntype Circle struct{...}\n\/\/ \u53ea\u8981 Circle \u5be6\u4f5c\u4e86 Area()\uff0c\u5b83\u5c31\u6eff\u8db3 Shape \u4ecb\u9762\nfunc (c Circle) Area() float64 { ... } \n<\/pre>\n                    <\/div>\n                `,\n                dispatch: `\n                    <h4 class=\"font-bold text-lg mb-2\">\u50c5\u52d5\u614b\u5206\u6d3e (Dynamic Dispatch)<\/h4>\n                    <p class=\"text-stone-700\">\u6240\u6709\u7684\u4ecb\u9762\u547c\u53eb\u90fd\u5728\u57f7\u884c\u671f\u900f\u904e\u65b9\u6cd5\u67e5\u627e\u4f86\u5be6\u73fe\uff08\u985e\u4f3c\u865b\u64ec\u51fd\u5f0f\u8868\uff09\u3002\u9019\u7a2e\u8a2d\u8a08\u4fdd\u6301\u4e86\u8a9e\u8a00\u7684\u7c21\u55ae\u548c\u7d71\u4e00\uff0c\u4f46\u5728\u5c0d\u6548\u80fd\u6975\u7aef\u654f\u611f\u7684\u5834\u666f\u4e0b\uff0c\u6703\u5b58\u5728\u56fa\u5b9a\u7684\u57f7\u884c\u671f\u958b\u92b7\u3002<\/p>\n                `,\n                summary: `\n                    <h4 class=\"font-bold text-lg mb-2\">\u512a\u52e2\u8207\u6b0a\u8861<\/h4>\n                    <ul class=\"list-disc list-inside text-stone-700 space-y-1\">\n                        <li><span class=\"font-semibold text-green-600\">\u512a\u52e2\uff1a<\/span>\u7c21\u6f54\u3001\u4f4e\u8a8d\u77e5\u8ca0\u64d4\u3001\u9ad8\u5ea6\u9748\u6d3b\u89e3\u8026\u3002<\/li>\n                        <li><span class=\"font-semibold text-red-600\">\u6b0a\u8861\uff1a<\/span>\u985e\u578b\u7cfb\u7d71\u8868\u9054\u529b\u6709\u9650\uff0c\u56fa\u5b9a\u7684\u57f7\u884c\u671f\u5206\u6d3e\u958b\u92b7\u3002<\/li>\n                    <\/ul>\n                `\n            },\n            rust: {\n                philosophy: `\n                    <h4 class=\"font-bold text-lg mb-2\">\u6b63\u78ba\u6027\u3001\u63a7\u5236\u529b\u8207\u96f6\u6210\u672c\u62bd\u8c61<\/h4>\n                    <p class=\"text-stone-700\">Rust \u7684\u8a2d\u8a08\u5c07\u8a18\u61b6\u9ad4\u5b89\u5168\u548c\u6548\u80fd\u7f6e\u65bc\u6700\u9ad8\u512a\u5148\u7d1a\u3002\u5b83\u900f\u904e\u6240\u6709\u6b0a\u7cfb\u7d71\u5728\u7de8\u8b6f\u671f\u4fdd\u8b49\u5b89\u5168\uff0c\u4e26\u9075\u5faa\u300c\u96f6\u6210\u672c\u62bd\u8c61\u300d\u539f\u5247\uff0c\u78ba\u4fdd\u9ad8\u968e\u62bd\u8c61\u4e0d\u6703\u5e36\u4f86\u975e\u5fc5\u8981\u7684\u6548\u80fd\u640d\u5931\u3002<\/p>\n                `,\n                reuse: `\n                    <h4 class=\"font-bold text-lg mb-2\">\u7d50\u69cb\u9ad4\u7d44\u5408\u8207 Trait<\/h4>\n                    <p class=\"text-stone-700 mb-2\">Rust \u9f13\u52f5\u4f7f\u7528\u660e\u78ba\u7684\u7d50\u69cb\u9ad4\u7d44\u5408\uff0c\u4e26\u900f\u904e Trait\uff08\u985e\u4f3c\u4ecb\u9762\uff09\u7684\u9810\u8a2d\u5be6\u4f5c\u4f86\u91cd\u7528\u884c\u70ba\u3002\u6c92\u6709\u81ea\u52d5\u7684\u65b9\u6cd5\u63d0\u5347\uff0c\u4f7f\u5f97\u7a0b\u5f0f\u78bc\u95dc\u4fc2\u66f4\u70ba\u660e\u78ba\uff0c\u4f46\u53ef\u80fd\u9700\u8981\u7de8\u5beb\u66f4\u591a\u59d4\u6d3e\u7a0b\u5f0f\u78bc\uff08\u53ef\u900f\u904e\u5de8\u96c6\u7c21\u5316\uff09\u3002<\/p>\n                    <div class=\"code-block text-sm\">\n<pre>trait CanWork {\n    fn work(&self) { ... } \/\/ \u9810\u8a2d\u5be6\u4f5c\n}\n\nstruct Worker { name: String }\nimpl CanWork for Worker {} \/\/ Worker \u7372\u5f97 work \u80fd\u529b\n\nstruct Manager {\n    worker: Worker, \/\/ \u660e\u78ba\u7d44\u5408\n    level: u32,\n}\n<\/pre>\n                    <\/div>\n                `,\n                polymorphism: `\n                    <h4 class=\"font-bold text-lg mb-2\">\u986f\u5f0f Trait (Explicit Traits)<\/h4>\n                    <p class=\"text-stone-700 mb-2\">\u4e00\u500b\u985e\u578b\u5fc5\u9808\u4f7f\u7528 `impl Trait for Type` \u4f86\u986f\u5f0f\u8072\u660e\u5176\u5be6\u73fe\u4e86\u67d0\u500b Trait\u3002Trait \u7cfb\u7d71\u8868\u9054\u529b\u6975\u5f37\uff0c\u652f\u6301\u95dc\u806f\u985e\u578b\u548c\u6cdb\u578b\uff0c\u80fd\u5728\u7de8\u8b6f\u671f\u9a57\u8b49\u975e\u5e38\u8907\u96dc\u7684\u985e\u578b\u95dc\u4fc2\uff0c\u4fdd\u8b49\u985e\u578b\u5b89\u5168\u3002<\/p>\n                    <div class=\"code-block text-sm\">\n<pre>trait Shape {\n    fn area(&self) -> f64;\n}\n\nstruct Circle{...}\n\/\/ \u5fc5\u9808\u986f\u5f0f\u8072\u660e\u5be6\u73fe\u4e86 Shape Trait\nimpl Shape for Circle {\n    fn area(&self) -> f64 { ... }\n}\n<\/pre>\n                    <\/div>\n                `,\n                dispatch: `\n                    <h4 class=\"font-bold text-lg mb-2\">\u975c\u614b\u5206\u6d3e (\u9810\u8a2d) vs. \u52d5\u614b\u5206\u6d3e<\/h4>\n                    <p class=\"text-stone-700\">\u9810\u8a2d\u60c5\u6cc1\u4e0b\uff0cRust \u900f\u904e\u6cdb\u578b\u548c\u55ae\u614b\u5316\u5be6\u73fe\u975c\u614b\u5206\u6d3e\uff0c\u6c92\u6709\u57f7\u884c\u671f\u958b\u92b7\u3002\u5728\u9700\u8981\u7570\u8cea\u96c6\u5408\u6642\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u660e\u78ba\u4f7f\u7528 `dyn Trait` \u4f86\u9078\u64c7\u52d5\u614b\u5206\u6d3e\u3002\u9019\u7a2e\u9078\u64c7\u6b0a\u662f\u5176\u300c\u96f6\u6210\u672c\u62bd\u8c61\u300d\u7684\u9ad4\u73fe\u3002<\/p>\n                `,\n                summary: `\n                    <h4 class=\"font-bold text-lg mb-2\">\u512a\u52e2\u8207\u6b0a\u8861<\/h4>\n                    <ul class=\"list-disc list-inside text-stone-700 space-y-1\">\n                        <li><span class=\"font-semibold text-green-600\">\u512a\u52e2\uff1a<\/span>\u6975\u81f4\u7684\u6548\u80fd\u63a7\u5236\u3001\u7121\u8207\u502b\u6bd4\u7684\u5b89\u5168\u6027\u8207\u8868\u9054\u529b\u3002<\/li>\n                        <li><span class=\"font-semibold text-red-600\">\u6b0a\u8861\uff1a<\/span>\u9661\u5ced\u7684\u5b78\u7fd2\u66f2\u7dda\uff0c\u8a9e\u6cd5\u8f03\u5197\u9577\uff0c\u5fc3\u667a\u8ca0\u64d4\u8f03\u91cd\u3002<\/li>\n                    <\/ul>\n                `\n            }\n        };\n\n        const navButtons = document.querySelectorAll('.nav-button');\n        const contentSections = document.querySelectorAll('.content-section');\n        const comparisonButtons = document.querySelectorAll('.comparison-button');\n        const goContent = document.getElementById('go-content');\n        const rustContent = document.getElementById('rust-content');\n\n        function showSection(targetId) {\n            navButtons.forEach(btn => {\n                btn.classList.toggle('active', btn.dataset.target === targetId);\n            });\n            contentSections.forEach(section => {\n                section.classList.toggle('active', section.id === targetId);\n            });\n        }\n\n        function showComparison(comparisonId) {\n            comparisonButtons.forEach(btn => {\n                btn.classList.toggle('active', btn.dataset.comparison === comparisonId);\n            });\n            goContent.innerHTML = contentData.go[comparisonId];\n            rustContent.innerHTML = contentData.rust[comparisonId];\n        }\n\n        navButtons.forEach(button => {\n            button.addEventListener('click', () => showSection(button.dataset.target));\n        });\n        \n        comparisonButtons.forEach(button => {\n            button.addEventListener('click', () => showComparison(button.dataset.comparison));\n        });\n\n        function createChart() {\n            const ctx = document.getElementById('comparisonChart').getContext('2d');\n            new Chart(ctx, {\n                type: 'radar',\n                data: {\n                    labels: ['\u6548\u80fd\u8207\u63a7\u5236\u529b', '\u958b\u767c\u6548\u7387\u8207\u7c21\u6f54\u6027', '\u8a18\u61b6\u9ad4\u5b89\u5168', '\u9748\u6d3b\u6027\u8207\u89e3\u8026', '\u5b78\u7fd2\u66f2\u7dda'],\n                    datasets: [{\n                        label: 'Go',\n                        data: [3, 5, 4, 5, 5],\n                        backgroundColor: 'rgba(0, 173, 216, 0.2)',\n                        borderColor: 'rgba(0, 173, 216, 1)',\n                        pointBackgroundColor: 'rgba(0, 173, 216, 1)',\n                        pointBorderColor: '#fff',\n                        pointHoverBackgroundColor: '#fff',\n                        pointHoverBorderColor: 'rgba(0, 173, 216, 1)'\n                    }, {\n                        label: 'Rust',\n                        data: [5, 2, 5, 3, 2],\n                        backgroundColor: 'rgba(222, 165, 132, 0.2)',\n                        borderColor: 'rgba(222, 165, 132, 1)',\n                        pointBackgroundColor: 'rgba(222, 165, 132, 1)',\n                        pointBorderColor: '#fff',\n                        pointHoverBackgroundColor: '#fff',\n                        pointHoverBorderColor: 'rgba(222, 165, 132, 1)'\n                    }]\n                },\n                options: {\n                    maintainAspectRatio: false,\n                    scales: {\n                        r: {\n                            angleLines: { color: '#D6D3D1' },\n                            grid: { color: '#D6D3D1' },\n                            pointLabels: {\n                                font: { size: 12 },\n                                color: '#57534E'\n                            },\n                            ticks: {\n                                backdropColor: '#F5F5F4',\n                                color: '#78716C',\n                                stepSize: 1,\n                                max: 5,\n                                min: 0\n                            }\n                        }\n                    },\n                    plugins: {\n                        legend: {\n                            labels: {\n                                color: '#292524'\n                            }\n                        }\n                    }\n                }\n            });\n        }\n\n        showSection('problem');\n        showComparison('philosophy');\n        createChart();\n    });\n<\/script>\n\n<\/body>\n<\/html>\n\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e92\u52d5\u5f0f\u5831\u544a\uff1a\u7d44\u5408 vs. \u7e7c\u627f &#8211; Rust \u8207 Go \u7684\u9078\u64c7 \u5178\u7bc4 &hellip; <a href=\"https:\/\/ouyangminwei.com\/index.php\/2025\/07\/18\/composition-vs-inheritance\/\">\u95b1\u8b80\u5168\u6587 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"post_format":[],"class_list":["post-839","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_edit_lock":"1752829825:1","_edit_last":"1","_aioseo_title":"#post_title #separator_sa #site_title","_aioseo_description":"#post_excerpt","_aioseo_keywords":"","_aioseo_og_title":"","_aioseo_og_description":"","_aioseo_og_article_section":"","_aioseo_og_article_tags":"","_aioseo_twitter_title":"","_aioseo_twitter_description":"","_oembed_2544c1d0cb3503ab4c4d558c3b3c8873":"","_oembed_time_2544c1d0cb3503ab4c4d558c3b3c8873":"","_oembed_99481806ecbe6ce4ee46f8588d320993":"","_oembed_db663acf973e82e6d9d80df71945dfb8":"","_oembed_16cdfab488f57db73586f4286af2704f":"","_wp_old_slug":"","_links":{"self":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts\/839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/comments?post=839"}],"version-history":[{"count":2,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts\/839\/revisions"}],"predecessor-version":[{"id":872,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts\/839\/revisions\/872"}],"wp:attachment":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/media?parent=839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/categories?post=839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/tags?post=839"},{"taxonomy":"post_format","embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/post_format?post=839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}