Rust 编程视频教程(进阶)——020_2 封装

视频地址

头条地址:https://www.ixigua.com/i6775861706447913485
B站地址:https://www.bilibili.com/video/av81202308/

源码地址

github地址:https://github.com/anonymousGiga/learn_rust

讲解内容

1、封装
(1)封装的思想:对象的实现细节不能被使用对象的代码获取到。
(2)在Rust中,使用pub关键字来标记模块、类型、函数和方法是公有的,默认情况下一切都是私有的。

例子:
(1) mkdir learn_oo
(2) cd learn_oo
(3) 编辑Cargo.toml:

[workspace]
members = [
        "getaver",
        "main",
]

(4) cargo new getaver --lib
(5) 编辑 getaver/src/lib.rs,代码如下:

pub struct AverCollect {
    list: Vec,
    aver: f64,
}

impl AverCollect {
    pub fn new() -> AverCollect {
        AverCollect{
            list: vec![],
            aver: 0.0,
        }
    }

    pub fn add(&mut self, value: i32) {
        self.list.push(value);
        self.update_average();
    }

    pub fn remove(&mut self) -> Option {
        let result = self.list.pop();
        match result {
            Some(value) => {
                self.update_average();
                Some(value)
            },
            None => None,
        }
    }

    pub fn average(&self) -> f64 {
        self.aver
    }

    fn update_average(&mut self) {
        let total: i32 = self.list.iter().sum();
        self.aver = total as f64 / self.list.len() as f64;
    }
}

(6) cargo new main
(7) 编辑main/Cargo.toml,添加:

[dependencies]
getaver = {path = "../getaver"}

(8) 编辑main/src/main.rs,代码如下:

use getaver::AverCollect;
fn main() {
   let mut a = getaver::AverCollect::new();
    a.add(1);
    println!("average value = {}", a.average());

    a.add(2);
    println!("average value = {}", a.average());

    a.add(3);
    println!("average value = {}", a.average());

    a.remove();
    println!("average value = {}", a.average());
    println!("Hello, world!");
}

2、继承
Rust不支持继承。但是Rust可以通过trait进行行为共享。trait的知识点此处不进行重复。

你可能感兴趣的:(Rust 编程视频教程(进阶)——020_2 封装)