Object-oriented Programming
Mathematics
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()
- }
- }
#[cfg(test)] mod tests { use super::*; #[test] fn test() { let mut c = Circle::new(113.776, "black", 7, (7, 6)); assert_eq!(c.color(), "black"); assert_eq!(c.thickness(), 7); assert_eq!(c.location(), (7, 6)); let mut s = GenericShape::new("white", 1, (0, 0)); assert_eq!(s.change_color("red"), ()); assert_eq!(c.change_color("blue"), "The color has changed from black to blue"); } }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn test() {
let mut c = Circle::new(113.776, "black".into(), 7, (7, 6));assert_eq!(c.color, "black");assert_eq!(c.thickness, 7);assert_eq!(c.location, (7, 6));- let mut c = Circle::new(113.776, "black", 7, (7, 6));
- assert_eq!(c.color(), "black");
- assert_eq!(c.thickness(), 7);
- assert_eq!(c.location(), (7, 6));
let mut s = Shape::new("white".into(), 1, (0, 0));assert_eq!(s.change_color("red".into()), ());assert_eq!(c.change_color("blue".into()), "The color has changed from black to blue");- let mut s = GenericShape::new("white", 1, (0, 0));
- assert_eq!(s.change_color("red"), ());
- assert_eq!(c.change_color("blue"), "The color has changed from black to blue");
- }
- }