Ad
Object-oriented Programming
Mathematics
Code
Diff
  • use std::f64::consts::PI;
    
    pub trait Shape {
        type Change;
        
        fn change_color(&mut self, color: impl Into<String>) -> Self::Change;
        
        fn color(&self) -> &str;
        
        fn location(&self) -> (i32, i32);
        
        fn thickness(&self) -> u32;
    }
    
    pub struct GenericShape {
        color: String,
        location: (i32, i32),
        thickness: u32,
    }
    
    impl GenericShape {
        pub fn new(color: impl Into<String>, thickness: u32, location: (i32, i32)) -> Self {
            Self {
                color: color.into(),
                location,
                thickness,
            }
        }
    }
    
    impl Shape for GenericShape {
        type Change = ();
        
        fn change_color(&mut self, color: impl Into<String>) -> Self::Change {
            self.color = color.into();
        }
        
        fn color(&self) -> &str {
            &self.color
        }
        
        fn location(&self) -> (i32, i32) {
            self.location
        }
        
        fn thickness(&self) -> u32 {
            self.thickness
        }
    }
    
    pub struct Circle {
        radius: f64,
        shape: GenericShape
    }
    
    impl Circle {
        pub fn new(radius: f64, color: impl Into<String>, thickness: u32, location: (i32, i32)) -> Self {
            Self {
                radius,
                shape: GenericShape::new(color, thickness, location),
            }
        }
        
        pub fn area(&self) -> f64 {
            PI * self.radius.powi(2)
        }
    }
    
    impl Shape for Circle {
        type Change = String;
    
        fn change_color(&mut self, color: impl Into<String>) -> Self::Change {
            let color = color.into();
            let message = format!("The color has changed from {} to {}", self.color(), color);
            self.shape.change_color(color);
            message
        }
        
        fn color(&self) -> &str {
            self.shape.color()
        }
        
        fn location(&self) -> (i32, i32) {
            self.shape.location()
        }
        
        fn thickness(&self) -> u32 {
            self.shape.thickness()
        }
    }
    • use std::f64::consts::PI;
    • pub struct Shape {
    • pub trait Shape {
    • type Change;
    • fn change_color(&mut self, color: impl Into<String>) -> Self::Change;
    • fn color(&self) -> &str;
    • fn location(&self) -> (i32, i32);
    • fn thickness(&self) -> u32;
    • }
    • pub struct GenericShape {
    • color: String,
    • location: (i32, i32),
    • thickness: u32,
    • location: (i32, i32)
    • }
    • impl Shape {
    • pub fn new(color: String, thickness: u32, location: (i32, i32)) -> Self {
    • impl GenericShape {
    • pub fn new(color: impl Into<String>, thickness: u32, location: (i32, i32)) -> Self {
    • Self {
    • color,
    • thickness,
    • color: color.into(),
    • location,
    • thickness,
    • }
    • }
    • }
    • impl Shape for GenericShape {
    • type Change = ();
    • fn change_color(&mut self, color: impl Into<String>) -> Self::Change {
    • self.color = color.into();
    • }
    • fn color(&self) -> &str {
    • &self.color
    • }
    • fn location(&self) -> (i32, i32) {
    • self.location
    • }
    • pub fn change_color(&mut self, color: String) {
    • self.color = color;
    • fn thickness(&self) -> u32 {
    • self.thickness
    • }
    • }
    • pub struct Circle {
    • radius: f64,
    • color: String,
    • thickness: u32,
    • location: (i32, i32)
    • shape: GenericShape
    • }
    • impl Circle {
    • pub fn new(radius: f64, color: String, thickness: u32, location: (i32, i32)) -> Self {
    • pub fn new(radius: f64, color: impl Into<String>, thickness: u32, location: (i32, i32)) -> Self {
    • Self {
    • radius,
    • color,
    • thickness,
    • location
    • shape: GenericShape::new(color, thickness, location),
    • }
    • }
    • pub fn change_color(&mut self, color: String) -> String {
    • let message = format!("The color has changed from {} to {}", self.color, color);
    • self.color = color;
    • pub fn area(&self) -> f64 {
    • PI * self.radius.powi(2)
    • }
    • }
    • impl Shape for Circle {
    • type Change = String;
    • fn change_color(&mut self, color: impl Into<String>) -> Self::Change {
    • let color = color.into();
    • let message = format!("The color has changed from {} to {}", self.color(), color);
    • self.shape.change_color(color);
    • message
    • }
    • pub fn area(&self) -> f64 {
    • PI * self.radius.powi(2)
    • fn color(&self) -> &str {
    • self.shape.color()
    • }
    • fn location(&self) -> (i32, i32) {
    • self.shape.location()
    • }
    • fn thickness(&self) -> u32 {
    • self.shape.thickness()
    • }
    • }