rust way step 6



pub struct AA {
    field: i32,
    a:i32 
}

impl AA {

    fn default() -> AA {
        AA {field:0, a:0}
    }
    pub fn new(value: i32,a1:i32) -> AA {
        AA {
            field: value,
            a:a1
        }
    }

    pub fn test1(&self) {
        println!("from 8888888   value={}",self.field);
        self.test();
    }

    fn test(&self) {
        println!("from 44444444");
    }

    pub  fn getA(&self)->i32{
            return self.a;
    }
    
    pub  fn setA(&mut self,t:i32){
         self.a=t;
    }
}

/*...................................................... */
trait Shape {
    fn area(&self) -> f64;
}


struct Circle {
    radius: f64,
}

impl Shape for Circle {
    fn area(&self) -> f64 {
       return  3.14159 * self.radius * self.radius
    }
}

impl  Circle {
    pub  fn test1(&self) -> f64 {
        return   self.radius ;
    }
}


struct Rectangle {
    width: f64,
    height: f64,
}

impl Shape for Rectangle {
    fn area(&self) -> f64 {
        self.width * self.height
    }
}

// 使用泛型函数展示多态
fn print_area<T: Shape>(shape: &T) {
    println!("Area: {}", shape.area());
}

 
fn main() {

    let mut a = AA::default();
   // let mut a = AA::new(777,44);
    a.test1();
    a.test();
    a.setA(888);
    print!("A={}\n",a.getA());

    let circle: Circle = Circle { radius: 2.0 };
    let rectangle = Rectangle { width: 3.0, height: 4.0 };

    print_area(&circle);
    print_area(&rectangle);
    println!("{}",circle.test1());

    /**************************************************** */

    fn add(a :i32,b:i32)->i32{

        return a+b;
    }

    //let add = |a, b| a + b;
    let add1 = |a, b| a + b;
    println!("{}  {}", add(2, 3),add1(1,1)); 
   

   let t=4;
   let sp=|x|x+t;
   println!("{}",sp(100));
   

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值