我是一名来自德国的学生,很抱歉可能语言不好。
我从讲师那里得到了 Java 基础知识的练习,但理解有问题。
他已经教了我们有关 OOP 的基础知识,所以我知道如何生成所需类的对象,例如使用 ClassX myObject = new ClassX ();
和类似的简单内容。
但现在在这个新练习中,我们必须创建一个工具来可视化排序算法并分析复杂性。讲师通过告诉我们一些帮助我们实现这一点:
用户使用该工具的一种可能方式是:
SortingTool sTool = new SortingTool();
sTool.setInitData(10,false);
sTool.performExperiment(Algorithms.INSERTIONSORT);
我很确定他的意思是我们创建一个名为 Algorithms
的类,并在其中构造一个最终对象,如下所示:
public class Algorithms {
public final static Algorithms INSERTIONSORT = new Algorithms ();
有人可以解释一下这是如何工作的吗?尤其是 sTool
的 performExperiment
类应该从对象 INSTERTIONSORT
中获取什么。
提前谢谢大家!
最佳答案
可能是这样的
interface Algorithms {
public static Algorithms INSERTION_SORT = new InsertionSort();
void sort(List<Integer> data);
}
public class InsertionSort implements Algorithms {
public void sort(List<Integer> data) {
// do insertion sort
}
}
sTool.performExperiment()
应该是这样的
performExperiment(Algorithms algo) {
algo.sort(data);
}
关于java - 什么将参数传递给类似 ...(myClass.FINALOBJECT) 的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53860168/