2016-03-01 112 views
0

首先,我想說我對Java很陌生,來自C++背景。我永遠無法接觸到我的老師,所以我想嘗試在這裏發表這個問題,我一直在想(我希望我能正確地說出它):Java靜態方法+類

如何創建方法而不使用static?我知道我可能需要爲它做一個課,但我該怎麼做呢?只是一個沒有變量和功能的類?除了以.java文件命名的類之外,我還要做第二類嗎?例如:

package musiclibrary; 
import java.util.Scanner; 

/** 
* This class implements a user to create a playlist from a selection of artists and songs 
* @author ahb5190 
*/ 
public class MusicLibrary { 
static String divider = "*****************************************************"; 
//Scanner class 
static Scanner input = new Scanner(System.in); 

/** 
* Welcome menu 
*/ 
public static void welcomeMenu() 
{ 
    System.out.println(divider); 
    System.out.println(); 
    System.out.println("Welcome to Art's personal music library!"); 
    System.out.println(); 
    System.out.println("Choose an option:"); 
    System.out.println("1) Create Playlist"); 
    System.out.println("2) Delete Playlist"); 
    System.out.println("3) Add Selection to Playlist"); 
    System.out.println("4) Remove Selection from Playlist"); 
    System.out.println("5) Quit"); 
    System.out.println(); 
    System.out.print("Your choice?: "); 
} 

/** 
* 
* @param min error check low bound 
* @param max error check high bound 
* @return 
*/ 
public static int getData(int min, int max) 
{ 
    boolean goodInput = false; 
    int choice = 1; //Will be overwritten 
    while(!goodInput) 
    {  
     choice = input.nextInt(); 
     if(choice < min || choice > max) 
     { 
      System.out.print(choice + " is not a valid choice. Please enter a number between " + min + " and " + max + ": "); 
      goodInput = false; 
     } 
     else 
     { 
      goodInput = true; 
     } 
    } 

    return choice; 
} 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) 
{ 
    //Variables 
    int getDataMin = 1; 
    int getDataMax = 5; 
    boolean quit = false; 
    int userInput; 

    do { 
     welcomeMenu(); 
     userInput = getData(getDataMin, getDataMax); 
     if (userInput == 1) 
     { 
      quit = false; 
     } 
     else if (userInput == 2) 
     { 
      quit = false; 
     } 
     else if (userInput == 3) 
     { 
      quit = false; 
     } 
     else if (userInput == 4) 
     { 
      quit = false; 
     } 
     else if (userInput == 5) 
     { 
      quit = true; 
     } 
    } while(!quit); 

} 

}

是被分配了java程序的開始。如果我從public static void welcomeMenu()刪除static,那麼當我嘗試呼叫welcomeMenu();時,它會給我non-static method welcomeMenu() cannot be referenced from a static context

的代碼的另一種塊(未很整齊,是一個定時考試的一部分):

package lalala; 

/** 
* 
* @author ahb5190 
*/ 
public class Lalala { 


    public class Movie 
    { 
     private String title; 
     private String genre; 
     private String director; 
     private String star; 

     public Movie (String t, String g, String d, String s) 
     { 
      title = t; 
      genre = g; 
      director = d; 
      star = s; 
     } 
     public String gettitle() 
     { 
      return title; 
     } 
     public String getGenre() 
     { 
      return genre; 
     } 
     public String getDirector() 
     { 
      return director; 
     } 
     public String getStar() 
     { 
      return star; 
     } 
     public void setTitle(String x) 
     { 
      title = x; 
     } 
     public void setGenre(String x) 
     { 
      genre = x; 
     } 
     public void setDirector(String x) 
     { 
      director = x; 
     } 
     public void setsStar(String x) 
     { 
      star = x; 
     } 
     public boolean equals(Movie otherMovie) 
     { 
      if(otherMovie == null) 
      { 
       return false; 
      } 
      else 
      { 
       return title.equals(otherMovie.title) && genre.equals(otherMovie.genre) && director.equals(otherMovie.director) && star.equals(otherMovie.star); 
      } 
     } 
     @Override 
     public String toString() 
     { 
      return(title + " " + genre + " " + director + " " + star); 
     } 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) 
    { 
     Movie a; 
     a = new Movie("Star Trek into Darkness", "Sci-fi", "J.J. Abrams", "Chris Pine"); //error: non-static variables this cannot be referenced from a static context 
     Movie b = new Movie("Star Trek", "Sci-Fi", "J.J. Abrams", "Chris Pine"); //error: non-static variables this cannot be referenced from a static context 
     Movie c = new Movie("Independence Day", "Action", "Roland Emmerich", "Will Smith"); //error: non-static variables this cannot be referenced from a static context 

     System.out.println("Movies"); 

     System.out.println("Title: " + a.title); 
     System.out.println("Genre: " + a.genre); 
     System.out.println("Director: " + a.director); 
     System.out.println("Star: " + a.star); 
     System.out.println(); 
     System.out.println("Title: " + b.title); 
     System.out.println("Genre: " + b.genre); 
     System.out.println("Director: " + b.director); 
     System.out.println("Star: " + b.star); 
     System.out.println(); 
     System.out.println("Title: " + c.title); 
     System.out.println("Genre: " + c.genre); 
     System.out.println("Director: " + c.director); 
     System.out.println("Star: " + c.star); 
     System.out.println(); 

     a.equals(b); 

    } 

} 

給我相同的靜態變量誤差之前,在代碼註釋的上方。在那種情況下,我如何才能「工作」是從public static void main(String args[])中刪除static

真的想學習正確的方式到Java,任何幫助將不勝感激。我也希望這符合MCV。

回答

1

要訪問類的非靜態成員,您需要該類的一個實例。因此new Movie("a", "b", "c", "d").getGenre()是合法的。

從main刪除static關鍵字是不合法的,因爲它是程序的入口點,因此它是強制存在的。

編輯: 在第一個來源,main()方法不會產生MusicLibrary的任何實例,這就是爲什麼你需要在所有成員都使用靜態的。

添加MusicLibrary lib = new MusicLibrary();然後調用lib.welcomeMenu();您可以擺脫靜態關鍵字。

+0

當我把靜態回來時,仍然給了我同樣的錯誤。我只是將靜態寫出來用於筆試,以便我可以測試其餘代碼中的任何語法錯誤,我知道靜態必須在那裏:) – bankey

+0

我已經編輯答案是更具體的你的情況。 – Xvolks

+0

啊!非常感謝!我知道這是小事:)我真的很感激它! – bankey

0

的所有非靜態方法首先只能通過類的實例來調用,

link也許會有幫助,瞭解它bit.And這種類型的問題是已經在這裏找到答案:this

+0

對於問題的另一部分,將類_Movie_放置在類_lalala_之外,由於其與_static void main()_ –