263 lines
494 KiB
HTML
263 lines
494 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html lang="zh"><head><title>面对对象</title><meta charset="utf-8"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&family=Noto Serif Simplified Chinese:wght@400;700&family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&display=swap"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="面对对象"/><meta property="og:description" content="Python 面对对象."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../static/icon.png"/><meta name="description" content="Python 面对对象."/><meta name="generator" content="Quartz"/><link href="../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch("../../../../static/contentIndex.json").then(data => data.json())</script></head><body data-slug="Technology/ProgrammingLanguage/Python/入门/面对对象"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h2 class="page-title"><a href="../../../..">🪴 X·Eden</a></h2><div class="spacer mobile-only"></div><div class="search"><button class="search-button" id="search-button"><p>搜索</p><svg role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title>Search</title><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></button><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="搜索些什么" placeholder="搜索些什么"/><div id="search-layout" data-preview="true"></div></div></div></div><button class="darkmode" id="darkmode"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35" xml:space="preserve" aria-label="暗色模式"><title>暗色模式</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="nightIcon" x="0
|
|||
|
<h2 id="基本概念">基本概念<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#基本概念" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<h3 id="理念和用途">理念和用途<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#理念和用途" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>**面向对象编程是在更高的抽象层次上思考问题的一种方式。**面向对象编程的主要理念是,将现实世界中的对象抽象化,将对象的属性(也叫状态)和行为(也叫方法)封装在一起。面向对象编程的思考方式,更贴近我们对现实世界的认知。</p>
|
|||
|
<p>面向对象编程的主要优点包括:</p>
|
|||
|
<ol>
|
|||
|
<li><strong>代码复用性</strong>:类的定义一旦完成,就可以在任何地方创建其对象,复用类的属性和方法,而无需重复编写代码。通过类之间的继承关系,子类可以继承父类的代码,从而实现代码的复用。</li>
|
|||
|
<li><strong>代码的可维护性和可扩展性</strong>:面向对象编程的封装特性,保护了对象内部的数据,使得代码更安全,更易于维护。通过类的继承和多态,可以轻松实现对现有代码的扩展。</li>
|
|||
|
</ol>
|
|||
|
<h3 id="特点">特点<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#特点" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p><strong>面向对象编程有三个主要的特点:封装、继承、和多态。</strong></p>
|
|||
|
<ul>
|
|||
|
<li><strong>封装</strong>:封装是指将对象的状态(数据)和行为(方法)包装在一起,隐藏对象的内部实现细节,仅对外提供必要的接口。这样可以保护对象的内部状态,提高代码的安全性,也提高了代码的可维护性。</li>
|
|||
|
<li><strong>继承</strong>:继承是指子类可以继承父类的属性和方法。继承让我们可以在父类的基础上进行扩展,实现代码的复用,提高代码的可扩展性。</li>
|
|||
|
<li><strong>多态</strong>:多态是指不同类的对象对同一消息会做出不同的响应。多态提供了统一的接口,使得我们可以使用一致的方式处理不同类的对象,提高了代码的灵活性。</li>
|
|||
|
</ul>
|
|||
|
<h2 id="python-中的类和对象">Python 中的类和对象<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#python-中的类和对象" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>在 Python 中,我们使用 <code>class</code> 关键字来定义类。类是一种数据类型,它定义了一种新的对象,这种对象具有自己的属性和方法。</p>
|
|||
|
<h3 id="类和对象的概念">类和对象的概念<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#类和对象的概念" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<ul>
|
|||
|
<li><strong>类 (Class)</strong>: 类是对象的蓝图或原型。你可以想象成是创建对象的模板,它定义了特定类型的对象的属性和方法。</li>
|
|||
|
</ul>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 一个简单的 Python 类的例子</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Dog</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> pass</span></span></code></pre></figure>
|
|||
|
<ul>
|
|||
|
<li><strong>对象 (Object)</strong>:对象是类的实例。具体来说,当我们根据类的定义创建了一个实例后,这个实例就是一个对象。每个对象都具有类定义的属性和方法。</li>
|
|||
|
</ul>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建Dog类的一个实例</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">my_dog </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Dog()</span></span></code></pre></figure>
|
|||
|
<p>我们可以通过创建类的实例,也就是对象,来使用类的属性和方法。</p>
|
|||
|
<h3 id="类的属性和方法">类的属性和方法<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#类的属性和方法" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>类的属性和方法是类的主要组成部分。属性用于描述类和其实例的状态,方法用于描述类和其实例可以进行的操作。</p>
|
|||
|
<p>在 Python 中,我们可以在类中定义两种类型的属性和方法:</p>
|
|||
|
<ul>
|
|||
|
<li><strong>实例属性和方法</strong>:实例属性和方法属于类的实例。每个实例都有自己的实例属性,这些属性与其他实例的属性互不影响。实例方法可以通过实例来调用,它可以访问和修改实例的属性。</li>
|
|||
|
</ul>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 在Python类中定义实例属性和方法的例子</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Dog</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> __init__</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self, name):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.name </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> name </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 实例属性</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> bark</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self): </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 实例方法</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> f</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">{self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.name</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">}</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> says woof!"</span></span>
|
|||
|
<span data-line> </span></code></pre></figure>
|
|||
|
<ul>
|
|||
|
<li><strong>类属性和方法</strong>:类属性和方法是属于类本身的,所有的实例都会共享同一个类属性。类方法可以通过类名直接调用,也可以通过实例调用,但是它不能访问和修改实例属性。</li>
|
|||
|
</ul>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 在Python类中定义类属性和方法的例子</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Dog</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> species </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "Canis familiaris"</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 类属性</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> @</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">classmethod</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> description</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(cls): </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 类方法</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "This class represents a dog."</span></span>
|
|||
|
<span data-line> </span></code></pre></figure>
|
|||
|
<h3 id="__init__"><code>__init__</code><a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#__init__" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>在 Python 中使用 <code>__init__</code> 方法来初始化类的实例。这个方法会在创建实例时自动调用,我们可以在这个方法中设置实例的初始状态,也就是定义实例的属性。</p>
|
|||
|
<p><code>__init__</code> 方法的第一个参数始终是 <code>self</code>,代表了实例本身。在 <code>__init__</code> 方法中,我们使用 <code>self.属性名</code> 来定义实例属性。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Dog</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> __init__</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self, name, age):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.name </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> name</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.age </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> age</span></span></code></pre></figure>
|
|||
|
<h2 id="封装">封装<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#封装" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<h3 id="概念和用途">概念和用途<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#概念和用途" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>封装,这是面向对象编程(OOP)中的基本概念之一,其目标是通过将数据(对象的状态)和对象的行为组合在一起,来<strong>隐藏或封装对象内部的详细信息</strong>。简单地说,封装就是把客户端代码(对象的使用者)从对象的内部细节中解耦出来。</p>
|
|||
|
<p>封装的主要优势包括:</p>
|
|||
|
<ol>
|
|||
|
<li><strong>提高了代码的安全性</strong>:封装可以防止对象状态的无意义或不合适的修改。只有通过定义好的方法(有时被称为 getter 和 setter )才能修改状态,这些方法可以控制对对象状态的更改,从而保证其正确和一致。</li>
|
|||
|
<li><strong>提高了代码的可维护性</strong>:由于客户端代码从对象的内部实现中解耦出来,我们可以自由地改变对象的内部实现,而不会影响到使用该对象的代码。</li>
|
|||
|
</ol>
|
|||
|
<h3 id="python-中实现封装">Python 中实现封装<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#python-中实现封装" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<h4 id="数据封装">数据封装<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#数据封装" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>在 Python 中,我们可以通过使用私有属性(私有变量)实现数据封装。**Python 中的私有属性名字前面带有两个下划线(例如 <code>__name</code>)。**这些属性只能在类的内部被访问,不能在类的外部直接访问,从而实现了数据的封装。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Dog</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> __init__</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self, name, age):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # __name和__age是私有属性,我们不能在类的外部直接访问</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.__name </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> name</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.__age </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> age</span></span></code></pre></figure>
|
|||
|
<h4 id="方法封装">方法封装<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#方法封装" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>Python 同样可以通过私有方法实现方法的封装。私有方法的名字也以两个下划线开始,它们只能在类的内部被调用,不能在类的外部直接调用。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Dog</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> __init__</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self, name, age):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.__name </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> name</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.__age </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> age</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # __bark 是一个私有方法,我们不能在类的外部直接调用</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> __bark</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> f</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">{self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.__name</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">}</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> says woof!"</span></span></code></pre></figure>
|
|||
|
<p>尽管私有属性和方法不能在类的外部直接访问,但是我们可以提供公共的 getter 和 setter 方法,让外部代码能够以受控的方式读写这些私有属性。这样我们就可以保证封装的安全性,同时也提供了一定的灵活性。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Dog</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> __init__</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self, name, age):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.__name </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> name</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.__age </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> age</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # getter for __name</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> get_name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.__name</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # setter for __name</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> set_name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self, name):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.__name </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> name</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # getter for __age</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> get_age</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.__age</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # setter for __age</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> set_age</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self, age):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.__age </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> age</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # __bark 是一个私有方法,我们不能在类的外部直接调用</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> __bark</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> f</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">{self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.__name</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">}</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> says woof!"</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 公共方法可以调用私有方法</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> public_bark</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.__bark()</span></span></code></pre></figure>
|
|||
|
<h2 id="继承">继承<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#继承" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<h3 id="继承的概念和用途">继承的概念和用途<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#继承的概念和用途" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>继承是面向对象编程中的核心概念之一。继承允许我们定义一个新的类(子类或派生类)来继承现有类(父类或基类)的属性(变量)和方法(函数)。这样我们就可以在新类中复用父类的代码,避免了代码的重复,同时也可以在新类中添加新的方法或对父类的方法进行重写(override)。</p>
|
|||
|
<p>继承的主要优点包括:</p>
|
|||
|
<ol>
|
|||
|
<li><strong>代码复用</strong>:子类继承了父类的所有属性和方法,所以我们可以通过创建子类,来复用和扩展父类的代码,减少了代码的重复。</li>
|
|||
|
<li><strong>易于维护和修改</strong>:由于子类可以重写父类的方法,这就意味着我们可以在不改变父类的情况下,改变子类的行为。这使得代码更容易维护和修改。</li>
|
|||
|
</ol>
|
|||
|
<h3 id="python-中实现继承">Python 中实现继承<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#python-中实现继承" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<h4 id="单继承">单继承<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#单继承" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>在 Python 中,我们可以通过在类定义时的括号中写入父类的名字来创建子类。新定义的类将会继承父类的所有属性和方法。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Animal</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> __init__</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self, name):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.name </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> name</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Dog 是 Animal 的子类,它继承了 Animal 的所有属性和方法。</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Dog</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">Animal</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> pass</span></span></code></pre></figure>
|
|||
|
<h4 id="多继承">多继承<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#多继承" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>Python 支持多继承,意味着一个类可以继承多个父类。但这可能会导致一些复杂性,如“菱形问题”。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Swimming</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> swim</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "Swims!"</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Flying</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> fly</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "Flies!"</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Duck</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">Swimming</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">Flying</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> pass</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">duck </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Duck()</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(duck.swim()) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Outputs: Swims!</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(duck.fly()) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Outputs: Flies!</span></span></code></pre></figure>
|
|||
|
<h4 id="super-函数"><code>super</code> 函数<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#super-函数" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>在 Python 中,<code>super</code> 函数是一个内置函数,它可以用来调用父类的方法。这在你需要在子类中扩展父类的方法时特别有用。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Animal</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> __init__</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self, name):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.name </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> name</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Dog 的 __init__ 方法中使用了 super().__init__(name) 来调用 Animal 的 __init__ 方法。</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Dog</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">Animal</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> __init__</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self, name, breed):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> super</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">().</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">__init__</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(name) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 调用父类的构造方法</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.breed </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> breed </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 新增的属性</span></span></code></pre></figure>
|
|||
|
<h2 id="多态">多态<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#多态" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<h3 id="多态的概念和用途">多态的概念和用途<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#多态的概念和用途" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>在面向对象编程(OOP)中,多态性是一种允许一个实体采取多种形态的能力。更具体地说,多态是指通过同一个接口,使用不同的实例,可以产生不同的结果。这使得我们可以在运行时确定我们正在使用的对象类型,然后执行相应的操作。</p>
|
|||
|
<p>多态的优势主要有两点:</p>
|
|||
|
<ol>
|
|||
|
<li><strong>提高代码的灵活性</strong>:多态能够让我们以更一般的方式编写代码,处理更广泛的数据类型,而不仅仅是特定的单一数据类型。这使得我们的代码更加灵活和可维护。</li>
|
|||
|
<li><strong>提高代码的可扩展性</strong>:如果我们想要添加新的数据类型,我们只需要确保它们遵循现有的接口规定。这使得我们的代码更易于扩展和改进,而无需修改大量现有代码。</li>
|
|||
|
<li><strong>促进了类之间的解耦</strong>:多态允许系统在不修改现有代码的情况下,添加新的对象类型。</li>
|
|||
|
</ol>
|
|||
|
<h3 id="python-中实现多态">Python 中实现多态<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#python-中实现多态" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>Python 是一种动态类型语言,意味着我们不需要明确地声明对象的类型。这就让我们在 Python 中能够非常容易地实现多态。以下是 Python 中实现多态的一些方法:</p>
|
|||
|
<h4 id="方法重写">方法重写<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#方法重写" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>在 Python 中,子类可以重写父类的方法。这就意味着,当我们在子类中调用一个父类的方法时,将会执行子类中定义的版本,而非父类中的原始版本。这使得我们可以改变子类中方法的行为,以符合我们的需求。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Animal</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> sound</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "Generic animal sound"</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Dog类重写了Animal类的sound方法,所以当我们调用Dog实例的sound方法时,会返回"Woof!",而不是"Generic animal sound"。</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Dog</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">Animal</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> sound</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "Woof!"</span></span></code></pre></figure>
|
|||
|
<h4 id="duck-typing">Duck Typing<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#duck-typing" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>Python 支持一种编程概念叫做 duck typing,这也是实现多态的一种方法。Duck typing 的核心思想是:如果一个对象能够像鸭子一样走路,像鸭子一样叫,那么我们就可以认为它是鸭子。对于 Python 来说,如果一个对象有我们需要的方法,我们就可以使用它,而不管它是什么类型的对象(运行时类型确定)。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> animal_sound</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(animal):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 这个函数只关心对象是否有sound方法,而不关心对象的具体类型</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> animal.sound()</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(animal_sound(Dog())) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 输出"Woof!"</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(animal_sound(Animal())) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 输出"Generic animal sound"</span></span></code></pre></figure>
|
|||
|
<h2 id="抽象类和接口">抽象类和接口<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#抽象类和接口" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<h3 id="抽象类和接口的概念">抽象类和接口的概念<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#抽象类和接口的概念" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>抽象类是一种特殊的类,它不能被实例化,只能被继承。抽象类中可以定义抽象方法,这些方法在抽象类中没有实现,在子类中必须实现。</p>
|
|||
|
<p>接口是一种特殊的抽象类,它只定义了一组方法的签名,没有提供任何实现。接口定义了一组行为,任何实现了这些行为的类都可以说是实现了这个接口。</p>
|
|||
|
<h3 id="python-的-abc-模块">Python 的 Abc 模块<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#python-的-abc-模块" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>在 Python 中,我们使用 abc 模块来创建抽象类和接口。abc 模块提供了 <code>ABC</code> 基类和 <code>abstractmethod</code> 装饰器,我们可以使用它们来定义抽象类和抽象方法。</p>
|
|||
|
<h3 id="python-中使用抽象类和接口">Python 中使用抽象类和接口<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#python-中使用抽象类和接口" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<h4 id="创建抽象类">创建抽象类<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#创建抽象类" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>我们可以使用 <code>ABC</code> 基类和 <code>abstractmethod</code> 装饰器来创建抽象类。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="Python" data-theme="github-light github-dark"><code data-language="Python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span>from abc import ABC, abstractmethod</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span># AbstractAnimal 是一个抽象类,它有一个抽象方法 sound。</span></span>
|
|||
|
<span data-line><span>class AbstractAnimal(ABC):</span></span>
|
|||
|
<span data-line><span> @abstractmethod</span></span>
|
|||
|
<span data-line><span> def sound(self):</span></span>
|
|||
|
<span data-line><span> pass</span></span></code></pre></figure>
|
|||
|
<h4 id="创建接口">创建接口<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#创建接口" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>在 Python 中,接口的概念可以通过完全由抽象方法构成的抽象类来实现。与抽象类类似,我们使用 ABC 基类和 abstractmethod 装饰器来定义接口。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> abc </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> ABC</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, abstractmethod</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># AnimalBehaviour 是一个接口,它定义了两个抽象方法:eat 和 sleep。</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> AnimalBehaviour</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ABC</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> @abstractmethod</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> eat</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> pass</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> @abstractmethod</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> sleep</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> pass</span></span>
|
|||
|
<span data-line> </span></code></pre></figure>
|
|||
|
<h4 id="实现接口">实现接口<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#实现接口" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>我们可以通过继承抽象类并实现其所有的抽象方法来实现接口。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="Python" data-theme="github-light github-dark"><code data-language="Python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span>class Dog(AbstractAnimal, AnimalBehaviour):</span></span>
|
|||
|
<span data-line><span> def sound(self):</span></span>
|
|||
|
<span data-line><span> return "Woof!"</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span> def eat(self):</span></span>
|
|||
|
<span data-line><span> return "The dog is eating."</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span> def sleep(self):</span></span>
|
|||
|
<span data-line><span> return "The dog is sleeping."</span></span></code></pre></figure></article><hr/><div class="page-footer"></div></div><div class="right sidebar"><div class="graph"><h3>关系图谱</h3><div class="graph-outer"><div id="graph-container" data-cfg="{"drag":true,"zoom":true,"depth":1,"scale":1.1,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[],"focusOnHover":false}"></div><button id="global-graph-icon" aria-label="Global Graph"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xml:space="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
|
|||
|
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
|
|||
|
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
|
|||
|
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
|
|||
|
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
|
|||
|
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
|
|||
|
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
|
|||
|
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
|
|||
|
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
|
|||
|
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
|
|||
|
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></button></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{"drag":true,"zoom":true,"depth":-1,"scale":0.9,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[],"focusOnHover":true}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class aria-controls="toc-content" aria-expanded="true"><h3>目录</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content" class><ul class="overflow"><li class="depth-0"><a href="#基本概念" data-for="基本概念">基本概念</a></li><li class="depth-1"><a href="#理念和用途" data-for="理念和用途">理念和用途</a></li><li class="depth-1"><a href="#特点" data-for="特点">特点</a></li><li class="depth-0"><a href="#python-中的类和对象" data-for="python-中的类和对象">Python 中的类和对象</a></li><li class="depth-1"><a href="#类和对象的概念" data-for="类和对象的概念">类和对象的概念</a></li><li class="depth-1"><a href="#类的属性和方法" data-for="类的属性和方法">类的属性和方法</a></li><li class="depth-1"><a href="#__init__" data-for="__init__">__init__</a></li><li class="depth-0"><a href="#封装" data-for="封装">封装</a></li><li class="depth-1"><a href="#概念和用途" data-for="概念和用途">概念和用途</a></li><li class="depth-1"><a href="#python-中实现封装" data-for="python-中实现封装">Python 中实现封装</a></li><li class="depth-0"><a href="#继承" data-for="继承">继承</a></li><li class="depth-1"><a href="#继承的概念和用途" data-for="继承的概念和用途">继承的概念和用途</a></li><li class="depth-1"><a href="#python-中实现继承" data-for="python-中实现继承">Python 中实现继承</a></li><li class="depth-0"><a href="#多态" data-for="多态">多态</a></li><li class="depth-1"><a href="#多态的概念和用途" data-for="多态的概念和用途">多态的概念和用途</a></li><li class="depth-1"><a href="#python-中实现多态" data-for="python-中实现多态">Python 中实现多态</a></li><li class="depth-0"><a href="#抽象类和接口" data-for="抽象类和接口">抽象类和接口</a></li><li class="depth-1"><a href="#抽象类和接口的概念" data-for="抽象类和接口的概念">抽象类和接口的概念</a></li><li class="depth-1"><a href="#python-的-abc-模块" data-for="python-的-abc-模块">Python 的 Abc 模块</a></li><li class="depth-1"><a href="#python-中使用抽象类和接口" data-for="python-中使用抽象类和接口">Python 中使用抽象类和接口</a></li></ul></div></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{"path":"Personal","collapsed":true},{"path":"Personal/Blog","collapsed":true},{"path":"Personal/Blog/2018","collapsed":true},{"path":"Personal/Blog/2020","collapsed":true},{"path":"Personal/Blog/2021","collapsed":true},{"path":"Personal/Blog/2022","collapsed":true},{"path":"Personal/Blog/2023","collapsed":true},{"path":"Personal/Blog/2024","collapsed":true},{"path":"Personal/Book","collapsed":true},{"path":"Personal/Book/个人成长","collapsed":true},{"path":"Personal/Book/医学健康&
|
|||
|
</script><script type="module">
|
|||
|
let mermaidImport = undefined
|
|||
|
document.addEventListener('nav', async () => {
|
|||
|
if (document.querySelector("code.mermaid")) {
|
|||
|
mermaidImport ||= await import('https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs')
|
|||
|
const mermaid = mermaidImport.default
|
|||
|
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
|
|||
|
mermaid.initialize({
|
|||
|
startOnLoad: false,
|
|||
|
securityLevel: 'loose',
|
|||
|
theme: darkMode ? 'dark' : 'default'
|
|||
|
})
|
|||
|
|
|||
|
await mermaid.run({
|
|||
|
querySelector: '.mermaid'
|
|||
|
})
|
|||
|
}
|
|||
|
});
|
|||
|
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../postscript.js" type="module"></script></html>
|