객체 지향 프로그래밍
OOP
이해
- 다형성의 일부 특징(전부는 아님) 추상화: 객체는 데이터와 행동을 포함합니다(Objects Contain Data and Behavior). 캡슐화: 구현 세부 사항을 숨기는 캡슐화(Encapsulation that Hides Implementation Details). 상속: 형식 시스템이자 코드 공유 방식으로서의 상속(Inheritance as a Type System and as Code Sharing).
- trait object
Box
는 trait object입니다. Rust가 런타임에 이를 해석하므로 일정한 성능 비용이 발생합니다. 가능한 한 사용하지 마세요. pub trait Draw { fn draw(&self); } // trait object pub struct Screen { pub components: Vec<Box<dyn Draw>>, } impl Screen { pub fn run(&self) { for component in self.components.iter() { component.draw(); } } } // generic type + trait buond pub struct Screen<T: Draw> { pub components: Vec<T>, } impl<T> Screen<T> where T: Draw, { pub fn run(&self) { for component in self.components.iter() { component.draw(); } } }