在創(chuàng)建接口切片并將其啟動(dòng)為具體類型時(shí)遇到一些問題,任何幫助都會(huì)非常感激界面type MatrixElement interface { GetValue() Element GetCoordinate() Coordinate}具體實(shí)施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創(chuàng)建新 HorizonatlMatrix 的實(shí)例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
創(chuàng)建一個(gè)具體類型的切片并轉(zhuǎn)換到其各自的接口
ibeautiful
2022-03-07 16:32:17