无法在随播对象方法中创建伴随类实例

时间:2018-03-10 16:09:10

标签: scala companion-object

此代码有什么问题:

import sys, copy
from itertools import product


def recursiveSearch(actual, index, size, matrices):
    if index==size-1:
        for j in range(size):
            if sum([actual[i][j] for i in range(size)])>1:
                return
        if sum([actual[i][j] for i in range(size) for j in range(size)]) == size:
            matrices.append(actual)
            return
        return
    cur_row = len(actual)
    for j in range(size):
        if sum([actual[i][j] for i in range(cur_row)])>1:
            return
    pr = product([0, 1], repeat=size-index-1)
    for p in pr:
        new_row = [0 for k in range(index+1)] + list(p)
        new_matrix = copy.deepcopy(actual)
        new_matrix.append(new_row)
        recursiveSearch(new_matrix, index+1, size, matrices)


if __name__ == '__main__':
    input_ = int(sys.argv[1])
    pr = product([0,1], repeat=input_)
    matrices = []
    for p in pr:
        recursiveSearch([list(p)], 0, input_, matrices)
    for m in matrices:
        print(m)

类和对象都在同一个源文件中定义,因此它们是伴侣。

错误消息如下:'无法解析符号t'

1 个答案:

答案 0 :(得分:3)

错误的语法(您正在使用Java语法)来创建对象。对于Scala,您无需提及变量t前面的类型,它将自动推断出来。

Trivials t = new Trivials("Trivials")

Scala语法

val t = new Trivials("Trivials")