duanpen9294 2014-08-22 12:28
浏览 46
已采纳

函数参数中的多态性

i found several questions with similar titles, but cannot find the answer to my question in them:

I have the following simple scenario:

types:

    type intMappedSortable interface {
       getIntMapping() int
    }

    type Rectangle struct {
       length, width int
    }

    func (r Rectangle) getIntMapping() int {
        return r.Area();
    }

    func (Rectangle r) getIntMapping() int {
        return r.length * r.width;
    }

main:

func main() {
    r := rand.New(rand.NewSource(time.Now().UnixNano()))

    var values []int
    values = make([]int, 0)

    for i := 0; i < 10; i++ {
        values = append(values, r.Intn(20))
    }

    var rects []Rectangle;
    rects = make([]intMappedSortable, len(values));

    for i,v:= range values {
        r := Rectangle{v,v};
        rects[i] = r;
    }
    for i,v:= range rects {
        fmt.Println(v.Area());
    }


    rectsRet := make(chan intMappedSortable, len(rects));
    sort(rects, rectsRet);
}

doWork:

func sort(values []intMappedSortable, out chan intMappedSortable) {...}

How do i manage to pass the Rectangles to the sorting function and then work with the sorted rectangles in main after it?

I tried:

var rects []*Rectangle;
rects = make([]*Rectangle, len(values));

as a habit from my C days, i don't want to copy the rectangles, just the addresses, so i can sort directly in the original slice, preventing 2 copy procedures for the whole data.

After this failed i tried:

var rects []intMappedSortable;
rects = make([]*Rectangle, len(values));

i learned that Go handles "polymorphism" by holding a pointer to the original data which is not exposed, so i changed *Rectangle to Rectangle, both gave me the compilererror that Rectangle is not []intMappedSortable

What obviously works is:

var rects []intMappedSortable;
rects = make([]intMappedSortable, len(values));

for i,v:= range values {
    r := Rectangle{v,v};
    rects[i] = r;
}

But are are these rectangles now copied or is just the memoryrepresentation of the interface with their reference copied? Additionally there now is no way to access length and width of the rectangles as the slice is not explicitly of type rectangle anymore.

So, how would i implement this scenario? I want to create a slice of ANY structure, that implements the mapToInt(), sort the slice and then keep working with the concrete type after it

EDIT/FOLLOWUP:

I know its not good style, but i'm, experimenting:

can i somehow use type assertion with a dynamic type like:

func maptest(values []intMappedSortable, out interface{}) {
    oType := reflect.TypeOf(out);
    fmt.Println(oType); // --> chan main.intMappedSortable  
    test := values[0].(oType) //i know this is not working AND wrong even in thought because oType holds "chan intMappedSortable", but just for theory's sake
}

how could i do this, or is this not possible. I do not mean wether it is "meant to be done", i know it is not. But is it possible?^^

  • 写回答

1条回答 默认 最新

  • douchui1488 2014-08-22 12:32
    关注

    But are are these rectangles now copied or is just the memory representation of the interface with their reference copied?

    The latter, see "what is the meaning of interface{} in golang?"

    An interface value is constructed of two words of data:

    • one word is used to point to a method table for the value’s underlying type,
    • and the other word is used to point to the actual data being held by that value.

    I want to create a slice of ANY structure, that implements the mapToInt(), sort the slice and then keep working with the concrete type after it

    That isn't possible, as there is no genericity in Go.
    See "What would generics in Go be?"

    That is why you have projects like "gen":

    generates code for your types, at development time, using the command line.
    gen is not an import; the generated source becomes part of your project and takes no external dependencies.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并