Golang面向对象编程架构,将一个切片传递给构造函数,创建一个切片对象。

huangapple go评论79阅读模式
英文:

Golang OOP architecture, passing a slice into a constructor, creating a slice object

问题

我刚刚开始学习go语言,一切看起来都很酷,我编写了我需要的所有代码,但是我遇到了两个问题,实际上是将一个切片传递给一个Struct或者所谓的object,以及如何创建一个方法,例如我可以添加另一个矩阵,但我认为这是一个较小的问题。

代码:

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    a := Matrix(nonsingularRandomMatrix())
    fmt.Println(a)
}

type Matrix struct {

    inputMatrix [][]int
}

func NewThing(inMatrix [][]int) *Matrix {
    p := new(Matrix)
    p.inputMatrix = inMatrix
    return p
}

func nonsingularRandomMatrix() [][]int {
    rand.Seed(time.Now().UTC().UnixNano())
    var matrixDeterminant int = 0
    nm := make([][]int, 4)
    for i := 0; i < 4; i++ {
        nm[i] = make([]int, 4)
        for j := 0; j < 4; j++ {
            nm[i][j] = rand.Intn(100)
    }
}
matrixDeterminant =
    nm[0][0]*nm[1][1]*nm[2][2]*nm[3][3] - nm[0][0]*nm[1][1]*nm[2][3]*nm[3][2] -
        nm[0][0]*nm[1][2]*nm[2][1]*nm[3][3] + nm[0][0]*nm[1][2]*nm[2][3]*nm[3][1] +
        nm[0][0]*nm[1][3]*nm[2][1]*nm[3][2] - nm[0][0]*nm[1][3]*nm[2][2]*nm[3][1] -
        nm[0][1]*nm[1][0]*nm[2][2]*nm[3][3] + nm[0][1]*nm[1][0]*nm[2][3]*nm[3][2] +
        nm[0][1]*nm[1][2]*nm[2][0]*nm[3][3] - nm[0][1]*nm[1][2]*nm[2][3]*nm[3][0] -
        nm[0][1]*nm[1][3]*nm[2][0]*nm[3][2] + nm[0][1]*nm[1][3]*nm[2][2]*nm[3][0] +
        nm[0][2]*nm[1][0]*nm[2][1]*nm[3][3] - nm[0][2]*nm[1][0]*nm[2][3]*nm[3][1] -
        nm[0][2]*nm[1][1]*nm[2][0]*nm[3][3] + nm[0][2]*nm[1][1]*nm[2][3]*nm[3][0] +
        nm[0][2]*nm[1][3]*nm[2][0]*nm[3][1] - nm[0][2]*nm[1][3]*nm[2][1]*nm[3][0] -
        nm[0][3]*nm[1][0]*nm[2][1]*nm[3][2] + nm[0][3]*nm[1][0]*nm[2][2]*nm[3][1] +
        nm[0][3]*nm[1][1]*nm[2][0]*nm[3][2] - nm[0][3]*nm[1][1]*nm[2][2]*nm[3][0] -
        nm[0][3]*nm[1][2]*nm[2][0]*nm[3][1] + nm[0][3]*nm[1][2]*nm[2][1]*nm[3][0]
if matrixDeterminant == 0 {
    nonsingularRandomMatrix()
}
return nm
}

我的控制台输出:

> go run oop.go
>
> command-line-arguments
>
> .\oop.go:10: 无法将nonsingularRandomMatrix() (类型为[][]int) 转换为类型Matrix

有什么线索吗?

问候!

英文:

I just started with go language, everything looks cool, I coded everything I need but I have two problems, actually it is passing an slice into a Struct or so called object, and how to create a method that I can add for example another matrix, but this is minor problem I think.

Code:

package main

import (
    &quot;fmt&quot;
    &quot;math/rand&quot;
    &quot;time&quot;
)

func main() {
    a := Matrix(nonsingularRandomMatrix())
    fmt.Println(a)
}

type Matrix struct {

    inputMatrix [][]int
}

func NewThing(inMatrix [][]int) *Matrix {
    p := new(Matrix)
    p.inputMatrix = inMatrix
    return p
}

func nonsingularRandomMatrix() [][]int {
    rand.Seed(time.Now().UTC().UnixNano())
    var matrixDeterminant int = 0
    nm := make([][]int, 4)
    for i := 0; i &lt; 4; i++ {
        nm[i] = make([]int, 4)
        for j := 0; j &lt; 4; j++ {
            nm[i][j] = rand.Intn(100)
    }
}
matrixDeterminant =
    nm[0][0]*nm[1][1]*nm[2][2]*nm[3][3] - nm[0][0]*nm[1][1]*nm[2][3]*nm[3][2] -
        nm[0][0]*nm[1][2]*nm[2][1]*nm[3][3] + nm[0][0]*nm[1][2]*nm[2][3]*nm[3][1] +
        nm[0][0]*nm[1][3]*nm[2][1]*nm[3][2] - nm[0][0]*nm[1][3]*nm[2][2]*nm[3][1] -
        nm[0][1]*nm[1][0]*nm[2][2]*nm[3][3] + nm[0][1]*nm[1][0]*nm[2][3]*nm[3][2] +
        nm[0][1]*nm[1][2]*nm[2][0]*nm[3][3] - nm[0][1]*nm[1][2]*nm[2][3]*nm[3][0] -
        nm[0][1]*nm[1][3]*nm[2][0]*nm[3][2] + nm[0][1]*nm[1][3]*nm[2][2]*nm[3][0] +
        nm[0][2]*nm[1][0]*nm[2][1]*nm[3][3] - nm[0][2]*nm[1][0]*nm[2][3]*nm[3][1] -
        nm[0][2]*nm[1][1]*nm[2][0]*nm[3][3] + nm[0][2]*nm[1][1]*nm[2][3]*nm[3][0] +
        nm[0][2]*nm[1][3]*nm[2][0]*nm[3][1] - nm[0][2]*nm[1][3]*nm[2][1]*nm[3][0] -
        nm[0][3]*nm[1][0]*nm[2][1]*nm[3][2] + nm[0][3]*nm[1][0]*nm[2][2]*nm[3][1] +
        nm[0][3]*nm[1][1]*nm[2][0]*nm[3][2] - nm[0][3]*nm[1][1]*nm[2][2]*nm[3][0] -
        nm[0][3]*nm[1][2]*nm[2][0]*nm[3][1] + nm[0][3]*nm[1][2]*nm[2][1]*nm[3][0]
if matrixDeterminant == 0 {
    nonsingularRandomMatrix()
}
return nm
}

Output from my console:

> go run oop.go
>
> command-line-arguments
>
> .\oop.go:10: cannot convert nonsingularRandomMatrix() (type [][]int) to type Matrix

Any clue?

Greets!

答案1

得分: 3

你的编译器的意思是[][]intMatrix是两个完全不相关的类型。你不能从一个类型转换为另一个类型。这是因为Matrix是一个包含[][]int的结构体,但Matrix本身并不是[][]int。要创建一个Matrix,你需要创建一个新的Matrix并将[][]int设置为其唯一成员:

a := Matrix{nonsingularRandomMatrix()}

更简单的方法是将Matrix定义为[][]int的别名:

type Matrix [][]int

这将为[][]int创建一个新的名称Matrix,在这种情况下,可以使用简单的(显式)转换:

Matrix(nonsingularRandomMatrix())
英文:

What your compiler is saying is that [][]int and Matrix are two totally unrelated types. You can not convert from one to the other. Thats because Matrix is a struct which contains a [][]int, but Matrix is not an [][]int. To create a Matrix out of it you'd need to create a new Matrix and set the [][]int to the only member:

a := Matrix{nonsingularRandomMatrix()}

An easier way would be if Matrix were a [][]int.

type Matrix [][]int

This would just create a new name (Matrix) for [][]int, and in this case, a simple (explicit) conversion would work

Matrix(nonsingularRandomMatrix())

huangapple
  • 本文由 发表于 2016年9月9日 10:07:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/39402767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定