new一个新的对象

package main

import "fmt"

type rec struct {
    width,height int
}

func (r *rec) area() int {
    return r.width * r.height
}

func main() {
    r := rec{width:100,height:100}
    fmt.Println("area:",r.area())
    newR := new(rec) // 相当于 &r
    newR.width=1
    newR.height=2
    fmt.Println("area1",newR.area())
}
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!