第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

相同問題go語言與php的實現(xiàn)對比

標簽:
PHP

一、面向对象


php:

class Rectangle{    private $width;    private $height;    private $color;    public function __construct($width, $height, $color)    {        $this->width  = $width;        $this->height = $height;        $this->color  = $color;    }    public function setColor($color)    {        $this->color = $color;    }    public function getColor()    {        return $this->color;    }    public function area()    {        return $this->width * $this->height;    }}$r1 = new Rectangle(12, 2, "白色");$r2 = new Rectangle(9, 4, "蓝色");echo "Area of r1 is ". $r1->area()."\n";echo "Area of r2 is ". $r2->area()."\n";echo "Color of r2 is ". $r2->getColor()."\n";echo "set new color\n";$r2->setColor("绿色");echo "Color of r2 is ". $r2->getColor()."\n";


go:

package mainimport "fmt"type Rectangle struct {	width, height float64	color         string}// 如果声明接收者为指定,当使用T类型来调用时,go会自动转换为*T,太TM聪明了func (r *Rectangle) SetColor(color string) {	r.color = color}func (r Rectangle) area() float64 {	return r.width * r.height}func main() {	r1 := Rectangle{12, 2, "白色"}	r2 := Rectangle{9, 4, "蓝色"}	fmt.Println("Area of r1 is: ", r1.area())	fmt.Println("Area of r2 is: ", r2.area())	fmt.Println("Color of r2 is: ", r2.color)	fmt.Println("set new color")	r2.SetColor("绿色") // 等价于(&r2).SetColor("绿色"),这里r2不需要传地址,当然,传地址也不错,只是go会自动帮助转换,没有必要	fmt.Println("Color of r2 is: ", r2.color)}


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消