package ArticleModel import "fmt" // ArticleModel ArticleModel结构体 type ArticleModel struct { aid int64 // 文章ID号 title string // 文章标题 click int64 // 点击次数 } // New ArticleModel构造函数 func New(aid int64, title string, click int64) (articleModel *ArticleModel) { articleModel = new(ArticleModel) (*articleModel).aid = aid (*articleModel).title = title (*articleModel).click = click return } // Print ArticleModel的Print()方法 func (articleModel *ArticleModel) Print() { aid := (*articleModel).aid title := (*articleModel).title click := (*articleModel).click fmt.Printf("第%d篇 => %s [点击数:%d]\n", aid, title, click) }
package main import ArticleModel "go-src/demo" func main() { // 调用构造函数获取ArticleModel对象 articleModel := ArticleModel.New(9527, "PHP是世界上最好の语言", 1024) // 调用ArticleModel对象的Print()方法 articleModel.Print() // 第9527篇 => PHP是世界上最好の语言 [点击数:1024] }
package ArticleModel import "fmt" // ArticleModel ArticleModel结构体 type ArticleModel struct { aid int64 // 文章ID号 title string // 文章标题 click int64 // 点击次数 } // NewArticleModel ArticleModel构造函数 func NewArticleModel(aid int64, title string, click int64) (articleModel *ArticleModel) { articleModel = new(ArticleModel) (*articleModel).aid = aid (*articleModel).title = title (*articleModel).click = click return } // Print ArticleModel的Print()方法 func (articleModel *ArticleModel) Print() { aid := (*articleModel).aid title := (*articleModel).title click := (*articleModel).click fmt.Printf("第%d篇 => %s [点击数:%d]\n", aid, title, click) }
package main import ArticleModel "go-src/demo" func main() { // 调用构造函数获取ArticleModel对象 articleModel := ArticleModel.NewArticleModel(9527, "PHP是世界上最好の语言", 1024) // 调用ArticleModel对象的Print()方法 articleModel.Print() }
package ArticleModel import "fmt" // ArticleModel ArticleModel结构体 type ArticleModel struct { aid int64 // 文章ID号 title string // 文章标题 click int64 // 点击次数 } // New ArticleModel构造函数 func New(funcSlice ...func(articleModel *ArticleModel)) (articleModel *ArticleModel) { articleModel = new(ArticleModel) for _, function := range funcSlice { function(articleModel) } return articleModel } // WithAid 设置aid字段 func WithAid(aid int64) func(articleModel *ArticleModel) { return func(articleModel *ArticleModel) { (*articleModel).aid = aid } } // WithTitle 设置title字段 func WithTitle(title string) func(articleModel *ArticleModel) { return func(articleModel *ArticleModel) { (*articleModel).title = title } } // WithClick 设置click字段 func WithClick(click int64) func(articleModel *ArticleModel) { return func(articleModel *ArticleModel) { (*articleModel).click = click } } // Print ArticleModel的Print()方法 func (articleModel *ArticleModel) Print() { aid := (*articleModel).aid title := (*articleModel).title click := (*articleModel).click fmt.Printf("第%d篇 => %s [点击数:%d]\n", aid, title, click) }
package main import "go-src/model/ArticleModel" func main() { // 创建ArticleModel对象(1个参数) articleModel1 := ArticleModel.New( ArticleModel.WithAid(9527), ) articleModel1.Print() // 第9527篇 => [点击数:0] // 创建ArticleModel对象(2个参数) articleModel2 := ArticleModel.New( ArticleModel.WithAid(9527), ArticleModel.WithTitle("PHP是世界上最好の语言"), ) articleModel2.Print() // 第9527篇 => PHP是世界上最好の语言 [点击数:0] // 创建ArticleModel对象(3个参数) articleModel3 := ArticleModel.New( ArticleModel.WithAid(9527), ArticleModel.WithTitle("PHP是世界上最好の语言"), ArticleModel.WithClick(1024), ) articleModel3.Print() // 第9527篇 => PHP是世界上最好の语言 [点击数:1024] }
Copyright © 2024 码农人生. All Rights Reserved