第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

創(chuàng)建一個(gè)具體類型的切片并轉(zhuǎn)換到其各自的接口

創(chuàng)建一個(gè)具體類型的切片并轉(zhuǎn)換到其各自的接口

Go
ibeautiful 2022-03-07 16:32:17
在創(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
查看完整描述

1 回答

?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

盡管如此 ,您既不能強(qiáng)制轉(zhuǎn)換[]ConcreteTypes([]Interfaces)也不能斷言implements 。您應(yīng)該定義容器[]Interfaces.([]ConcreteTypes)ConcreteTypesInterfaces


type Matrix interface{

    GetElem(abs, ord int) MatrixElement 

}

并滿足它HorizontalMatrix或使矩陣作為[]接口


horzMatrix := make([][]matrix.MatrixElement, rows)

horzMatrix[i] = make([]matrix.MatrixElement, columns)

然后用具體類型填充它LocatableElement


查看完整回答
反對(duì) 回復(fù) 2022-03-07
  • 1 回答
  • 0 關(guān)注
  • 143 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)