Java 静态方法 + 类



首先,我想说我对Java很陌生,来自C++背景。我永远无法与我的老师取得联系,所以我想尝试在这里发布这个问题,我已经想知道了一段时间(希望我能正确措辞):

如何在不使用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,当我尝试在 main 中调用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。

要访问类的非静态成员,您需要该类的实例。

所以new Movie("a", "b", "c", "d").getGenre()是合法的。

从 main 中删除静态关键字是不合法的,因为它是程序的入口点,因此必须存在。

编辑:在第一个源代码中,main() 方法不会创建任何 MusicLibrary 实例,这就是为什么您需要在所有成员上使用 static 的原因。

添加MusicLibrary lib = new MusicLibrary();然后调用lib.welcomeMenu();,您可以摆脱静态关键字。

首先,非静态方法只能使用类的实例调用,

这个链接可能会有所帮助,以便稍微理解一下。这种类型的问题已经在这里得到了回答:这个

相关内容

  • 没有找到相关文章

最新更新