首页 > 其他分享 >如何写出漂亮的构造函数 option funciton

如何写出漂亮的构造函数 option funciton

时间:2023-03-03 15:46:24浏览次数:35  
标签:ISBN string Title Book func funciton 构造函数 option

1. Golang里面没有构造函数,但是Golang却可以像C++一样实现类似继承、构造函数一样等面向对象编程的思想和方法

Golang里面要实现相关的构造函数定义可以通过通过new来创建构造函数,

通过new一个对象,或者利用Golang本身的&方式来生成一个对象并返回一个对象指针

package main

type Book struct {
    Title string
    ISBN  string
}

func NewBook(t string) *Book {
    return &Book{
        Title: t,
        ISBN:  "",
    }
}

func NewBook2(t string, i string) *Book {
    return &Book{
        Title: t,
        ISBN:  i,
    }
}

func main() {
    _ = NewBook("早点下班的go语言学习法")
    _ = NewBook2("早点下班的go语言学习法", "20230101")
}

// How To Make Constructor Better
View Code

 

2.上面这种有重复,显然不优雅。可以利用配置参数的形式添加

package main

type Book__ struct {
    cfg *Config
}
type Config struct {
    Title string
    ISBN  string
}

func NewBook__(cfg *Config) *Book__ {
    return &Book__{cfg}
}

func main__() {
    _ = NewBook__(&Config{
        Title: "早点下班的go语言学习法",
        ISBN:  "20230101",
    })
}
View Code

3.上面违反了开闭原则,字段部分修改,客户端报错,并也要修改

package main

type Book___ struct {
    Title string
    Code  string
}
type Option func(*Book___)

func NewBook_(options ...Option) *Book___ {
    b := &Book___{}
    for _, option := range options {
        option(b)
    }
    return b
}

func WithTitle(title string) Option {
    return func(b *Book___) {
        b.Title = title
    }
}

func WithISBN(ISBN string) Option {
    return func(b *Book___) {
        b.Code = ISBN
    }
}

func main_() {
    _ = NewBook_(
        WithTitle("Go for Dummies"), WithISBN("1234567890"),
    )
}
View Code

 

标签:ISBN,string,Title,Book,func,funciton,构造函数,option
From: https://www.cnblogs.com/gongxianjin/p/17175815.html

相关文章

  • go options 模式
    ...函数式选项模式的使用场景有哪些呢:我们一般用来配置一些基础的服务配置,比如MySQL,Redis,Kafka的配置,很多可选参数,可以方便动态灵活的配置想要配置的参数。https://zhuan......
  • 使用构造函数 创建3个商品 放到数组中
    类packagecom.fqs.demo1;publicclassShop{privateStringid;privateStringname;privatedoubleprice;privateintkucun;//空参......
  • MySQL报错:ERROR 1290 (HY000): The MySQL server is running with the --secure-file-
     MySQL报错:ERROR1290(HY000):TheMySQLserverisrunningwiththe--secure-file-privoptionsoitcannotexecutethisstatement 测试使用“select...into......
  • JavaScript 构造函数
    <!DOCTYPEhtml><html> <head> <metacharset="UTF-8"> <title></title> <scripttype="text/javascript"> /* *创建一个构造函数,专门用来创建Person对......
  • ES5构造函数继承
      <!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title></head><body><script>functionPhone(brand,pr......
  • 构造函数和析构函数
    类内的构造函数:相当于初始化函数,名字和类名一致,可以在里面写入初始化语句类内的析构函数类的对象调用完所有成员函数,将跳出程序之前释放内存空间,名字是构造函数......
  • 拷贝构造函数调用时机
    这里讨论两种情况下的拷贝函数调用:作为参数传值过程中的拷贝函数调用,有一个Dog类,d1是Dog类的一个实例现在通过function将d1传入,voidfunction(Dogdog);d1是dog的实参,在......
  • 构造函数的注意事项
    调用无参构造不能使用括号,编译器会判定这种写法为函数申明构造函数除了一般的参数构造和默认的无参构造,还有拷贝构造注意拷贝构造的写法。假设有一个Dog类拷贝构造函数为......
  • 构造函数说明
    1#include<iostream>23usingnamespacestd;456classAdder{7public:89//构造函数10//第一份代码相当于后面的两个函数......
  • 深入学习jquery源码之jQuery的构造函数与实例化
    深入学习jquery源码之jQuery的构造函数与实例化创建jQuery对象的整个流程如下:1、调用$()方法;2、调用jQuery.prototype.init()构造函数;3、根据选择器不同返回不同的jQuery对......