Apex类上的无效构造函数名称错误

问题描述

我在下面编写了Apex类。但是由于“无效的构造函数名称:randomize”而无法使用。有人有想法吗?

public class apextest {
    List < Integer > testLst = new List < Integer > {
        0,1,2
    };
    randomize(testLst);

    public List < integer > randomize(List < integer > lst) {  
        integer currentIndex = 3;  
        integer randomIndex;  
        while (0 != currentIndex) {   
            randomIndex = integer.valueOf(Math.floor(Math.random() * currentIndex));   
            currentIndex -= 1;   
            lst[currentIndex] = lst[randomIndex];  
        }  
        return lst; 

    }
}

解决方法

public class apextest {
 List<Integer> testLst = new List<Integer>{0,1,2};
 randomize(testLst);

在加载此类后,您将有一些代码在运行,在代码的其他地方引用了该代码。这是一个静态块。您可以做到,这可能不是一个好主意(如果这是运行查询或插入内容-这是非常糟糕的主意),但是如果您希望它可以工作-它也必须是静态方法。您已将randomize()声明为普通的公共实例函数(没有static关键字),因此编译器会困惑您要做什么。