简体   繁体   English

如何将main方法分成自己的类?

[英]How do I seperate main method into its own class?

So I wrote this Bubblesort code and everything is working fine, but now I want to split this up into two classes and I can't find out how to do this. 因此,我编写了这段Bubblesort代码,一切正常,但是现在我想将其分为两个类,而我找不到执行该操作的方法。

import java.util.Arrays;
public class bubblesorter
{
    public static int[] bubblesort(int[] zusortieren) {
        int temp;
        for(int i=1; i<zusortieren.length; i++) {
            for(int j=0; j<zusortieren.length-i; j++) {
                if(zusortieren[j]<zusortieren[j+1]) {
                    temp=zusortieren[j];
                    zusortieren[j]=zusortieren[j+1];
                    zusortieren[j+1]=temp;
                }

            }
        }
        return zusortieren;
    }
        public static void main(String[] args) {
        int[] unsortiert={1,5,8,2,7,4};
        int[] sortiert=bubblesort(unsortiert);

        for (int i = 0; i<sortiert.length; i++) {
            System.out.print(sortiert[i] + ", ");
        }
    }
}

Thanks for your help. 谢谢你的帮助。

I think you want something like this: 我想你想要这样的东西:

Main class 主班

public class Main
{
    public static void main(String[] args) {
        int[] unsortiert={1,5,8,2,7,4};
        int[] sortiert=BubbleSort.sort(unsortiert);

        for (int i = 0; i<sortiert.length; i++) {
            System.out.print(sortiert[i] + ", ");
        }
    }
}

Sort class 排序类别

public class BubbleSort {
    public static int[] sort(int[] zusortieren) {
        int temp;
        for(int i=1; i<zusortieren.length; i++) {
            for(int j=0; j<zusortieren.length-i; j++) {
                if(zusortieren[j]<zusortieren[j+1]) {
                    temp=zusortieren[j];
                    zusortieren[j]=zusortieren[j+1];
                    zusortieren[j+1]=temp;
                }

            }
        }
        return zusortieren;
    }
}

Just copy paste your main into it's own file. 只需将您的main复制粘贴到它自己的文件中即可。

Main.java Main.java

public class Main
{
    public static void main(String[] args) {
        int[] unsortiert={1,5,8,2,7,4};
        int[] sortiert=BubbleSort.sort(unsortiert);

        for (int i = 0; i<sortiert.length; i++) {
            System.out.print(sortiert[i] + ", ");
        }
    }
}

BubbleSort.java BubbleSort.java

public class BubbleSort {
    public static int[] sort(int[] zusortieren) {
        int temp;
        for(int i=1; i<zusortieren.length; i++) {
            for(int j=0; j<zusortieren.length-i; j++) {
                if(zusortieren[j]<zusortieren[j+1]) {
                    temp=zusortieren[j];
                    zusortieren[j]=zusortieren[j+1];
                    zusortieren[j+1]=temp;
                }

            }
        }
        return zusortieren;
    }
}

As long as you refer to both files in the javac call when it comes time to compile, it'll all work nicely. 只要在编译时就在javac调用中引用了这两个文件,它们就可以很好地工作。 However, for the sake of not being messy, some structure is good to have. 但是,为了不混乱,某些结构是好的。 Later in your Java experience, you'll be hearing about tools like Maven and Gradle, so I suggest getting in the habit of using their folder formats: 在您的Java经验的后期,您将听到有关Maven和Gradle之类的工具的信息,因此,我建议养成使用其文件夹格式的习惯:

ProjectName/
    src/main/java/
        LuisIsLuis/Awesome/Package/
            Main.java
            BubbleSort.java

I did not follow "pretty package naming" conventions, I figure you'll learn that on your own later. 我没有遵循“漂亮的程序包命名”约定,我想您以后会自己学习的。 The only thing you need to do with the files themselves is put package LuisIsLuis.Awesome.Package; 您需要对文件本身做的唯一一件事情就是将package LuisIsLuis.Awesome.Package; as the first line in both files when organizing your code in this manner; 以这种方式组织代码时,作为两个文件的第一行; if you're curious as to why, go look up package naming standards. 如果您对原因感到好奇,请查阅软件包命名标准。 That being said, your coursework will probably cover that soon. 话虽这么说,您的课程可能很快就会涵盖。

It is pretty straight forward and depends on what you want to achieve. 这非常简单,取决于您要实现的目标。 If you have a helper class with static methods, then add static method BubbleSorter to it and access it like rmpt mentioned above. 如果您的帮助程序类具有静态方法,则向其添加静态方法BubbleSorter并像上面提到的rmpt一样访问它。 Otherwise, you can store the method in a separate class BubbleSorter.java and access it via an instance of the class. 否则,您可以将方法存储在单独的类BubbleSorter.java中,并通过该类的实例进行访问。

public class Main
{
    int[] unsortiert={1,5,8,2,7,4};
    Bubblesorter bubble = new Bubblesorter();
    int [] sortiert = bubble.bubblesort(unsortiert);

    for (int i = 0; i<sortiert.length; i++) {
        System.out.print(sortiert[i] + ", ");
    }
}

public class Bubblesorter
{
    public int[] bubblesort(int[] zusortieren) {
        int temp;
        for(int i=1; i<zusortieren.length; i++) {
            for(int j=0; j<zusortieren.length-i; j++) {
                if(zusortieren[j]<zusortieren[j+1]) {
                    temp=zusortieren[j];
                    zusortieren[j]=zusortieren[j+1];
                    zusortieren[j+1]=temp;
                }
            }
        }
        return zusortieren;
    }
}

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

相关问题 我需要帮助将此主要方法与 class 分开 - i need help seperate this main method to a class 如何制作新的主 class? 已经制作了一个,但不是自己的。java 文件,目前无法使用 - How do I make a new main class? There is already one made but its not its own .java file and currently does not work main 方法中使用的方法是否需要自己的类? - Does a method used in main method need its own class? 继承并在其自己的类/包中使用 main 方法 - inheretance and using main method in its own class/package 如何使用自己的类克隆基于对象的数组列表? - How do I clone a object based arraylist with its own class? 如何检测Java类是由其自己的main()调用还是从另一个类调用? - How can I detect if a Java class is called by its own main() or from another class? 如何在另一个类中调用一个类的main()方法? - How do I invoke a main() method of one class in another class? Java do-while循环在自己类中的方法内部 - Java do-while loop inside a method in its own class Java:主类中的递归调用subclass-method而不是自己的方法 - Java: recursion within main-class calls subclass-method instead of its own method 如何定期从main方法执行可运行类? - How do I periodically execute a runnable class from a main method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM