Study Rust Bible analysis - Rust learn-14 (object-oriented)

Rust object-oriented

Under some definitions, Rust is object-oriented; under other definitions, Rust is not

Objects contain data and behavior

Object-oriented programs are made up of objects. An object contains data and procedures for manipulating that data. These procedures are often referred to as methods or operations.
Under this definition, Rust is object-oriented

  • structure
  • enumerate

Both data and behavior can be defined

encapsulation

Object-oriented pays attention to encapsulation. The caller does not need to know how to implement it, but only needs to know how to use it. Rust's package management, module management, pub keyword, function fn, and trait are in line with the idea of ​​encapsulation

inherit

Inheritance is a mechanism provided by many programming languages. An object can be defined to inherit the elements in another object definition, which allows it to obtain the data and behavior of the parent object without redefinition.
From this point of view, Rust is not object-oriented, because Rust has no inheritance, only implementation (trait)

polymorphism

Polymorphism is an important feature of object-oriented, which means that the same type of thing has multiple forms at the same time, that is, the same type of thing, at different times, represents different objects, which refers to multiple forms of objects.
From this point of view, Rust is also object-oriented. By implementing traits, structures or enumerations are polymorphic.

implement object-oriented

A simple login case, the user enters the username and password for verification
insert image description here

Write the outermost logic

We need username and password, build a User object, use the new method, and verify it through the login method in userService

use crate::user::User;

mod userService;
mod user;

fn main() {
    
    
    //服务逻辑
    let username = "testuser1".to_string();
    let password = "testpassword".to_string();
    let user = User::new(username, password);
    match userService::login(user) {
    
    
        Ok(user) => println!("{:#?}", user),
        Err(e) => println!("{:?}", e)
    }

}

userService

use crate::user::{
    
    User};

const CHECK_USERNAME: &str = "testuser";
const CHECK_PASSWORD: &str = "testpassword";

pub fn login(user: User) -> Result<User, Box<&'static str>> {
    
    
    let username = user.get_username();
    let password = user.get_password();
    return if username == CHECK_USERNAME {
    
    
        if password == CHECK_PASSWORD {
    
    
            Ok(User::new(username.to_string(), password.to_string()))
        } else {
    
    
            Err(Box::new("password error"))
        }
    } else {
    
    
        Err(Box::new("username error"))
    };
}

User

#[derive(Debug,Clone)]
pub struct User {
    
    
    username: String,
    password: String,
}

impl User {
    
    
    pub fn new(username: String, password: String) -> Self {
    
    
        User {
    
    
            username,
            password,
        }
    }
    pub fn newNoArgs() -> Self {
    
    
        User {
    
    
            username: String::new(),
            password: String::new(),
        }
    }
    pub fn get_username(&self) -> &str {
    
    
        &self.username
    }
    pub fn get_password(&self) -> &str {
    
    
        &self.password
    }
}

Guess you like

Origin blog.csdn.net/qq_51553982/article/details/130387437