如何在没有对象实例化的情况下编写静态方法调用

时间:2014-04-16 23:58:30

标签: java object methods

做一些工作,我需要使用面向对象编程编写方法,并在没有对象实例化的情况下制作方法。 我不知道该怎么做。我已经回复了以下反馈:

Your main() method should go in the tester class, while all of the implementation methods and          
constructor should go in the object class.

这是一些代码,谢谢!

public static void main(String[] args) throws IOException
{      
  // variables
  Scanner in = new Scanner(System.in);
  File fileRead = new File("/Users/jerome/Desktop/MorseCode.txt");
  Scanner withinFile = new Scanner (fileRead);
  String userInput = "";
  String toMorse = "";
  String[] file = new String[25];
  String[] alp  = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "o", "m", "n",   "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
         for(int i = 0; i < 25; i ++)
     {
      file[i] = withinFile.nextLine();

  }
  System.out.print("Enter a word to translate: ");
  userInput = in.nextLine();
  // static methods
  MorseCode2 morse = new MorseCode2(); 
  morse.tooMorse(userInput, alp, file, toMorse);
  // output
  System.out.print("'"+ userInput+"'"+" translated into Morse Code is: " + tooMorse(userInput, alp, file, toMorse));
 }

再多一点!

  public static String tooMorse(String userInput, String[] alp, String[] file, String toMorse)
  {
 for(int i = 0; i < userInput.length(); i ++)
  {
    if(userInput.substring(i, i+1).equals(alp[i])) // a
    {
        toMorse += "  " + file[i];
    }
    else if(userInput.substring(i, i+1).equals(alp[i+1])) // b
    {
        toMorse += "  " + file[i+1];
    }
     else if(userInput.substring(i, i+1).equals(alp[i+2])) // c
    {
        toMorse += "  " + file[i+2];
    }
    else if(userInput.substring(i, i+1).equals(alp[i+3])) // d
    {
        toMorse += "  " + file[i+3];
    }
     else if(userInput.substring(i, i+1).equals(alp[i+4])) // e
    {
        toMorse += "  " + file[i+4];
    }
    else if(userInput.substring(i, i+1).equals(alp[i+5])) // f
    {
        toMorse += "  " + file[i+5];
    }
     else if(userInput.substring(i, i+1).equals(alp[i+6])) // g
    {
        toMorse += "  " + file[i+6];
    }
    else if(userInput.substring(i, i+1).equals(alp[i+7])) //h 
    {
        toMorse += "  " + file[i+7];
    }
     else if(userInput.substring(i, i+1).equals(alp[i+8])) // i 
    {
        toMorse += "  " + file[i+8];
    }
    else if(userInput.substring(i, i+1).equals(alp[i+9])) // j
    {
        toMorse += "  " + file[i+9];
    }
     else if(userInput.substring(i, i+1).equals(alp[i+10])) // k
    {
        toMorse += "  " + file[i+10];
    }
    else if(userInput.substring(i, i+1).equals(alp[i+11])) // l
    {
        toMorse += "  " + file[i+11];
    }
     else if(userInput.substring(i, i+1).equals(alp[i+12])) // m
    {
        toMorse += "  " + file[i+12];
    }
    else if(userInput.substring(i, i+1).equals(alp[i+13])) // n
    {
        toMorse += "  " + file[i+13];
    }
     else if(userInput.substring(i, i+1).equals(alp[i+14])) //o
    {
        toMorse += "  " + file[i+14];
    }
    else if(userInput.substring(i, i+1).equals(alp[i+15])) // p 
    {
        toMorse += "  " + file[i+15];
    }
     else if(userInput.substring(i, i+1).equals(alp[i+16])) // q
    {
        toMorse += "  " + file[i+16];
    }
    else if(userInput.substring(i, i+1).equals(alp[i+17])) // r
    {
        toMorse += "  " + file[i+17];
    }
     else if(userInput.substring(i, i+1).equals(alp[i+18])) // s
    {
        toMorse += "  " + file[i+18];
    }
    else if(userInput.substring(i, i+1).equals(alp[i+19])) // t
    {
        toMorse += "  " + file[i+19];
    }
     else if(userInput.substring(i, i+1).equals(alp[i+20])) // u
    {
        toMorse += "  " + file[i+20];
    }
    else if(userInput.substring(i, i+1).equals(alp[i+21])) // v
    {
        toMorse += "  " + file[i+21];
    }
     else if(userInput.substring(i, i+1).equals(alp[i+22])) // w
    {
        toMorse += "  " + file[i+22];
    }
    else if(userInput.substring(i, i+1).equals(alp[i+23])) // x
    {
        toMorse += "  " + file[i+23];
    }
     else if(userInput.substring(i, i+1).equals(alp[i+24])) // y
    {
        toMorse += "  " + file[i+24];
    }
    else if(userInput.substring(i, i+1).equals(alp[i+25])) // z
    {
        toMorse += "  " + file[i+25];
    }
     else
    {
        return "no";
    }
}
  return toMorse;

1 个答案:

答案 0 :(得分:0)

MorseCode2 morse = new MorseCode2(); 
morse.tooMorse(userInput, alp, file, toMorse);

将其更改为:

MorseCode2.tooMorse(userInput, alp, file, toMorse);

如果您有其他静态方法,请执行相同的操作。

与普通方法不同,您不会在类的实例上调用静态方法 - 仅在类本身上调用。如果您执行编写morse.tooMorse(...),编译器将为您简单地将其转换为MorseCode2.tooMorse(...),并发出警告。所以你的原始代码等同于:

MorseCode2 morse = new MorseCode2();
MorseCode2.tooMorse(userInput, alp, file, toMorse);

由于morse未在任何地方使用,因此效率稍低。