mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2024-11-14 04:41:49 +08:00
166 lines
15 KiB
HTML
166 lines
15 KiB
HTML
|
<!DOCTYPE HTML>
|
|||
|
<html lang="en">
|
|||
|
<head>
|
|||
|
<meta charset="UTF-8">
|
|||
|
<title>什么是面向对象 - Rust 程序设计语言 简体中文版</title>
|
|||
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|||
|
<meta name="description" content="Rust 程序设计语言 简体中文版">
|
|||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|||
|
|
|||
|
<base href="">
|
|||
|
|
|||
|
<link rel="stylesheet" href="book.css">
|
|||
|
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
|
|||
|
|
|||
|
<link rel="shortcut icon" href="favicon.png">
|
|||
|
|
|||
|
<!-- Font Awesome -->
|
|||
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
|
|||
|
|
|||
|
<link rel="stylesheet" href="highlight.css">
|
|||
|
<link rel="stylesheet" href="tomorrow-night.css">
|
|||
|
|
|||
|
<!-- MathJax -->
|
|||
|
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
|||
|
|
|||
|
<!-- Fetch JQuery from CDN but have a local fallback -->
|
|||
|
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
|
|||
|
<script>
|
|||
|
if (typeof jQuery == 'undefined') {
|
|||
|
document.write(unescape("%3Cscript src='jquery.js'%3E%3C/script%3E"));
|
|||
|
}
|
|||
|
</script>
|
|||
|
</head>
|
|||
|
<body class="light">
|
|||
|
<!-- Set the theme before any content is loaded, prevents flash -->
|
|||
|
<script type="text/javascript">
|
|||
|
var theme = localStorage.getItem('theme');
|
|||
|
if (theme == null) { theme = 'light'; }
|
|||
|
$('body').removeClass().addClass(theme);
|
|||
|
</script>
|
|||
|
|
|||
|
<!-- Hide / unhide sidebar before it is displayed -->
|
|||
|
<script type="text/javascript">
|
|||
|
var sidebar = localStorage.getItem('sidebar');
|
|||
|
if (sidebar === "hidden") { $("html").addClass("sidebar-hidden") }
|
|||
|
else if (sidebar === "visible") { $("html").addClass("sidebar-visible") }
|
|||
|
</script>
|
|||
|
|
|||
|
<div id="sidebar" class="sidebar">
|
|||
|
<ul class="chapter"><li><a href="ch01-00-introduction.html"><strong>1.</strong> 介绍</a></li><li><ul class="section"><li><a href="ch01-01-installation.html"><strong>1.1.</strong> 安装</a></li><li><a href="ch01-02-hello-world.html"><strong>1.2.</strong> Hello, World!</a></li></ul></li><li><a href="ch02-00-guessing-game-tutorial.html"><strong>2.</strong> 猜猜看教程</a></li><li><a href="ch03-00-common-programming-concepts.html"><strong>3.</strong> 通用编程概念</a></li><li><ul class="section"><li><a href="ch03-01-variables-and-mutability.html"><strong>3.1.</strong> 变量和可变性</a></li><li><a href="ch03-02-data-types.html"><strong>3.2.</strong> 数据类型</a></li><li><a href="ch03-03-how-functions-work.html"><strong>3.3.</strong> 函数如何工作</a></li><li><a href="ch03-04-comments.html"><strong>3.4.</strong> 注释</a></li><li><a href="ch03-05-control-flow.html"><strong>3.5.</strong> 控制流</a></li></ul></li><li><a href="ch04-00-understanding-ownership.html"><strong>4.</strong> 认识所有权</a></li><li><ul class="section"><li><a href="ch04-01-what-is-ownership.html"><strong>4.1.</strong> 什么是所有权</a></li><li><a href="ch04-02-references-and-borrowing.html"><strong>4.2.</strong> 引用 & 借用</a></li><li><a href="ch04-03-slices.html"><strong>4.3.</strong> Slices</a></li></ul></li><li><a href="ch05-00-structs.html"><strong>5.</strong> 结构体</a></li><li><ul class="section"><li><a href="ch05-01-method-syntax.html"><strong>5.1.</strong> 方法语法</a></li></ul></li><li><a href="ch06-00-enums.html"><strong>6.</strong> 枚举和模式匹配</a></li><li><ul class="section"><li><a href="ch06-01-defining-an-enum.html"><strong>6.1.</strong> 定义枚举</a></li><li><a href="ch06-02-match.html"><strong>6.2.</strong> <code>match</code>控制流运算符</a></li><li><a href="ch06-03-if-let.html"><strong>6.3.</strong> <code>if let</code>简单控制流</a></li></ul></li><li><a href="ch07-00-modules.html"><strong>7.</strong> 模块</a></li><li><ul class="section"><li><a href="ch07-01-mod-and-the-filesystem.html"><strong>7.1.</strong> <code>mod</code>和文件系统</a></li><li><a href="ch07-02-controlling-visibility-with-pub.html"><strong>7.2.</strong> 使用<code>pub</code>控制可见性</a></li><li><a href="ch07-03-importing-names-with-use.html"><strong>7.3.</strong> 使用<code>use</code>导入命名</a></li></ul></li><li><a href="ch08-00-common-collections.html"><strong>8.</strong> 通用集合类型</a></li><li><ul class="section"><li><a href="ch08-01-vectors.html"><strong>8.1.</strong> vector</a></li><li><a href="ch08-02-strings.html"><strong>8.2.</strong> 字符串</a></li><li><a href="ch08-03-hash-maps.html"><strong>8.3.</strong> 哈希 map</a></li></ul></li><li><a href="ch09-00-error-handling.html"><strong>9.</strong> 错误处理</a></li><li><ul class="section"><li><a href="ch09-01-unrecoverable-errors-with-panic.html"><strong>9.1.</strong> <code>panic!</code>与不可恢复的错误</a></li><li><a href="ch09-02-recoverable-errors-with-result.html"><strong>9.2.</strong> <code>Result</code>与可恢复的错误</a></li><li><a href="ch09-03-to-panic-or-not-to-panic.html"><strong>9.3.</strong> <code>panic!</code>还是不<code>panic!</code></a></li></ul></li><li><a href="ch10-00-generics.html"><strong>10.</strong> 泛型、trait 和生命周期</a></li><li><ul class="section"><li><a href="ch10-01-syntax.html"><strong>10.1.</strong> 泛型数据类型</a></li><li><a href="ch10-02-traits.html"><strong>10.2.</strong> trait:定义共享的行为</a></li><li><a href="ch10-03-lifetime-syntax.html"><strong>10.3.</strong> 生命周期与引用有效性</a></li></ul></li><li><a href="ch11-00-testing.html"><strong>11.</strong> 测试</a></li><li><ul class="section"><li><a href="ch11-01-writing-tests.html"><strong>11.1.</strong> 编写测试</a></li><li><a href="ch11-02-running-tests.html"><strong>11.2.</strong> 运行测试</a></li><li><a href="ch11-03-test-organization.html"><strong>11.3.</strong> 测试的组织结构</a></li></ul></li><li><a href="ch12-00-an-io-project.h
|
|||
|
</div>
|
|||
|
|
|||
|
<div id="page-wrapper" class="page-wrapper">
|
|||
|
|
|||
|
<div class="page">
|
|||
|
<div id="menu-bar" class="menu-bar">
|
|||
|
<div class="left-buttons">
|
|||
|
<i id="sidebar-toggle" class="fa fa-bars"></i>
|
|||
|
<i id="theme-toggle" class="fa fa-paint-brush"></i>
|
|||
|
</div>
|
|||
|
|
|||
|
<h1 class="menu-title">Rust 程序设计语言 简体中文版</h1>
|
|||
|
|
|||
|
<div class="right-buttons">
|
|||
|
<i id="print-button" class="fa fa-print" title="Print this book"></i>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
<div id="content" class="content">
|
|||
|
<a class="header" href="#什么是面向对象" name="什么是面向对象"><h2>什么是面向对象?</h2></a>
|
|||
|
<blockquote>
|
|||
|
<p><a href="https://github.com/rust-lang/book/blob/master/second-edition/src/ch17-01-what-is-oo.md">ch17-01-what-is-oo.md</a>
|
|||
|
<br>
|
|||
|
commit 46334522e22d6217b392451cff8b4feca2d69d79</p>
|
|||
|
</blockquote>
|
|||
|
<p>关于一门语言是否需要是面向对象,在编程社区内并达成一致意见。Rust被很多不同的编程模式影响,我们探索了13章提到的函数式编程的特性。面向对象编程语言的一些特性往往是对象、封装和继承。我们看一下每个的含义和Rust是否支持它们。</p>
|
|||
|
<a class="header" href="#对象包含数据和行为" name="对象包含数据和行为"><h2>对象包含数据和行为</h2></a>
|
|||
|
<p><code>Design Patterns: Elements of Reusable Object-Oriented Software</code>这本书被俗称为<code>The Gang of Four book</code>,是面向对象编程模式的目录。它这样定义面向对象编程:</p>
|
|||
|
<blockquote>
|
|||
|
<p>面向对象的程序是由对象组成的。一个对象包数据和操作这些数据的程序。程序通常被称为方法或操作。</p>
|
|||
|
</blockquote>
|
|||
|
<p>在这个定一下,Rust是面向对象的:结构体和枚举包含数据和impl块提供了在结构体和枚举上的方法。虽然带有方法的结构体和枚举不称为对象,但是他们提供了和对象相同的功能,使用了<code>Gang of Four</code>定义的对象。</p>
|
|||
|
<a class="header" href="#隐藏了实现细节的封装" name="隐藏了实现细节的封装"><h2>隐藏了实现细节的封装</h2></a>
|
|||
|
<p>通常与面向对象编程相关的另一个方面是封装的思想:对象的实现细节不能被使用对象的代码获取到。唯一与对象交互的方式是通过对象提供的public API,使用对象的代码无法深入到对象内部并直接改变数据或者行为。封装使得改变和重构对象的内部,无需改变使用对象的代码。</p>
|
|||
|
<p>就像我们在第7张讨论的那样,我们可以使用pub关键字来决定模块、类型函数和方法是public的(默认情况下一切都是private)。比如,我们可以定义一个结构体<code>AveragedCollection</code>包含一个<code>i32</code>类型的vector。结构体也可以有一个字段,该字段保存了vector中所有值的平均值。这样,希望知道结构体中的vector的平均值的人可以随着获取到,而无需自己计算。<code>AveragedCollection</code> 会为我们缓存平均值结果。 Listing 17-1有<code>AveragedCollection</code> 结构体的定义。</p>
|
|||
|
<p>Filename: src/lib.rs</p>
|
|||
|
<pre><code>pub struct AveragedCollection {
|
|||
|
list: Vec<i32>,
|
|||
|
average: f64,
|
|||
|
}
|
|||
|
</code></pre>
|
|||
|
<p><code>AveragedCollection</code>结构体维护了一个Integer列表和集合中所有元素的平均值。</p>
|
|||
|
<p>注意,结构体本身被标记为pub,这样其他代码可以使用这个结构体,但是在结构体内部的字段仍然是private。这是非常重要的,因为我们希望保证变量被增加到列表或者被从列表删除时,也会同时更新平均值。我们通过在结构体上实现add、remove和average方法来做到这一点( Listing 17-2:):</p>
|
|||
|
<p>Filename: src/lib.rs</p>
|
|||
|
<pre><code># pub struct AveragedCollection {
|
|||
|
# list: Vec<i32>,
|
|||
|
# average: f64,
|
|||
|
# }
|
|||
|
impl AveragedCollection {
|
|||
|
pub fn add(&mut self, value: i32) {
|
|||
|
self.list.push(value);
|
|||
|
self.update_average();
|
|||
|
}
|
|||
|
|
|||
|
pub fn remove(&mut self) -> Option<i32> {
|
|||
|
let result = self.list.pop();
|
|||
|
match result {
|
|||
|
Some(value) => {
|
|||
|
self.update_average();
|
|||
|
Some(value)
|
|||
|
},
|
|||
|
None => None,
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
pub fn average(&self) -> f64 {
|
|||
|
self.average
|
|||
|
}
|
|||
|
|
|||
|
fn update_average(&mut self) {
|
|||
|
let total: i32 = self.list.iter().sum();
|
|||
|
self.average = total as f64 / self.list.len() as f64;
|
|||
|
}
|
|||
|
}
|
|||
|
</code></pre>
|
|||
|
<p>Listing 17-2:在<code>AveragedCollection</code>结构体上实现了add、remove和average public方法</p>
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
<!-- Mobile navigation buttons -->
|
|||
|
|
|||
|
<a href="ch17-00-oop.html" class="mobile-nav-chapters previous">
|
|||
|
<i class="fa fa-angle-left"></i>
|
|||
|
</a>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<a href="ch17-00-oop.html" class="nav-chapters previous" title="You can navigate through the chapters using the arrow keys">
|
|||
|
<i class="fa fa-angle-left"></i>
|
|||
|
</a>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<!-- Local fallback for Font Awesome -->
|
|||
|
<script>
|
|||
|
if ($(".fa").css("font-family") !== "FontAwesome") {
|
|||
|
$('<link rel="stylesheet" type="text/css" href="_FontAwesome/css/font-awesome.css">').prependTo('head');
|
|||
|
}
|
|||
|
</script>
|
|||
|
|
|||
|
<!-- Livereload script (if served using the cli tool) -->
|
|||
|
|
|||
|
|
|||
|
<script src="highlight.js"></script>
|
|||
|
<script src="book.js"></script>
|
|||
|
</body>
|
|||
|
</html>
|