ts-learnings/_book/03_classes.html
2019-04-04 16:11:16 +08:00

717 lines
59 KiB
HTML

<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>类 · GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="04_interfaces.html" />
<link rel="prev" href="02_variables_declaration.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="./">
<a href="./">
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" data-path="01_basic_data_types.html">
<a href="01_basic_data_types.html">
基本数据类型
</a>
</li>
<li class="chapter " data-level="1.3" data-path="02_variables_declaration.html">
<a href="02_variables_declaration.html">
变量声明
</a>
</li>
<li class="chapter active" data-level="1.4" data-path="03_classes.html">
<a href="03_classes.html">
</a>
</li>
<li class="chapter " data-level="1.5" data-path="04_interfaces.html">
<a href="04_interfaces.html">
接口
</a>
</li>
<li class="chapter " data-level="1.6" data-path="05_functions.html">
<a href="05_functions.html">
函数
</a>
</li>
<li class="chapter " data-level="1.7" data-path="06_generics.html">
<a href="06_generics.html">
泛型
</a>
</li>
<li class="chapter " data-level="1.8" data-path="07_enums.html">
<a href="07_enums.html">
枚举
</a>
</li>
<li class="chapter " data-level="1.9" data-path="08_type_inference.html">
<a href="08_type_inference.html">
类型推导
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="." ></a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h2 id="&#x7C7B;&#xFF08;classes&#xFF09;">&#x7C7B;&#xFF08;Classes&#xFF09;</h2>
<h2 id="&#x7B80;&#x4ECB;">&#x7B80;&#x4ECB;</h2>
<p>&#x4F20;&#x7EDF;&#x7684;JavaScript&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x4E0E;&#x57FA;&#x4E8E;&#x539F;&#x578B;&#x7684;&#x7EE7;&#x627F;&#xFF08;prototype-based inheritance&#xFF09;&#xFF0C;&#x6765;&#x5EFA;&#x7ACB;&#x53EF;&#x91CD;&#x7528;&#x7684;&#x7EC4;&#x4EF6;&#x3002;&#x4F46;&#x8FD9;&#x79CD;&#x5904;&#x7406;&#x4F1A;&#x4EE4;&#x5230;&#x90A3;&#x4E9B;&#x4E60;&#x60EF;&#x4E8E;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x65B9;&#x6CD5;&#x7684;&#x7A0B;&#x5E8F;&#x5458;&#x4E0D;&#x81EA;&#x5728;&#xFF0C;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x65B9;&#x6CD5;&#x6709;&#x7740;&#x529F;&#x80FD;&#x7EE7;&#x627F;&#x3001;&#x5BF9;&#x8C61;&#x5EFA;&#x7ACB;&#x81EA;&#x7C7B;&#x7B49;&#x7279;&#x6027;&#x3002;&#x4ECE;ECMAScript 2015, &#x4E5F;&#x5C31;&#x662F;ES6&#x5F00;&#x59CB;&#xFF0C;JavaScript&#x7A0B;&#x5E8F;&#x5458;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x7684;&#x3001;&#x57FA;&#x4E8E;&#x7C7B;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x6765;&#x6784;&#x5EFA;&#x4ED6;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x4E86;&#x3002;&#x5728;TypeScript&#x4E2D;&#xFF0C;&#x73B0;&#x5728;&#x5C31;&#x53EF;&#x4EE5;&#x7528;&#x4E0A;&#x8FD9;&#x4E9B;&#x6280;&#x672F;&#xFF0C;&#x5E76;&#x5C06;&#x5176;&#x5411;&#x4E0B;&#x7F16;&#x8BD1;&#x5230;&#x53EF;&#x5DE5;&#x4F5C;&#x4E8E;&#x6240;&#x6709;&#x4E3B;&#x6D41;&#x6D4F;&#x89C8;&#x5668;&#x4E0E;&#x5E73;&#x53F0;&#x7684;JavaScript&#xFF0C;&#x800C;&#x65E0;&#x9700;&#x7B49;&#x5F85;&#x4E0B;&#x4E00;&#x7248;&#x7684;JavaScript&#x3002;</p>
<h2 id="&#x5173;&#x4E8E;&#x7C7B;">&#x5173;&#x4E8E;&#x7C7B;</h2>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x57FA;&#x4E8E;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x5427;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Greeter {
greeting: <span class="hljs-built_in">string</span>;
<span class="hljs-keyword">constructor</span> ( message: string ) {
<span class="hljs-keyword">this</span>.greeting = message;
}
greet () {
<span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Hello, &quot;</span> + <span class="hljs-keyword">this</span>.greeting;
}
}
<span class="hljs-keyword">let</span> greeter = <span class="hljs-keyword">new</span> Greeter (<span class="hljs-string">&quot;world&quot;</span>);
</code></pre>
<p>&#x5982;&#x4F60;&#x4E4B;&#x524D;&#x66FE;&#x4F7F;&#x7528;&#x8FC7;C#&#x6216;Java, &#x90A3;&#x4E48;&#x5C31;&#x5E94;&#x8BE5;&#x5BF9;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x7684;&#x8BED;&#x6CD5;&#x6BD4;&#x8F83;&#x719F;&#x6089;&#x4E86;&#x3002;&#x8FD9;&#x91CC;&#x58F0;&#x660E;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7C7B;<code>Greeter</code>&#xFF08;declare a new class <code>Greeter</code>&#xFF09;&#x3002;&#x6B64;&#x7C7B;&#x6709;&#x4E09;&#x4E2A;&#x6210;&#x5458;&#xFF1A;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;<code>greeting</code>&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x4E00;&#x4E2A;&#x6784;&#x5EFA;&#x5668;&#xFF0C;&#x4EE5;&#x53CA;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;<code>greet</code>&#x3002;</p>
<p>&#x5728;&#x7C7B;&#x4E2D;&#xFF0C;&#x5C06;&#x6CE8;&#x610F;&#x5230;&#x5F53;&#x5BF9;&#x8BE5;&#x7C7B;&#x7684;&#x67D0;&#x4E2A;&#x6210;&#x5458;&#x8FDB;&#x884C;&#x5F15;&#x7528;&#x65F6;&#xFF0C;&#x5728;&#x8BE5;&#x6210;&#x5458;&#x524D;&#x52A0;&#x4E0A;&#x4E86;<code>this.</code>&#x3002;&#x8FD9;&#x5C31;&#x8868;&#x540D;&#x90A3;&#x662F;&#x4E00;&#x4E2A;&#x6210;&#x5458;&#x8BBF;&#x95EE;&#xFF08;a member access&#xFF09;&#x3002;</p>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x7684;&#x6700;&#x540E;&#x4E00;&#x884C;&#x4F7F;&#x7528;<code>new</code>&#x5173;&#x952E;&#x5B57;&#x6784;&#x5EFA;&#x51FA;&#x8BE5;<code>Greeter</code>&#x7C7B;&#x7684;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#xFF08;construct an instance of the <code>Greeter</code> class by using <code>new</code>&#xFF09;&#x3002;&#x8FD9;&#x8C03;&#x7528;&#x4E86;&#x5148;&#x524D;&#x6240;&#x5B9A;&#x4E49;&#x7684;&#x6784;&#x5EFA;&#x51FD;&#x6570;&#xFF08;constructor, &#x6784;&#x5EFA;&#x5668;&#xFF09;&#xFF0C;&#x4ECE;&#x800C;&#x4EE5;&#x8BE5;<code>Greeter</code>&#x4E3A;&#x5916;&#x5F62;(shape)&#xFF0C;&#x8FDB;&#x884C;&#x65B0;&#x5BF9;&#x8C61;&#x7684;&#x521B;&#x5EFA;&#xFF0C;&#x5E76;&#x8FD0;&#x884C;&#x8BE5;&#x6784;&#x9020;&#x51FD;&#x6570;&#x5BF9;&#x5176;&#x8FDB;&#x884C;&#x521D;&#x59CB;&#x5316;&#x3002;</p>
<h2 id="&#x7EE7;&#x627F;&#xFF08;inheritance">&#x7EE7;&#x627F;&#xFF08;Inheritance)</h2>
<p>&#x5728;TypeScript&#x4E2D;&#x53EF;&#x4F7F;&#x7528;&#x901A;&#x5E38;&#x7684;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x6A21;&#x5F0F;&#xFF08;common object-oriented patterns)&#x3002;&#x800C;&#x57FA;&#x4E8E;&#x7C7B;&#x7F16;&#x7A0B;&#x7684;&#x6700;&#x4E3A;&#x57FA;&#x7840;&#x6A21;&#x5F0F;&#x4E4B;&#x4E00;&#xFF0C;&#x5C31;&#x662F;&#x5177;&#x5907;&#x8FD0;&#x7528;&#x7EE7;&#x627F;&#xFF0C;&#x5BF9;&#x65E2;&#x6709;&#x7C7B;&#x52A0;&#x4EE5;&#x6269;&#x5C55;&#xFF0C;&#x4ECE;&#x800C;&#x521B;&#x5EFA;&#x51FA;&#x65B0;&#x7C7B;&#x7684;&#x80FD;&#x529B;&#x4E86;&#x3002;</p>
<p>&#x770B;&#x770B;&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Animal {
move ( distanceInMeters: <span class="hljs-built_in">number</span> = <span class="hljs-number">0</span> ) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Animal moved <span class="hljs-subst">${distanceInMeters}</span>m.`</span>);
}
}
<span class="hljs-keyword">class</span> Dog extends Animal {
bark () {
<span class="hljs-built_in">console</span>.log (<span class="hljs-string">&apos;Woof! Woof!&apos;</span>);
}
}
<span class="hljs-keyword">const</span> dog = <span class="hljs-keyword">new</span> Dog ();
dog.bark();
dog.move(<span class="hljs-number">10</span>);
dog.bark();
</code></pre>
<p>&#x6B64;&#x5B9E;&#x4F8B;&#x7ED9;&#x51FA;&#x4E86;&#x6700;&#x57FA;&#x672C;&#x7684;&#x7EE7;&#x627F;&#x7279;&#x6027;&#xFF1A;&#x7C7B;&#x81EA;&#x57FA;&#x7C7B;&#x7EE7;&#x627F;&#x5C5E;&#x6027;&#x53CA;&#x65B9;&#x6CD5;&#xFF08;classes inherit properties and methods from base classes&#xFF09;&#x3002;&#x8FD9;&#x91CC;&#x7684;<code>Dog</code>&#x7C7B;&#x662F;&#x4E00;&#x4E2A;&#x4F7F;&#x7528;<code>extends</code>&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x6D3E;&#x751F;&#x81EA;<code>Animal</code>&#x8FD9;&#x4E2A; <em>&#x57FA;&#x7C7B;&#xFF08;base class&#xFF09;</em> &#x7684; <em>&#x6D3E;&#x751F;&#xFF08;derived&#xFF09;</em> &#x7C7B;&#x3002;&#x6D3E;&#x751F;&#x7C7B;&#xFF08;derived classes&#xFF09;&#x901A;&#x5E38;&#x88AB;&#x79F0;&#x4F5C; <em>&#x5B50;&#x7C7B;&#xFF08;subclass&#xFF09;</em> &#xFF0C;&#x540C;&#x65F6;&#x57FA;&#x7C7B;&#x53C8;&#x901A;&#x5E38;&#x88AB;&#x53EB;&#x505A; <em>&#x8D85;&#x7C7B;&#xFF08;superclass&#xFF09;</em> &#x3002;</p>
<p>&#x56E0;&#x4E3A;<code>Dog</code>&#x6269;&#x5C55;&#x4E86;&#x6765;&#x81EA;<code>Animal</code>&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x91CC;&#x5C31;&#x80FD;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x53EF;&#x540C;&#x65F6;<code>bark()</code>&#x53CA;<code>move()</code>&#x7684;<code>Dog</code>&#x7684;&#x5B9E;&#x4F8B;&#x3002;</p>
<p>&#x518D;&#x6765;&#x770B;&#x4E00;&#x4E2A;&#x66F4;&#x590D;&#x6742;&#x7684;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Animal {
name: <span class="hljs-built_in">string</span>;
<span class="hljs-keyword">constructor</span> (theName: string) { <span class="hljs-keyword">this</span>.name = theName; }
move ( distanceInMeters: <span class="hljs-built_in">number</span> = <span class="hljs-number">0</span> ) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${this.name}</span> moved <span class="hljs-subst">${distanceInMeters}</span>m.`</span>);
}
}
<span class="hljs-keyword">class</span> Snake extends Animal {
<span class="hljs-keyword">constructor</span> (name: string) { <span class="hljs-keyword">super</span>(name); }
move ( distanceInMeters = <span class="hljs-number">5</span> ) {
<span class="hljs-built_in">console</span>.log( <span class="hljs-string">&quot;Slithering...&quot;</span> );
<span class="hljs-keyword">super</span>.move(distanceInMeters);
}
}
<span class="hljs-keyword">class</span> Horse extends Animal {
<span class="hljs-keyword">constructor</span> (name: string) { <span class="hljs-keyword">super</span>(name); }
move (distanceInMeters = <span class="hljs-number">45</span>) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Galloping...&quot;</span>);
<span class="hljs-keyword">super</span>.move(distanceInMeters);
}
}
<span class="hljs-keyword">let</span> sam = <span class="hljs-keyword">new</span> Snake(<span class="hljs-string">&quot;Sammy the Python&quot;</span>);
<span class="hljs-keyword">let</span> tom: Animal = <span class="hljs-keyword">new</span> Horse(<span class="hljs-string">&quot;Tommy the Palomino&quot;</span>);
sam.move();
tom.move(<span class="hljs-number">34</span>);
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#x6DB5;&#x76D6;&#x4E86;&#x4E00;&#x4E9B;&#x524D;&#x9762;&#x6CA1;&#x6709;&#x63D0;&#x5230;&#x7684;&#x5176;&#x5B83;&#x7279;&#x6027;&#x3002;&#x518D;&#x5EA6;&#x770B;&#x5230;&#x4F7F;&#x7528;&#x4E86;<code>extends</code>&#x5173;&#x952E;&#x5B57;&#x5EFA;&#x7ACB;&#x4E86;<code>Animal</code>&#x7684;&#x4E24;&#x4E2A;&#x65B0;&#x5B50;&#x7C7B;&#xFF1A;<code>Horse</code>&#x4E0E;<code>Snake</code>&#x3002;</p>
<p>&#x4E0E;&#x524D;&#x4E00;&#x793A;&#x4F8B;&#x7684;&#x4E00;&#x70B9;&#x4E0D;&#x540C;&#xFF0C;&#x5C31;&#x662F;&#x6BCF;&#x4E2A;&#x542B;&#x6709;&#x6784;&#x5EFA;&#x5668;&#x7684;&#x6D3E;&#x751F;&#x7C7B;&#xFF0C;&#x90FD; <strong>&#x5FC5;&#x987B;</strong> &#x8C03;&#x7528;<code>super()</code>&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x4EE5;&#x6267;&#x884C;&#x5230;&#x57FA;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x5426;&#x5219;&#x7F16;&#x8BD1;&#x5668;&#x5C06;&#x62A5;&#x9519;&#xFF08;<code>error TS2377: Constructors for derived classes must contain a &apos;super&apos; call.</code>, &#x53CA;<code>error TS17009: &apos;super&apos; must be called before accessing &apos;this&apos; in the constructor of a derived class</code>&#xFF09;&#x3002;&#x6B64;&#x5916;&#xFF0C;&#x5728;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4F53;&#x4E2D;&#xFF0C;&#x4E8E;&#x8BBF;&#x95EE;<code>this</code>&#x4E0A;&#x7684;&#x67D0;&#x4E2A;&#x5C5E;&#x6027;&#x4E4B;&#x524D;&#xFF0C; <strong>&#x5FC5;&#x987B;</strong> &#x5148;&#x8C03;&#x7528;<code>super()</code>&#x65B9;&#x6CD5;&#x3002;TypeScript&#x7F16;&#x8BD1;&#x5668;&#x5C06;&#x5F3A;&#x5236;&#x6267;&#x884C;&#x6B64;&#x4E00;&#x89C4;&#x5219;&#x3002;</p>
<p>&#x8BE5;&#x793A;&#x4F8B;&#x8FD8;&#x5C55;&#x793A;&#x4E86;&#x600E;&#x6837;&#x4EE5;&#x7279;&#x5B9A;&#x4E8E;&#x5B50;&#x7C7B;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x8986;&#x5199;&#x57FA;&#x7C7B;&#x4E2D;&#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x91CC;&#x7684;<code>Snake</code>&#x4E0E;<code>Horse</code>&#x90FD;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x8986;&#x5199;<code>Animal</code>&#x4E2D;&#x7684;<code>move()</code>&#x65B9;&#x6CD5;&#x7684;<code>move()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x4ECE;&#x800C;&#x8D4B;&#x4E88;&#x5176;&#x9488;&#x5BF9;&#x4E0D;&#x540C;&#x7C7B;&#x7684;&#x7279;&#x5B9A;&#x529F;&#x80FD;&#x3002;&#x8BF7;&#x6CE8;&#x610F;&#x5C31;&#x7B97;<code>tom</code>&#x662F;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;<code>Animal</code>&#x52A0;&#x4EE5;&#x58F0;&#x660E;&#x7684;&#xFF0C;&#x5176;&#x503C;&#x8FD8;&#x662F;&#x4E00;&#x4E2A;<code>Horse</code>&#xFF0C; &#x5BF9;<code>tom.move(34)</code>&#x7684;&#x8C03;&#x7528;&#xFF0C;&#x5C06;&#x8C03;&#x7528;&#x5230;<code>Horse</code>&#x4E2D;&#x6240;&#x8986;&#x5199;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-bash">Slithering...
Sammy the Python moved 5m.
Galloping...
Tommy the Palomino moved 34m.
</code></pre>
<h2 id="&#x516C;&#x5171;&#x5C5E;&#x6027;&#x3001;&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x4E0E;&#x53D7;&#x4FDD;&#x62A4;&#x7684;&#x4FEE;&#x6539;&#x5668;&#xFF08;public-private-and-protected-modifiers&#xFF09;">&#x516C;&#x5171;&#x5C5E;&#x6027;&#x3001;&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x4E0E;&#x53D7;&#x4FDD;&#x62A4;&#x7684;&#x4FEE;&#x6539;&#x5668;&#xFF08;Public, Private and protected modifiers&#xFF09;</h2>
<h3 id="&#x5C5E;&#x6027;&#x9ED8;&#x8BA4;&#x662F;&#x516C;&#x5171;&#x7684;&#xFF08;public-by-default&#xFF09;">&#x5C5E;&#x6027;&#x9ED8;&#x8BA4;&#x662F;&#x516C;&#x5171;&#x7684;&#xFF08;Public by default&#xFF09;</h3>
<p>&#x5728;&#x4E0A;&#x9762;&#x8FD9;&#x4E9B;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x53EF;&#x5728;&#x6574;&#x4E2A;&#x7A0B;&#x5E8F;&#x4E2D;&#x81EA;&#x7531;&#x5730;&#x8BBF;&#x95EE;&#x5230;&#x6240;&#x58F0;&#x660E;&#x7684;&#x90A3;&#x4E9B;&#x6210;&#x5458;&#x3002;&#x5982;&#x4F60;&#x719F;&#x6089;&#x5176;&#x5B83;&#x8BED;&#x8A00;&#x4E2D;&#x7684;&#x7C7B;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x53EF;&#x80FD;&#x5DF2;&#x7ECF;&#x6CE8;&#x610F;&#x5230;&#x4E0A;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x4E0D;&#x5FC5;&#x4F7F;&#x7528;<code>public</code>&#x5173;&#x952E;&#x5B57;&#x6765;&#x8FBE;&#x5230;&#x6B64;&#x76EE;&#x7684;&#xFF1B;&#x6BD4;&#x5982;&#xFF0C;C#&#x5C31;&#x8981;&#x6C42;&#x663E;&#x5F0F;&#x5730;&#x7ED9;&#x6210;&#x5458;&#x6253;&#x4E0A;<code>public</code>&#x6807;&#x7B7E;&#xFF0C;&#x4EE5;&#x4EE4;&#x5230;&#x5176;&#x5BF9;&#x5916;&#x90E8;&#x53EF;&#x89C1;&#x3002;&#x800C;&#x5728;TypeScript&#x4E2D;&#xFF0C;&#x9ED8;&#x8BA4;&#x5404;&#x6210;&#x5458;&#x90FD;&#x662F;&#x516C;&#x5171;&#x7684;&#x3002;</p>
<p>&#x5F53;&#x7136;&#x4E5F;&#x53EF;&#x4EE5;&#x5C06;&#x67D0;&#x4E2A;&#x6210;&#x5458;&#x663E;&#x5F0F;&#x5730;&#x6807;&#x8BB0;&#x4E3A;<code>public</code>&#x3002;&#x53EF;&#x4EE5;&#x4E0B;&#x9762;&#x7684;&#x5F62;&#x5F0F;&#x7F16;&#x5199;&#x4E0A;&#x4E00;&#x5C0F;&#x8282;&#x4E2D;&#x7684;<code>Animal</code>&#x7C7B;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Animal {
<span class="hljs-keyword">public</span> name: <span class="hljs-built_in">string</span>;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">constructor</span> ( theName: string ) { <span class="hljs-keyword">this</span>.name = theName; }
<span class="hljs-keyword">public</span> move ( distanceInMeters: <span class="hljs-built_in">number</span> ) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${this.name}</span> moved <span class="hljs-subst">${distanceInMeters}</span>m.`</span>);
}
}
</code></pre>
<h3 id="&#x638C;&#x63E1;private">&#x638C;&#x63E1;<code>private</code></h3>
<p>&#x5F53;&#x67D0;&#x4E2A;&#x6210;&#x5458;&#x88AB;&#x6807;&#x8BB0;&#x4E3A;<code>private</code>&#x65F6;&#xFF0C;&#x5176;&#x5C31;&#x4E0D;&#x80FD;&#x4ECE;&#x5305;&#x542B;&#x5B83;&#x7684;&#x7C7B;&#x7684;&#x5916;&#x90E8;&#x8BBF;&#x95EE;&#x5230;&#x4E86;&#x3002;&#x6BD4;&#x5982;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Animal {
<span class="hljs-keyword">private</span> name: <span class="hljs-built_in">string</span>;
<span class="hljs-keyword">constructor</span> ( theName: string ) { <span class="hljs-keyword">this</span>.name = theName; }
}
<span class="hljs-keyword">new</span> Animal(<span class="hljs-string">&quot;Cat&quot;</span>).name(); <span class="hljs-comment">// &#x62A5;&#x9519;&#xFF1A;`name` &#x662F;&#x79C1;&#x6709;&#x7684;, error TS2341: Property &apos;name&apos; is private and only accessible within class &apos;Creature&apos;.</span>
</code></pre>
<p>TypeScript&#x662F;&#x4E00;&#x4E2A;&#x7ED3;&#x6784;&#x5316;&#x7684;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x3002;&#x5728;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x65E0;&#x8BBA;&#x5B83;&#x4EEC;&#x6765;&#x81EA;&#x4F55;&#x5904;&#xFF0C;&#x81EA;&#x8981;&#x6240;&#x6709;&#x6210;&#x5458;&#x662F;&#x76F8;&#x5BB9;&#x7684;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x8BF4;&#x4E24;&#x4E2A;&#x7C7B;&#x578B;&#x672C;&#x8EAB;&#x4E5F;&#x662F;&#x76F8;&#x5BB9;&#x7684;&#xFF08;TypeScript is a structural type system. When we compare two different types, regardless of where they come from, if the types of all members are compatible, then we say the types themselves are compatible&#xFF09;&#x3002;</p>
<p>&#x4F46;&#x5728;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x6709;&#x7740;<code>private</code>&#x53CA;<code>protected</code>&#x6210;&#x5458;&#x7684;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x5C06;&#x52A0;&#x4EE5;&#x4E0D;&#x540C;&#x7684;&#x5BF9;&#x5F85;&#x3002;&#x5BF9;&#x4E8E;&#x4E24;&#x4E2A;&#x88AB;&#x8BA4;&#x4E3A;&#x662F;&#x76F8;&#x5BB9;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x5982;&#x5176;&#x4E2D;&#x4E4B;&#x4E00;&#x6709;&#x4E00;&#x4E2A;<code>private</code>&#x6210;&#x5458;&#xFF0C;&#x90A3;&#x4E48;&#x53E6;&#x4E00;&#x4E2A;&#x5C31;&#x5FC5;&#x987B;&#x8981;&#x6709;&#x4E00;&#x4E2A;&#x6E90;&#x81EA;&#x540C;&#x6837;&#x58F0;&#x660E;&#x7684;<code>private</code>&#x6210;&#x5458;&#x3002;&#x540C;&#x6837;&#x7684;&#x89C4;&#x5219;&#x4E5F;&#x9002;&#x7528;&#x4E8E;&#x90A3;&#x4E9B;<code>protected</code>&#x6210;&#x5458;&#xFF08;For two types to be considered compatible, if one of them has a <code>private</code> member, then the other must have a <code>private</code> member that originated in the same declaration. The same applies to <code>protected</code> members&#xFF09;&#x3002;</p>
<p>&#x4E3A;&#x641E;&#x6E05;&#x695A;&#x8FD9;&#x4E00;&#x89C4;&#x5219;&#x5728;&#x5B9E;&#x8DF5;&#x4E2D;&#x5982;&#x4F55;&#x53D1;&#x6325;&#x4F5C;&#x7528;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x770B;&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Animal {
<span class="hljs-keyword">private</span> name: <span class="hljs-built_in">string</span>;
<span class="hljs-keyword">constructor</span> ( theName: string ) { <span class="hljs-keyword">this</span>.name = theName; }
}
Class Rhino extends Animal {
<span class="hljs-keyword">constructor</span> () { <span class="hljs-keyword">super</span> (<span class="hljs-string">&apos;Rhino&apos;</span>); }
}
Class Employee {
<span class="hljs-keyword">private</span> name: <span class="hljs-built_in">string</span>;
<span class="hljs-keyword">constructor</span> ( theName: string ) { <span class="hljs-keyword">this</span>.name = theName; }
}
<span class="hljs-keyword">let</span> animal = <span class="hljs-keyword">new</span> Animal (<span class="hljs-string">&quot;Goat&quot;</span>);
<span class="hljs-keyword">let</span> rhino = <span class="hljs-keyword">new</span> Rhino();
<span class="hljs-keyword">let</span> employee = <span class="hljs-keyword">new</span> Employee(<span class="hljs-string">&apos;Bob&apos;</span>);
animal = rhino;
animal = employee; <span class="hljs-comment">// &#x62A5;&#x9519;&#xFF1A; `Animal` &#x4E0E; `Employee` &#x5E76;&#x4E0D;&#x76F8;&#x5BB9;, error TS2322: Type &apos;Employee&apos; is not assignable to type &apos;Creature&apos;. Types have separate declarations of a private property &apos;name&apos;.</span>
</code></pre>
<p>&#x6B64;&#x793A;&#x4F8B;&#x6709;&#x7740;&#x4E00;&#x4E2A;<code>Animal</code>&#x4E0E;<code>Rhino</code>, &#x5176;&#x4E2D;<code>Rhino</code>&#x662F;<code>Animal</code>&#x7684;&#x4E00;&#x4E2A;&#x5B50;&#x7C7B;&#x3002;&#x540C;&#x65F6;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x65B0;&#x7684;<code>Employee</code>&#x7C7B;&#xFF0C;&#x5B83;&#x5728;&#x5F62;&#x72B6;&#x4E0A;&#x770B;&#x8D77;&#x6765;&#x4E0E;<code>Animal</code>&#x4E00;&#x81F4;&#x3002;&#x793A;&#x4F8B;&#x4E2D;&#x53C8;&#x521B;&#x5EFA;&#x4E86;&#x51E0;&#x4E2A;&#x8FD9;&#x4E9B;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;&#x5E76;&#x5C1D;&#x8BD5;&#x8FDB;&#x884C;&#x76F8;&#x4E92;&#x4E4B;&#x95F4;&#x7684;&#x8D4B;&#x503C;&#xFF0C;&#x4EE5;&#x770B;&#x770B;&#x4F1A;&#x53D1;&#x751F;&#x4EC0;&#x4E48;&#x3002;&#x56E0;&#x4E3A;<code>Animal</code>&#x4E0E;<code>Rhino</code>&#x5171;&#x4EAB;&#x4E86;&#x6765;&#x81EA;<code>Animal</code>&#x4E2D;&#x7684;&#x540C;&#x4E00;&#x58F0;&#x660E;<code>private name: string</code>&#x7684;&#x5B83;&#x4EEC;&#x5F62;&#x72B6;&#x7684;<code>private</code>&#x4FA7;&#xFF0C;&#x56E0;&#x6B64;&#x5B83;&#x4EEC;&#x662F;&#x76F8;&#x5BB9;&#x7684;&#xFF08;Because <code>Animal</code> and <code>Rhino</code> share the <code>private</code> side of their shape from the same declaration of <code>private name: string</code> in <code>Animal</code>, they are compatible&#xFF09;&#x3002;&#x4F46;&#x5BF9;&#x4E8E;<code>Employee</code>&#x5374;&#x4E0D;&#x662F;&#x8FD9;&#x6837;&#x4E86;&#x3002;&#x5728;&#x5C1D;&#x8BD5;&#x5C06;&#x4E00;&#x4E2A;<code>Employee</code>&#x8D4B;&#x503C;&#x7ED9;<code>Animal</code>&#x65F6;&#xFF0C;&#x5C31;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x8FD9;&#x4E9B;&#x7C7B;&#x578B;&#x4E0D;&#x76F8;&#x5BB9;&#x7684;&#x9519;&#x8BEF;&#x3002;&#x5C31;&#x7B97;<code>Employee</code>&#x4E5F;&#x6709;&#x7740;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;<code>name</code>&#x7684;<code>private</code>&#x6210;&#x5458;&#xFF0C;&#x4F46;&#x8BE5;&#x6210;&#x5458;&#x4E5F;&#x5E76;&#x4E0D;&#x662F;&#x90A3;&#x4E2A;&#x5728;<code>Animal</code>&#x4E2D;&#x6240;&#x58F0;&#x660E;&#x7684;&#x3002;</p>
<h3 id="&#x638C;&#x63E1;protected">&#x638C;&#x63E1;<code>protected</code></h3>
<p>&#x9664;&#x4E86;&#x7ECF;&#x7531;<code>protected</code>&#x5173;&#x952E;&#x5B57;&#x58F0;&#x660E;&#x7684;&#x6210;&#x5458;&#x4ECD;&#x53EF;&#x4EE5;&#x88AB;&#x6D3E;&#x751F;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x6240;&#x8BBF;&#x95EE;&#x5916;&#xFF0C;<code>protected</code>&#x4FEE;&#x6539;&#x5668;&#xFF08;the <code>protected</code> modifier&#xFF09;&#x4E0E;<code>private</code>&#x4FEE;&#x6539;&#x5668;&#x6709;&#x7740;&#x76F8;&#x4F3C;&#x7684;&#x884C;&#x4E3A;&#x3002;&#x6BD4;&#x5982;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Person {
<span class="hljs-keyword">protected</span> name: <span class="hljs-built_in">string</span>;
<span class="hljs-keyword">constructor</span> ( name: string ) { <span class="hljs-keyword">this</span>.name = name; }
}
<span class="hljs-keyword">class</span> Employee extends Person {
<span class="hljs-keyword">private</span> department: <span class="hljs-built_in">string</span>;
<span class="hljs-keyword">constructor</span> ( name: string, department: string ) {
<span class="hljs-keyword">super</span>(name);
<span class="hljs-keyword">this</span>.department = department;
}
<span class="hljs-keyword">public</span> getElevatorPitch () {
<span class="hljs-keyword">return</span> <span class="hljs-string">`Hello, my name is <span class="hljs-subst">${this.name}</span> and I work in <span class="hljs-subst">${this.department}</span>.`</span>;
}
}
<span class="hljs-keyword">let</span> howard = <span class="hljs-keyword">new</span> Employee (<span class="hljs-string">&quot;Howard&quot;</span>, <span class="hljs-string">&quot;Sales&quot;</span>);
<span class="hljs-built_in">console</span>.log(howard.getElevatorPitch());
<span class="hljs-built_in">console</span>.log(howard.name); <span class="hljs-comment">// &#x62A5;&#x9519;&#xFF1A; error TS2445: Property &apos;name&apos; is protected and only accessible within class &apos;Person&apos; and its subclasses.</span>
</code></pre>
<h2 id="&#x5173;&#x4E8E;&#x53EA;&#x8BFB;&#x4FEE;&#x6539;&#x5668;&#xFF08;readonly-modifier&#xFF09;">&#x5173;&#x4E8E;&#x53EA;&#x8BFB;&#x4FEE;&#x6539;&#x5668;&#xFF08;Readonly modifier&#xFF09;</h2>
<p>&#x4F7F;&#x7528;<code>readonly</code>&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x53EF;&#x4EE4;&#x5230;&#x5C5E;&#x6027;&#x53EA;&#x8BFB;&#x3002;&#x53EA;&#x8BFB;&#x7684;&#x5C5E;&#x6027; <strong>&#x5FC5;&#x987B;&#x5728;&#x5176;&#x58F0;&#x660E;&#x5904;&#x6216;&#x6784;&#x9020;&#x51FD;&#x6570;&#x91CC;&#x8FDB;&#x884C;&#x521D;&#x59CB;&#x5316;</strong> &#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Octopus {
readonly name: <span class="hljs-built_in">string</span>;
readonly numberOfLegs = <span class="hljs-number">8</span>;
<span class="hljs-keyword">constructor</span> (theName: string) {
<span class="hljs-keyword">this</span>.name = theName;
}
}
<span class="hljs-keyword">let</span> dad = <span class="hljs-keyword">new</span> Octopus (<span class="hljs-string">&quot;Man with the 8 strong legs&quot;</span>);
dad.name = <span class="hljs-string">&quot;Man with the 3-piece suit&quot;</span>; <span class="hljs-comment">// &#x62A5;&#x9519;&#xFF0C;`name` &#x662F;&#x53EA;&#x8BFB;&#x7684;&#x3002;error TS2540: Cannot assign to &apos;name&apos; because it is a constant or a read-only property.</span>
</code></pre>
<h3 id="&#x53C2;&#x6570;&#x5F0F;&#x5C5E;&#x6027;&#xFF08;parameter-properties&#xFF09;">&#x53C2;&#x6570;&#x5F0F;&#x5C5E;&#x6027;&#xFF08;Parameter properties&#xFF09;</h3>
<p>&#x4E0A;&#x4E00;&#x4E2A;&#x793A;&#x4F8B;&#x4E0D;&#x5F97;&#x4E0D;&#x5728;<code>Octopus</code>&#x8FD9;&#x4E2A;&#x7C7B;&#x4E2D;&#xFF0C;&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x53EA;&#x8BFB;&#x6210;&#x5458;<code>name</code>&#xFF0C;&#x4EE5;&#x53CA;&#x4E00;&#x4E2A;&#x6784;&#x5EFA;&#x5668;&#x53C2;&#x6570;<code>theName</code>&#xFF0C;&#x4E14;&#x968F;&#x540E;&#x8981;&#x7ACB;&#x5373;&#x5C06;<code>name</code>&#x8BBE;&#x7F6E;&#x4E3A;<code>theName</code>&#x3002;&#x8FD9;&#x79CD;&#x505A;&#x6CD5;&#x88AB;&#x8BC1;&#x660E;&#x662F;&#x4E00;&#x79CD;&#x5341;&#x5206;&#x5E38;&#x89C1;&#x7684;&#x505A;&#x6CD5;&#x3002;&#x901A;&#x8FC7; <em>&#x53C2;&#x6570;&#x5F0F;&#x5C5E;&#x6027;&#xFF08;parameter properties&#xFF09;</em> &#x53EF;&#x5728;&#x4E00;&#x5904;&#x5C31;&#x5B8C;&#x6210;&#x6210;&#x5458;&#x7684;&#x521B;&#x5EFA;&#x4E0E;&#x521D;&#x59CB;&#x5316;&#x3002;&#x4E0B;&#x9762;&#x662F;&#x4F7F;&#x7528;&#x53C2;&#x6570;&#x5F0F;&#x5C5E;&#x6027;&#x65B9;&#x6CD5;&#xFF0C;&#x5BF9;&#x4E0A;&#x4E00;&#x4E2A;<code>Octopus</code>&#x7C7B;&#x7684;&#x66F4;&#x8FDB;&#x4E00;&#x6B65;&#x4FEE;&#x8BA2;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Octopus {
readonly numberOfLegs: <span class="hljs-built_in">number</span> = <span class="hljs-number">8</span>;
<span class="hljs-keyword">constructor</span> (readonly: name: string) {}
}
</code></pre>
<p>&#x8BF7;&#x6CE8;&#x610F;&#x8FD9;&#x91CC;&#x5B8C;&#x5168;&#x4E22;&#x5F03;&#x4E86;<code>theName</code>&#xFF0C;&#x800C;&#x4EC5;&#x4F7F;&#x7528;&#x6784;&#x5EFA;&#x5668;&#x4E0A;&#x7B80;&#x5316;&#x7684;<code>readonly name: string</code>&#x53C2;&#x6570;&#xFF0C;&#x8FDB;&#x884C;<code>name</code>&#x6210;&#x5458;&#x7684;&#x521B;&#x5EFA;&#x4E0E;&#x521D;&#x59CB;&#x5316;&#x3002;&#x4ECE;&#x800C;&#x5B9E;&#x73B0;&#x4E86;&#x5C06;&#x58F0;&#x660E;&#x4E0E;&#x8D4B;&#x503C;&#x5F3A;&#x56FA;&#x5230;&#x4E00;&#x4E2A;&#x5730;&#x65B9;&#x3002;</p>
<p>&#x53C2;&#x6570;&#x5F0F;&#x5C5E;&#x6027;&#x662F;&#x901A;&#x8FC7;&#x5728;&#x6784;&#x9020;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x524D;&#xFF0C;&#x52A0;&#x4E0A;&#x53EF;&#x8BBF;&#x95EE;&#x6027;&#x4FEE;&#x6539;&#x5668;&#xFF08;<code>public/private/protected</code>&#xFF09;&#x6216;<code>readonly</code>&#xFF0C;&#x6291;&#x6216;&#x540C;&#x65F6;&#x52A0;&#x4E0A;&#x53EF;&#x8BBF;&#x95EE;&#x6027;&#x4FEE;&#x6539;&#x5668;&#x4E0E;<code>readonly</code>&#xFF0C;&#x5F97;&#x4EE5;&#x58F0;&#x660E;&#x7684;&#x3002;&#x5BF9;&#x4E8E;&#x4E00;&#x4E2A;&#x58F0;&#x660E;&#x5E76;&#x521D;&#x59CB;&#x5316;&#x79C1;&#x6709;&#x6210;&#x5458;&#x7684;&#x53C2;&#x6570;&#x5316;&#x5C5E;&#x6027;&#xFF0C;&#x5C31;&#x4F7F;&#x7528;<code>private</code>&#x505A;&#x524D;&#x7F00;&#xFF1B;&#x5BF9;&#x4E8E;<code>public</code>&#x3001;<code>protected</code>&#x53CA;<code>readonly</code>&#x4EA6;&#x7136;&#x3002;</p>
<h2 id="&#x8BBF;&#x95EE;&#x5668;&#xFF08;accessors&#xFF09;">&#x8BBF;&#x95EE;&#x5668;&#xFF08;Accessors&#xFF09;</h2>
<p>TypeScript&#x652F;&#x6301;&#x4EE5;<code>getters/setters</code>&#x65B9;&#x5F0F;&#xFF0C;&#x6765;&#x62E6;&#x622A;&#x5BF9;&#x67D0;&#x5BF9;&#x8C61;&#x6210;&#x5458;&#x7684;&#x8BBF;&#x95EE;&#x3002;&#x6B64;&#x7279;&#x6027;&#x8D4B;&#x4E88;&#x5BF9;&#x5404;&#x4E2A;&#x5BF9;&#x8C61;&#x6210;&#x5458;&#x7684;&#x8BBF;&#x95EE;&#x4EE5;&#x4E00;&#x79CD;&#x66F4;&#x4E3A;&#x7CBE;&#x826F;&#x7684;&#x63A7;&#x5236;&#xFF08;TypeScript supports getters/setters as a way of intercepting accesses to a member of an object. This gives you a way of having finer-grained control over how a member is accessed on each object&#xFF09;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x5C06;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x7C7B;&#xFF0C;&#x8F6C;&#x6362;&#x6210;&#x4F7F;&#x7528;<code>get</code>&#x53CA;<code>set</code>&#x7684;&#x5F62;&#x5F0F;&#x3002;&#x9996;&#x5148;&#xFF0C;&#x4ECE;&#x6CA1;&#x6709;&#x83B7;&#x53D6;&#x5668;&#x4E0E;&#x8BBE;&#x7F6E;&#x5668;&#xFF08;getter and setter&#xFF09;&#x5F00;&#x59CB;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Employee {
fullName: <span class="hljs-built_in">string</span>;
}
<span class="hljs-keyword">let</span> employee = <span class="hljs-keyword">new</span> Employee ();
employee.fullName = <span class="hljs-string">&quot;Bob Smith&quot;</span>;
<span class="hljs-keyword">if</span> (employee.fullName) {
<span class="hljs-built_in">console</span>.log(employee.fullName);
}
</code></pre>
<p>&#x5C3D;&#x7BA1;&#x5141;&#x8BB8;&#x4EBA;&#x4E3A;&#x968F;&#x673A;&#x5BF9;<code>fullName</code>&#x8FDB;&#x884C;&#x76F4;&#x63A5;&#x8BBE;&#x7F6E;&#x76F8;&#x5F53;&#x65B9;&#x4FBF;&#xFF0C;&#x4F46;&#x5982;&#x679C;&#x67D0;&#x4EBA;&#x53EF;&#x4EE5;&#x7A81;&#x53D1;&#x5947;&#x60F3;&#x5730;&#x4FEE;&#x6539;&#x540D;&#x5B57;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x6837;&#x505A;&#x5C31;&#x53EF;&#x80FD;&#x5E26;&#x6765;&#x9EBB;&#x70E6;&#xFF08;while allowing people to randomly set <code>fullName</code> directly is pretty handy, this might get us in trouble if people can change names on a whim&#xFF09;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x4E00;&#x7248;&#x4E2D;&#xFF0C;&#x5C06;&#x5728;&#x5141;&#x8BB8;&#x7528;&#x6237;&#x4FEE;&#x6539;<code>employee</code>&#x5BF9;&#x8C61;&#x4E4B;&#x524D;&#xFF0C;&#x5148;&#x68C0;&#x67E5;&#x7528;&#x6237;&#x662F;&#x5426;&#x6709;&#x4E00;&#x4E2A;&#x53EF;&#x7528;&#x7684;&#x5BC6;&#x7801;&#x3002;&#x8FD9;&#x662F;&#x901A;&#x8FC7;&#x628A;&#x5BF9;<code>fullName</code>&#x7684;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;&#xFF0C;&#x66FF;&#x6362;&#x4E3A;&#x4E00;&#x4E2A;&#x5C06;&#x68C0;&#x67E5;&#x5BC6;&#x7801;&#x7684;<code>set</code>&#x65B9;&#x6CD5;&#x6765;&#x5B9E;&#x73B0;&#x7684;&#x3002;&#x540C;&#x65F6;&#x8FD8;&#x52A0;&#x5165;&#x4E86;&#x4E00;&#x4E2A;&#x76F8;&#x5E94;&#x7684;<code>get</code>&#x65B9;&#x6CD5;&#xFF0C;&#x4EE5;&#x5141;&#x8BB8;&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#x53EF;&#x4EE5;&#x65E0;&#x7F1D;&#x5730;&#x7EE7;&#x7EED;&#x5DE5;&#x4F5C;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">let</span> passcode = <span class="hljs-string">&quot;secret passcode&quot;</span>;
<span class="hljs-keyword">class</span> Employer {
<span class="hljs-keyword">private</span> _fullName: <span class="hljs-built_in">string</span>;
<span class="hljs-keyword">get</span> fullName(): <span class="hljs-built_in">string</span> {
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._fullName;
}
<span class="hljs-keyword">set</span> fullName(newName: <span class="hljs-built_in">string</span>) {
<span class="hljs-keyword">if</span> (passcode &amp;&amp; passcode === <span class="hljs-string">&quot;secret passcode&quot;</span>) {
<span class="hljs-keyword">this</span>._fullName = newName;
}
<span class="hljs-keyword">else</span> {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Error: Unauthenticated update of employer!&quot;</span>)
}
}
}
<span class="hljs-keyword">let</span> employer = <span class="hljs-keyword">new</span> Employer ();
employer.fullName = <span class="hljs-string">&quot;Bob Smith&quot;</span>;
<span class="hljs-keyword">if</span> (employer.fullName) {
<span class="hljs-built_in">console</span>.log(employer.fullName);
}
</code></pre>
<p>&#x4E3A;&#x4E86;&#x8BC1;&#x5B9E;&#x8FD9;&#x91CC;&#x7684;&#x8BBF;&#x95EE;&#x5668;&#x6709;&#x5BF9;&#x5BC6;&#x7801;&#x8FDB;&#x884C;&#x68C0;&#x67E5;&#xFF0C;&#x53EF;&#x4FEE;&#x6539;&#x4E00;&#x4E0B;&#x90A3;&#x4E2A;&#x5BC6;&#x7801;&#xFF0C;&#x770B;&#x770B;&#x5728;&#x5176;&#x4E0D;&#x5339;&#x914D;&#x65F6;&#xFF0C;&#x5C06;&#x5F97;&#x5230;&#x8B66;&#x544A;&#x6CA1;&#x6709;&#x66F4;&#x65B0;<code>employer</code>&#x6743;&#x9650;&#x7684;&#x6D88;&#x606F;&#x3002;</p>
<p>&#x6709;&#x5173;&#x8BBF;&#x95EE;&#x5668;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x4EE5;&#x4E0B;&#x51E0;&#x70B9;&#xFF1A;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x8BBF;&#x95EE;&#x5668;&#x7279;&#x6027;&#x8981;&#x6C42;&#x5C06;TypeScript&#x7F16;&#x8BD1;&#x5668;&#x8BBE;&#x7F6E;&#x5230;&#x8F93;&#x51FA;&#x4E3A;ECMAScript 5&#x6216;&#x66F4;&#x9AD8;&#x7248;&#x672C;&#x3002;&#x964D;&#x7EA7;&#x5230;ECMAScript 3&#x662F;&#x4E0D;&#x652F;&#x6301;&#x7684;&#x3002;&#x5176;&#x6B21;&#xFF0C;&#x5E26;&#x6709;<code>get</code>&#x5374;&#x6CA1;&#x6709;<code>set</code>&#x7684;&#x8BBF;&#x95EE;&#x5668;&#xFF0C;&#x5C06;&#x81EA;&#x52A8;&#x63A8;&#x7406;&#x5230;&#x662F;<code>readonly</code>&#x6210;&#x5458;&#x3002;&#x8FD9;&#x6837;&#x505A;&#x5728;&#x4ECE;&#x4EE3;&#x7801;&#x751F;&#x6210;&#x5230;<code>.d.ts</code>&#x6587;&#x4EF6;&#x65F6;&#x662F;&#x6709;&#x5E2E;&#x52A9;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x7528;&#x5230;&#x8BE5;&#x5C5E;&#x6027;&#x7684;&#x4EBA;&#x53EF;&#x4EE5;&#x660E;&#x767D;&#x4ED6;&#x4EEC;&#x4E0D;&#x80FD;&#x4FEE;&#x6539;&#x8BE5;&#x5C5E;&#x6027;&#x3002;</p>
<h2 id="&#x5173;&#x4E8E;&#x9759;&#x6001;&#x5C5E;&#x6027;&#xFF08;static-properties&#xFF09;">&#x5173;&#x4E8E;&#x9759;&#x6001;&#x5C5E;&#x6027;&#xFF08;Static Properties&#xFF09;</h2>
<p>&#x5230;&#x76EE;&#x524D;&#x4E3A;&#x6B62;&#xFF0C;&#x90FD;&#x8BA8;&#x8BBA;&#x7684;&#x662F;&#x7C7B;&#x7684; <em>&#x5B9E;&#x4F8B;&#xFF08;instance&#xFF09;</em> &#x6210;&#x5458;&#xFF0C;&#x8FD9;&#x4E9B;&#x6210;&#x5458;&#x90FD;&#x662F;&#x5728;&#x5BF9;&#x8C61;&#x88AB;&#x5B9E;&#x4F8B;&#x5316;&#x4E86;&#x540E;&#x624D;&#x51FA;&#x73B0;&#x5728;&#x5BF9;&#x8C61;&#x4E0A;&#x7684;&#xFF08;Up to this point, we&apos;ve only talked about the <em>instance</em> members of the class, those that show up on the object when it&apos;s instantiated&#xFF09;&#x3002;&#x5176;&#x5B9E;&#x8FD8;&#x53EF;&#x4EE5;&#x7ED9;&#x7C7B;&#x521B;&#x5EFA; <em>&#x9759;&#x6001;&#xFF08;static&#xFF09;</em> &#x6210;&#x5458;&#xFF0C;&#x6240;&#x8C13;&#x9759;&#x6001;&#x6210;&#x5458;&#xFF0C;&#x5C31;&#x662F;&#x5728;&#x7C7B;&#x672C;&#x8EAB;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x793A;&#x4F8B;&#x4E0A;&#x53EF;&#x89C1;&#x7684;&#x6210;&#x5458;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x5728;<code>origin</code>&#x4E0A;&#x4F7F;&#x7528;&#x4E86;<code>static</code>&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x56E0;&#x4E3A;<code>origin</code>&#x662F;&#x6240;&#x6709;<code>Grid</code>&#x7684;&#x901A;&#x7528;&#x503C;&#x3002;&#x5404;&#x4E2A;&#x5B9E;&#x4F8B;&#x901A;&#x8FC7;&#x5728;<code>origin</code>&#x524D;&#x52A0;&#x4E0A;&#x8BE5;&#x7C7B;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x6765;&#x8BBF;&#x95EE;&#x6B64;&#x503C;&#x3002;&#x4E0E;&#x5728;&#x8BBF;&#x95EE;&#x5B9E;&#x4F8B;&#x65F6;&#x5728;&#x524D;&#x9762;&#x52A0;&#x4E0A;<code>this.</code>&#x7C7B;&#x4F3C;&#xFF0C;&#x5728;&#x8BBF;&#x95EE;&#x9759;&#x6001;&#x6210;&#x5458;&#x65F6;&#xFF0C;&#x524D;&#x9762;&#x52A0;&#x7684;&#x662F;<code>Grid.</code>&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Grid {
<span class="hljs-keyword">static</span> origin = { x: <span class="hljs-number">0</span>, y: <span class="hljs-number">0</span> };
calculateDistanceFromOrigin ( point: { x: <span class="hljs-built_in">number</span>, y: <span class="hljs-built_in">number</span> } ) {
<span class="hljs-keyword">let</span> xDist = (point.x - Grid.origin.x);
<span class="hljs-keyword">let</span> yDist = (point.y - Grid.origin.y);
<span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.sqrt( xDist * xDist + yDist * yDist ) / <span class="hljs-keyword">this</span>.scale;
}
<span class="hljs-keyword">constructor</span> ( public scale: number ) {};
}
<span class="hljs-keyword">let</span> grid1 = <span class="hljs-keyword">new</span> Grid(<span class="hljs-number">1.0</span>);
<span class="hljs-keyword">let</span> grid2 = <span class="hljs-keyword">new</span> Grid(<span class="hljs-number">2.0</span>);
<span class="hljs-built_in">console</span>.log(grid1.calculateDistanceFromOrigin({x: <span class="hljs-number">10</span>, y: <span class="hljs-number">10</span>}));
<span class="hljs-built_in">console</span>.log(grid2.calculateDistanceFromOrigin({x: <span class="hljs-number">10</span>, y: <span class="hljs-number">10</span>}));
</code></pre>
<h2 id="&#x5173;&#x4E8E;&#x62BD;&#x8C61;&#x7C7B;&#xFF08;abstract-classes&#xFF09;">&#x5173;&#x4E8E;&#x62BD;&#x8C61;&#x7C7B;&#xFF08;Abstract Classes&#xFF09;</h2>
<p>&#x62BD;&#x8C61;&#x7C7B;&#x662F;&#x4E00;&#x4E9B;&#x53EF;&#x4EE5;&#x6D3E;&#x751F;&#x51FA;&#x5176;&#x5B83;&#x7C7B;&#x7684;&#x57FA;&#x7C7B;&#x3002;&#x62BD;&#x8C61;&#x7C7B;&#x4E0D;&#x53EF;&#x4EE5;&#x88AB;&#x76F4;&#x63A5;&#x5B9E;&#x4F8B;&#x5316;&#x3002;&#x4E0E;&#x63A5;&#x53E3;&#x7684;&#x4E0D;&#x540C;&#x4E4B;&#x5904;&#x5728;&#x4E8E;&#xFF0C;&#x67D0;&#x4E2A;&#x62BD;&#x8C61;&#x7C7B;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x5176;&#x6210;&#x5458;&#x5B9E;&#x73B0;&#x7684;&#x7EC6;&#x8282;&#x3002;&#x62BD;&#x8C61;&#x7C7B;&#x53CA;&#x67D0;&#x4E2A;&#x62BD;&#x8C61;&#x7C7B;&#x4E2D;&#x7684;&#x62BD;&#x8C61;&#x65B9;&#x6CD5;&#x7684;&#x5B9A;&#x4E49;&#xFF0C;&#x662F;&#x4F7F;&#x7528;<code>abstract</code>&#x5173;&#x952E;&#x5B57;&#x5B8C;&#x6210;&#x7684;&#xFF08;Unlike an interface, an abstract class may contain implementation details for its members. The <code>abstract</code> keyword is used to define abstract classes as well as abstract methods within an abstract class&#xFF09;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> Animal {
<span class="hljs-keyword">abstract</span> makeSound(): <span class="hljs-built_in">void</span>;
move(): <span class="hljs-built_in">void</span> {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;roaming the earth...&quot;</span>);
}
}
</code></pre>
<p>&#x62BD;&#x8C61;&#x7C7B;&#x4E2D;&#x88AB;&#x6807;&#x8BB0;&#x4E3A;<code>abstract</code>&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4E0D;&#x5305;&#x542B;&#x5176;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#xFF0C;&#x800C;&#x5FC5;&#x987B;&#x8981;&#x5728;&#x6D3E;&#x751F;&#x7C7B;&#x4E2D;&#x52A0;&#x4EE5;&#x5B9E;&#x73B0;&#x3002;&#x62BD;&#x8C61;&#x65B9;&#x6CD5;&#x4E0E;&#x63A5;&#x53E3;&#x65B9;&#x6CD5;&#x6709;&#x7740;&#x7C7B;&#x4F3C;&#x7684;&#x8BED;&#x6CD5;&#x3002;&#x4E8C;&#x8005;&#x90FD;&#x5B9A;&#x4E49;&#x4E86;&#x4E0D;&#x5E26;&#x6709;&#x65B9;&#x6CD5;&#x4F53;&#x7684;&#x67D0;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x7B7E;&#x540D;&#x3002;&#x4F46;&#x62BD;&#x8C61;&#x65B9;&#x6CD5;&#x5FC5;&#x987B;&#x5E26;&#x6709;<code>abstract</code>&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x540C;&#x65F6;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x8BBF;&#x95EE;&#x4FEE;&#x6539;&#x5668;&#xFF08;Abstract methods share a similar syntax to interface methods. Both define the signature of a method without including a method body. However, abstract methods must include the <code>abstract</code> keyword and may optionally include access modifiers&#xFF09;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> Department {
<span class="hljs-keyword">constructor</span> ( public name: string ) {}
printName (): <span class="hljs-built_in">void</span> {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Department name: &quot;</span> + <span class="hljs-keyword">this</span>.name);
}
<span class="hljs-keyword">abstract</span> printMeeting (): <span class="hljs-built_in">void</span>; <span class="hljs-comment">// &#x5728;&#x6D3E;&#x751F;&#x7C7B;&#x4E2D;&#x5FC5;&#x987B;&#x5B9E;&#x73B0;&#x6B64;&#x65B9;&#x6CD5;</span>
}
<span class="hljs-keyword">class</span> AccountingDepartment extends Department {
<span class="hljs-keyword">constructor</span> () {
<span class="hljs-keyword">super</span> (<span class="hljs-string">&quot;Accounting and Auditing&quot;</span>); <span class="hljs-comment">// &#x6D3E;&#x751F;&#x7C7B;&#x4E2D;&#x7684;&#x6784;&#x5EFA;&#x5668;&#x5FC5;&#x987B;&#x8C03;&#x7528; `super()` &#x65B9;&#x6CD5;</span>
}
printMeeting (): <span class="hljs-built_in">void</span> {
<span class="hljs-built_in">console</span>.log (<span class="hljs-string">&quot;The Accounting Department meets each Monday @10am.&quot;</span>);
}
generateReports (): <span class="hljs-built_in">void</span> {
<span class="hljs-built_in">console</span>.log (<span class="hljs-string">&quot;Generating accounting reports...&quot;</span>);
}
}
<span class="hljs-keyword">let</span> department: Department; <span class="hljs-comment">// &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5230;&#x62BD;&#x8C61;&#x7C7B;&#x578B;&#x7684;&#x5F15;&#x7528;&#x662F;&#x6CA1;&#x6709;&#x95EE;&#x9898;&#x7684; </span>
department = <span class="hljs-keyword">new</span> Department (); <span class="hljs-comment">// &#x62A5;&#x9519;&#xFF1A; &#x65E0;&#x6CD5;&#x521B;&#x5EFA;&#x67D0;&#x4E2A;&#x62BD;&#x8C61;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B; error TS2511: Cannot create an instance of the abstract class &apos;Department&apos;.</span>
department = <span class="hljs-keyword">new</span> AccountingDepartment(); <span class="hljs-comment">// &#x521B;&#x5EFA;&#x975E;&#x62BD;&#x8C61;&#x5B50;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x5E76;&#x4E3A;&#x5176;&#x8D4B;&#x503C;&#xFF0C;&#x6CA1;&#x6709;&#x95EE;&#x9898;</span>
department.printName();
department.printMeeting();
department.generateReports(); <span class="hljs-comment">// &#x62A5;&#x9519;&#xFF1A;&#x8BE5;&#x65B9;&#x6CD5;&#x5E76;&#x4E0D;&#x5B58;&#x5728;&#x4E0E;&#x6240;&#x58F0;&#x660E;&#x7684;&#x62BD;&#x8C61;&#x7C7B;&#x578B;&#x4E0A; error TS2339: Property &apos;generateReports&apos; does not exist on type &apos;Department&apos;.</span>
</code></pre>
<h2 id="&#x4E00;&#x4E9B;&#x9AD8;&#x7EA7;&#x6280;&#x5DE7;&#xFF08;advanced-techniques&#xFF09;">&#x4E00;&#x4E9B;&#x9AD8;&#x7EA7;&#x6280;&#x5DE7;&#xFF08;Advanced Techniques&#xFF09;</h2>
<h3 id="&#x5173;&#x4E8E;&#x6784;&#x5EFA;&#x5668;&#x51FD;&#x6570;">&#x5173;&#x4E8E;&#x6784;&#x5EFA;&#x5668;&#x51FD;&#x6570;</h3>
<p>&#x5F53;&#x5728;TypeScript&#x4E2D;&#x58F0;&#x660E;&#x7C7B;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5B9E;&#x9645;&#x4E0A;&#x5C31;&#x662F;&#x540C;&#x65F6;&#x521B;&#x5EFA;&#x51FA;&#x4E86;&#x591A;&#x4E2A;&#x7684;&#x58F0;&#x660E;&#x3002;&#x9996;&#x5148;&#x662F;&#x8BE5;&#x7C7B;&#x7684; <em>&#x5B9E;&#x4F8B;&#xFF08;instance&#xFF09;</em> &#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Greeter {
greeting: <span class="hljs-built_in">string</span>;
construtor (msg: <span class="hljs-built_in">string</span>) {
<span class="hljs-keyword">this</span>.greeting = msg;
}
greet () {
<span class="hljs-keyword">return</span> <span class="hljs-string">`Hello, <span class="hljs-subst">${this.greeting}</span>`</span>;
}
}
<span class="hljs-keyword">let</span> greeter: Greeter;
greeter = <span class="hljs-keyword">new</span> Greeter(<span class="hljs-string">&quot;World&quot;</span>);
<span class="hljs-built_in">console</span>.log(greeter.greet());
</code></pre>
<p>&#x8FD9;&#x91CC;&#x5728;&#x8BF4;&#x5230;<code>let greeter: Greeter</code>&#x65F6;&#xFF0C;&#x5C31;&#x4F7F;&#x7528;&#x4E86;<code>Greeter</code>&#x4F5C;&#x4E3A;&#x7C7B;<code>Greeter</code>&#x7684;&#x5B9E;&#x4F8B;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x8FD9;&#x5BF9;&#x4E8E;&#x90A3;&#x4E9B;&#x5176;&#x5B83;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x8BED;&#x8A00;&#x7684;&#x7A0B;&#x5E8F;&#x5458;&#x6765;&#x8BF4;&#xFF0C;&#x51E0;&#x4E4E;&#x662F;&#x7B2C;&#x4E8C;&#x5929;&#x6027;&#x4E86;&#xFF08;This is almost second nature to programmers from other object-oriented languages&#xFF09;&#x3002;</p>
<p>&#x540C;&#x65F6;&#x8FD8;&#x521B;&#x5EFA;&#x51FA;&#x540D;&#x4E3A;<code>&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF08;construtor function&#xFF09;</code>&#x7684;&#x53E6;&#x4E00;&#x4E2A;&#x503C;&#x3002;&#x8FD9;&#x5C31;&#x662F;&#x5728;&#x4F7F;&#x7528;<code>new</code>&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x5EFA;&#x7ACB;&#x8BE5;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x65F6;&#xFF0C;&#x6240;&#x8C03;&#x7528;&#x7684;&#x90A3;&#x4E2A;&#x51FD;&#x6570;&#x3002;&#x4E3A;&#x641E;&#x6E05;&#x695A;&#x8BE5;&#x51FD;&#x6570;&#x5B9E;&#x9645;&#x9762;&#x8C8C;&#xFF0C;&#x8BF7;&#x770B;&#x770B;&#x4E0B;&#x9762;&#x7531;&#x4EE5;&#x4E0A;&#x793A;&#x4F8B;&#x6240;&#x751F;&#x6210;&#x7684;JavaScript&#xFF08;ES6&#xFF09;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">let</span> Greeter = (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)</span>{
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Greeter</span> (<span class="hljs-params">msg</span>) </span>{
<span class="hljs-keyword">this</span>.greeting = msg;
}
Greeter.prototype.greet = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-string">`Hello, <span class="hljs-subst">${this.greeting}</span>`</span>;
}
<span class="hljs-keyword">return</span> Greeter;
})();
<span class="hljs-keyword">let</span> greeter;
greeter = <span class="hljs-keyword">new</span> Greeter(<span class="hljs-string">&quot;World&quot;</span>)!
<span class="hljs-built_in">console</span>.log(greeter.greet());
</code></pre>
<p>&#x8FD9;&#x91CC;&#x7684;<code>let Greeter</code> <strong>&#x5373;&#x5C06;</strong> &#x88AB;&#x8BE5;&#x6784;&#x9020;&#x51FD;&#x6570;&#x8D4B;&#x503C;&#xFF08;Here, <code>let Greeter</code> is going to be assigned (by) the construtor function&#xFF09;&#x3002;&#x5728;&#x8C03;&#x7528;<code>new</code>&#x5E76;&#x5141;&#x8BB8;&#x6B64;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x5C31;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x8BE5;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x3002;&#x6784;&#x9020;&#x51FD;&#x6570;&#x8FD8;&#x5305;&#x542B;&#x4E86;&#x8BE5;&#x7C7B;&#x7684;&#x6240;&#x6709;&#x9759;&#x6001;&#x6210;&#x5458;&#xFF08;<code>greet()</code>&#xFF09;&#x3002;&#x8FD8;&#x53EF;&#x4EE5;&#x628A;&#x5404;&#x4E2A;&#x7C7B;&#x60F3;&#x6210;&#x662F;&#x6709;&#x7740;&#x4E00;&#x4E2A; <em>&#x5B9E;&#x4F8B;</em> &#x7AEF;&#x4E0E; <em>&#x9759;&#x6001;</em> &#x7AEF;&#xFF08;Another way to think of each class is that there is an <em>instance</em> side and <em>static</em> side&#xFF09;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x5BF9;&#x8BE5;&#x793A;&#x4F8B;&#x7A0D;&#x52A0;&#x4FEE;&#x6539;&#xFF0C;&#x6765;&#x5C55;&#x793A;&#x8FD9;&#x79CD;&#x533A;&#x522B;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Greeter {
<span class="hljs-keyword">static</span> standardGreeting = <span class="hljs-string">&quot;Hello, there&quot;</span>;
greeting: <span class="hljs-built_in">string</span>;
greet () {
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.greeting) {
<span class="hljs-keyword">return</span> <span class="hljs-string">`Hello, <span class="hljs-subst">${this.greeting}</span>`</span>;
}
<span class="hljs-keyword">else</span> {
<span class="hljs-keyword">return</span> Greeter.standardGreeting;
}
}
}
<span class="hljs-keyword">let</span> greeter1 : Greeter;
greeter1 = <span class="hljs-keyword">new</span> Greeter();
<span class="hljs-built_in">console</span>.log (greeter1.greet());
<span class="hljs-keyword">let</span> greeterMaker: <span class="hljs-keyword">typeof</span> Greeter = Greeter;
greeterMaker.standardGreeting = <span class="hljs-string">&quot;Hey there!&quot;</span>;
<span class="hljs-keyword">let</span> greeter2: Greeter = <span class="hljs-keyword">new</span> greeterMaker();
<span class="hljs-built_in">console</span>.log(greeter2.greet());
</code></pre>
<p>&#x672C;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;<code>greeter1</code>&#x7684;&#x8FD0;&#x4F5C;&#x4E0E;&#x4E0A;&#x9762;&#x7C7B;&#x4F3C;&#x3002;&#x5BF9;<code>Greeter</code>&#x7C7B;&#x8FDB;&#x884C;&#x4E86;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x5F97;&#x5230;&#x5E76;&#x4F7F;&#x7528;&#x4E86;&#x5BF9;&#x8C61;<code>greeter1</code>&#x3002;&#x8FD9;&#x6837;&#x6240;&#x5728;&#x524D;&#x9762;&#x6709;&#x89C1;&#x8FC7;&#x3002;</p>
<p>&#x63A5;&#x4E0B;&#x6765;&#x5C31;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x4E86;&#x7C7B;<code>Greeter</code>&#x3002;&#x4E8E;&#x6B64;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;<code>greeterMaker</code>&#x7684;&#x65B0;&#x53D8;&#x91CF;&#x3002;&#x6B64;&#x53D8;&#x91CF;&#xFF08;&#x6CE8;&#xFF1A;&#x5B9E;&#x9645;&#x4E0A;&#x5BF9;&#x5E94;&#x7684;&#x5185;&#x5B58;&#x5355;&#x5143;&#xFF09;&#x5C06;&#x4FDD;&#x6709;&#x7C7B;<code>Greeter</code>&#x81EA;&#x8EAB;&#xFF0C;&#x6362;&#x79CD;&#x8BF4;&#x6CD5;&#x5C31;&#x662F;&#x7C7B;<code>Greeter</code>&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF08;&#x7C7B;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF1F;&#xFF09;&#x3002;&#x8FD9;&#x91CC;&#x4F7F;&#x7528;&#x4E86;<code>typeof Greeter</code>&#xFF0C;&#x4ECE;&#x800C;&#x8FBE;&#x5230;&#x201C;&#x7ED9;&#x6211;&#x7C7B;<code>Greeter</code>&#x672C;&#x8EAB;&#x7684;&#x7C7B;&#x578B;&#x201D;&#xFF0C;&#x800C;&#x975E;&#x7C7B;&#x793A;&#x4F8B;&#x7C7B;&#x578B;&#x7684;&#x76EE;&#x7684;&#x3002;&#x6216;&#x8005;&#x66F4;&#x51C6;&#x786E;&#x5730;&#x8BF4;&#xFF0C;&#x201C;&#x7ED9;&#x6211;&#x90A3;&#x4E2A;&#x540D;&#x53EB;<code>Greeter</code>&#x7B26;&#x53F7;&#x7684;&#x7C7B;&#x578B;&#x201D;&#xFF0C;&#x90A3;&#x5C31;&#x662F;<code>Greeter</code>&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x7C7B;&#x578B;&#x4E86;&#x3002;&#x6B64;&#x7C7B;&#x578B;&#x5C06;&#x5305;&#x542B;<code>Greeter</code>&#x7684;&#x6240;&#x6709;&#x9759;&#x6001;&#x6210;&#x5458;&#xFF0C;&#x4EE5;&#x53CA;&#x5EFA;&#x7ACB;<code>Greeter</code>&#x7C7B;&#x5B9E;&#x4F8B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x3002;&#x540E;&#x9762;&#x901A;&#x8FC7;&#x5728;<code>greeterMaker</code>&#x4E0A;&#x4F7F;&#x7528;<code>new</code>&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x521B;&#x5EFA;<code>Greeter</code>&#x7684;&#x65B0;&#x5B9E;&#x4F8B;&#xFF0C;&#x5E76;&#x5982;&#x4E4B;&#x524D;&#x90A3;&#x6837;&#x8FD0;&#x884C;&#x5B83;&#x4EEC;&#xFF0C;&#x5C31;&#x5C31;&#x8BC1;&#x5B9E;&#x4E86;&#x8FD9;&#x4E00;&#x70B9;&#x3002;</p>
<h3 id="&#x5C06;&#x7C7B;&#x7528;&#x4F5C;&#x63A5;&#x53E3;&#xFF08;using-a-class-as-an-interface&#xFF09;">&#x5C06;&#x7C7B;&#x7528;&#x4F5C;&#x63A5;&#x53E3;&#xFF08;Using a class as an interface&#xFF09;</h3>
<p>&#x6B63;&#x5982;&#x4E0A;&#x4E00;&#x5C0F;&#x8282;&#x6240;&#x8BF4;&#xFF0C;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x58F0;&#x660E;&#xFF0C;&#x521B;&#x5EFA;&#x51FA;&#x4E24;&#x4E2A;&#x4E1C;&#x897F;&#xFF1A;&#x8BE5;&#x7C7B;&#x5B9E;&#x4F8B;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4EE5;&#x53CA;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF08;a class declaration creates two things: a type representing instances of the class and a constructor function&#xFF09;&#x3002;&#x56E0;&#x4E3A;&#x7C7B;&#x521B;&#x5EFA;&#x4E86;&#x7C7B;&#x578B;&#xFF0C;&#x6240;&#x4EE5;&#x5C31;&#x53EF;&#x4EE5;&#x5728;&#x90A3;&#x4E9B;&#x53EF;&#x4F7F;&#x7528;&#x63A5;&#x53E3;&#x5730;&#x65B9;&#x4F7F;&#x7528;&#x7C7B;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Point {
x: <span class="hljs-built_in">number</span>;
y: <span class="hljs-built_in">number</span>;
}
<span class="hljs-keyword">interface</span> Point3d <span class="hljs-keyword">extends</span> Point {
z: <span class="hljs-built_in">number</span>;
}
<span class="hljs-keyword">let</span> point3d: Point3d = { x: <span class="hljs-number">1</span>, y: <span class="hljs-number">2</span>, z: <span class="hljs-number">3</span> };
</code></pre>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="02_variables_declaration.html" class="navigation navigation-prev " aria-label="Previous page: 变量声明">
<i class="fa fa-angle-left"></i>
</a>
<a href="04_interfaces.html" class="navigation navigation-next " aria-label="Next page: 接口">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"类","level":"1.4","depth":1,"next":{"title":"接口","level":"1.5","depth":1,"path":"04_interfaces.md","ref":"04_interfaces.md","articles":[]},"previous":{"title":"变量声明","level":"1.3","depth":1,"path":"02_variables_declaration.md","ref":"02_variables_declaration.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"ignoreSpecialCharacters":false,"maxIndexSize":1000000},"sharing":{"weibo":false,"all":["facebook","google","twitter","weibo","instapaper"],"google":false,"twitter":true,"vk":false,"instapaper":false,"facebook":true},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css","pdf":"styles/pdf.css","epub":"styles/epub.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css","pdf":"styles/pdf.css","epub":"styles/epub.css"}},"file":{"path":"03_classes.md","mtime":"2019-04-03T06:43:16.308Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-04-04T00:04:23.982Z"},"basePath":".","book":{"language":""}});
});
</script>
</div>
<script src="gitbook/gitbook.js"></script>
<script src="gitbook/theme.js"></script>
<script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
<script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="gitbook/gitbook-plugin-search/search.js"></script>
<script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>