在创建接口切片并将其启动为具体类型时遇到一些问题,任何帮助都会非常感激界面type MatrixElement interface { GetValue() Element GetCoordinate() Coordinate}具体实施type LocatableElement struct { value datastructures.Element coordinate datastructures.Coordinate}func (ele LocatableElement)GetValue() datastructures.Element { return ele.value}func (ele LocatableElement)GetCoordinate() datastructures.Coordinate { return ele.coordinate}func CreateLocatableElement(value datastructures.Element, coordinate datastructures.Coordinate) LocatableElement { return LocatableElement{ value: value, coordinate: coordinate, }}将类型定义为切片type HorizontalMatrix [][]datastructures.MatrixElement创建新 HorizonatlMatrix 的实例func CreateHorizontalMatrix(rows int, columns int) HorizontalMatrix { horzMatrix := make([][]matrix.LocatableElement, rows) for i := 0; i < rows; i++ { horzMatrix[i] = make([]matrix.LocatableElement, columns) } return horzMatrix;}cannot use horzMatrix (type [][]matrix.LocatableElement) as type HorizontalMatrix in return argument
1 回答
慕后森
TA贡献1802条经验 获得超5个赞
尽管如此 ,您既不能强制转换[]ConcreteTypes([]Interfaces)也不能断言implements 。您应该定义容器[]Interfaces.([]ConcreteTypes)ConcreteTypesInterfaces
type Matrix interface{
GetElem(abs, ord int) MatrixElement
}
并满足它HorizontalMatrix或使矩阵作为[]接口
horzMatrix := make([][]matrix.MatrixElement, rows)
horzMatrix[i] = make([]matrix.MatrixElement, columns)
然后用具体类型填充它LocatableElement
- 1 回答
- 0 关注
- 116 浏览
添加回答
举报
0/150
提交
取消