x-i18n: generated_at: “2026-03-01T14:49:32Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: 422a0ab9c4e755eb30c3b057632d51cd76ee3b4983239bc215ae65e9a2d850c7 source_path: ch18-00-oop.md workflow: 16
面向对象编程特性 (Object-Oriented Programming Features)
Object-Oriented Programming Features
面向对象编程 (Object-oriented programming, OOP) 是一种为程序建模的方式。对象作为编程概念最早在 20 世纪 60 年代的 Simula 编程语言中引入。这些对象影响了艾伦·凯 (Alan Kay) 的编程架构,即对象之间相互传递消息。为了描述这种架构,他在 1967 年创造了“面向对象编程”一词。许多竞争性的定义描述了 OOP 是什么,根据其中一些定义,Rust 是面向对象的,但根据另一些定义,它则不是。在本章中,我们将探讨一些通常被认为是面向对象的特征,以及这些特征如何转化为地道的 Rust。然后,我们将向你展示如何在 Rust 中实现面向对象的设计模式,并讨论这样做与利用 Rust 的一些优势来实现解决方案之间的权衡。
Object-oriented programming (OOP) is a way of modeling programs. Objects as a programmatic concept were introduced in the programming language Simula in the 1960s. Those objects influenced Alan Kay’s programming architecture in which objects pass messages to each other. To describe this architecture, he coined the term object-oriented programming in 1967. Many competing definitions describe what OOP is, and by some of these definitions Rust is object oriented but by others it is not. In this chapter, we’ll explore certain characteristics that are commonly considered object oriented and how those characteristics translate to idiomatic Rust. We’ll then show you how to implement an object-oriented design pattern in Rust and discuss the trade-offs of doing so versus implementing a solution using some of Rust’s strengths instead.