简体   繁体   English

Java:接口和抽象类的实现

[英]Java: Implementation of Interfaces and abstract classes

I am trying to build a program where I have a visual representation of an int array while they get sorted. 我正在尝试构建一个程序,其中我有一个int数组的可视化表示,同时它们被排序。 There will be two different search algorithms of choice. 将有两种不同的搜索算法可供选择。 All the functionality is split up into classes, interfaces and abstract classes. 所有功能都分为类,接口和抽象类。 My main problem is getting the data from one thing to the next. 我的主要问题是将数据从一件事情转移到另一件事情。

My main class implements basic window functionality. 我的主类实现了基本的窗口功能。 Paints the window, a few buttons to choose the search algorithm, provides a textfield to input an array and displays in its center a bar graph visualisation of the array. 绘制窗口,选择搜索算法的几个按钮,提供输入数组的文本字段,并在其中心显示数组的条形图可视化。 Drawing the bars is done in a class that extends JComponent . 绘制条形图是在扩展JComponent的类中完成的。 This is also where I transform the String of numbers into an int array. 这也是我将数字字符串转换为int数组的地方。 I can already draw the graphs, change the array and it gets also drawn. 我已经可以绘制图形,更改数组,也可以绘制它。

Now I have an interface called Sorter which provides the following methods. 现在我有一个名为Sorter的接口,它提供了以下方法。

public void setUpTo( int i ); // to limit the number of swaps during the search
public void setNumbers( int[] numbers );
public void sort();
public String getName();
public int getSwaps();

Then I have the abstract class CountingSort that implements Sorter and the class CountingBubbleSort which extends CountingSort . 然后,我有抽象类CountingSort实现Sorter和类CountingBubbleSort延伸CountingSort
This is all pretty confusing to me. 这对我来说非常困惑。
Within my main class I listen to a button to pass on the content of TextField and start the sorting. 在我的主类中,我听一个按钮来传递TextField的内容并开始排序。 What do I need to do to get the int array through CountingSort into CountingBubbleSort ? 我需要做什么才能通过CountingSort将int数组导入CountingBubbleSort I have already implemented CountingBubbleSort . 我已经实现了CountingBubbleSort

Let me know what additional information I need to provide. 让我知道我需要提供哪些其他信息。

If you add another method to your Sorter interface ( getNumbers() ), it will guarantee, that all the Sorter implementations will have a getter and setter to the internal int array. 如果向Sorter接口添加另一个方法( getNumbers() ),它将保证所有Sorter实现都有一个getter和setter到内部int数组。

interface Sorter {
    public int[] getNumbers();
    public void setNumbers(int [] numbers);
    //... other methods....
}

Then if you implement it like this in your abstract CountingSort class, then you will be able to use these methods without the need to implement them in all subclasses. 然后,如果在抽象的CountingSort类中实现它,那么您将能够使用这些方法而无需在所有子类中实现它们。

abstract class CountingSort implements Sorter{
    private int [] mNumbers;

    @Override
    public int[] getNumbers() {
        return mNumbers;
    }

    @Override
    public void setNumbers(int[] numbers) {
        mNumbers = numbers;
    }
}

Then you can access the numbers in the concrete implementation any time, if you have them set. 然后,如果已设置,则可以随时访问具体实现中的数字。 I would advise to use a constructor in the CountingBubbleSort , which takes the int array (or generates it randomly, however you want to create it). 我建议在CountingBubbleSort使用一个构造函数,它接受int数组(或者随机生成它,但是你想创建它)。 This constructor could maybe moved up to the abstract class: 这个构造函数可能会移动到抽象类:

class CountingBubbleSort extends CountingSort{
    public CountingBubbleSort(int [] numbers){
        setNumbers(numbers);
    }

    public void someOtherMethod(){
        int [] numbers = getNumbers();
    }
}

In your main class you can use what inheritance has to offer, something like this: 在您的主类中,您可以使用继承提供的内容,如下所示:

class MainClass {
    private Sorter mSorter;

    public void doSort(){
        //Create sorter objects
        if(you want CountingBubbleSort){
            mSorter = new CountingBubbleSort();
        }else{
            mSorter = new BubbleSort();
        }
        //get the numbers
        int [] numbers = mSorter.getNumbers();

        // do the sorting
        mSorter.sort();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM