面向对象编程特性
Object-Oriented Programming Features
面向对象编程(OOP)是一种对程序进行建模的方法。对象(objects)作为一种编程概念最早于 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.