如何从main运行实例方法 [英] How to run instance methods from main

查看:121
本文介绍了如何从main运行实例方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,但我在这里脑子里放屁(猜想这就是几天的小睡可以使你发脾气).在不将任何内容更改为静态的情况下,我如何制作将运行此文件的main().

Sorry but I'm having a major brain fart here (guess that's what a few days of little sleep get you). Without changing anything to static how can I make a main() that will run this.

package merger;

import java.util.Random;

public class another {

    public int[] numbers;
    private final static int size = 100;
    private static int maxNumber = 30;
    private final static int limit = 10;
    public int number;
    private boolean Insertion = false;

    public void arraytosort(){
        numbers = new int[size];
        Random number = new Random();
        for (int i=0; i< numbers.length; i++){
            numbers[i] = number.nextInt(maxNumber);
        }
        test(numbers);
    }

    public void test(int[] array){
        this.numbers = array;
        number = array.length;
        mergesort(0,number - 1);
    }

     public void mergesort(int low, int high){
         if(Insertion || high-low < limit){
             insertionsort(low, high);
             return;
         }
         if (low<high){
             int middle = (low+high) / 2;
             mergesort(low, middle);
             mergesort(middle +1, high);
             merge(low,middle,high);
             return;
         }
     }

     public void merge(int low, int middle, int high){
         int[] temp = new int[number];
         for (int i=low;i<=high; i++){
             temp[i] = numbers[i];
         }
         int i = low;
         int j = middle+1;
         int k = low;
         while (i<=middle || j<=high){
             if (temp[i] <= temp[j]){
                 numbers[k] = temp[i];
                 i++;
             }
             else{
                 temp[k] = temp[j];
                 j++;
             }
             k++;
         }
         while (i<=middle){
             temp[k] = temp[i];
             k++;
             i++;
         }
         temp = null;
         return;
             }

     public void insertionsort(int low, int high){
         for(int i=low+1;i<=high;i++){
             int t = numbers[i];
             for(int j = i-1; j>=low; j--){
                 if(t>numbers[j]) break;
                 numbers[j+1] = numbers[j];

             numbers[j+1] = t;
             }
         }
     }

    /**
     * @param args
     */
    public static void main(String[] args){


    }
}

我只需要能够对其进行测试以查看其是否有效.在我的脑海中似乎应该可以使用.

I just need to be able to test it to see if this is working. In my head it seems like it should work.

谢谢

推荐答案

不更改任何静态内容,如何制作main()来运行它.

Without changing anything to static how can I make a main() that will run this.

您必须创建该类的实例:

You have to create an instance of the class:

public static void main(String[] args){
    another instance = new another();
    instance.whateverMethodYouLike();
}


顺便说一句,请遵循Java约定和名称类,并使用大写首字母.


BTW, please follow the Java convention and name classes with a capital first letter.

这篇关于如何从main运行实例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆