cutego/docs/创建.md

37 lines
669 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

```go
type Car struct {
color string
size string
}
```
方式一使用T{…}方式,结果为值类型
```go
c := Car{}
```
方式二使用new的方式结果为指针类型
```go
c1 := new(Car)
```
方式三:使用&方式,结果为指针类型
```go
c2 := &Car{}
```
以下为创建并初始化
```go
c3 := &Car{"红色", "1.2L"}
c4 := &Car{color: "红色"}
c5 := Car{color: "红色"}
```
构造函数:
在Go语言中没有构造函数的概念对象的创建通常交由一个全局的创建函数来完成
NewXXX 来命名,表示“构造函数”
```go
func NewCar(color,size string) *Car {
return &Car{color,size}
}
```