如果我有以下接口和结构:
package shape
type Shape interface {
Area()
}
type Rectangle struct {
}
func (this *Rectangle) Area() {}
func New() Shape {
return &Rectangle{}
}
那么如何将New()
方法(作为构造函数)添加到接口中Shape
?
用例是,如果我有另一个结构Square
type Square struct {
Rectangle
}
那么Square
就会有一个方法Area()
。但它不会有New()
。我的目的是让任何继承的结构都自动Shape
拥有一个New()
方法。我怎样才能做到这一点?