如何在java中另一个类的静态方法中调用非静态方法?
问题描述:
我已经在我的课如何在java中另一个类的静态方法中调用非静态方法?
@Override
public void liveFlights(int one, int two, int three, int four, int five,
int six, int seven, int eight, int nine, int ten, int eleven, int twelve) {
System.out.println("There is "+counter+" flights at this moment ");
System.out.println("England to Netherland at 12.00 pm "+one+"is flight number");
System.out.println("Netherland to England at 12.00 pm "+two+"is flight");
System.out.println("Turkey to USA at 06.00 pm "+three+"is flight number");
的一个这种方法,我想称之为非静态方法在这个静态方法(其他类)
public static void searchResEntry() throws java.io.IOException // start of method
{
// variable declarations
String Name;
String Address;
Scanner read = new Scanner(System.in);
System.out.print("Please enter your following information for your reservation result: \n"); // displaying the information to enter for searching a reservation
System.out.print(" Enter Name : "); // displaying message to enter name
Name = read.nextLine(); // prompt for the name
}
任何帮助?
答
包含非静态方法分为静态方法,它
public static void searchResEntry(YourClass instance) throws java.io.IOException // start of method
{
// variable declarations
instance.liveFlights(...);
或创建实例类的
通行证实例:
public static void searchResEntry() throws java.io.IOException // start of method
{
// variable declarations
YourClass instance = new YourClass();
instance.liveFlights(...);
+0
我忘了添加,非静态方法是在我的抽象类,为什么实例认为不工作。它取决于? –
+0
既然你不能实例化抽象类,除非你有一些类来扩展这个抽象类,否则在它中实现非静态(实例)方法是没有意义的。要么声明这个方法是'abstract',并且在扩展抽象类的类中提供实现,或者将此方法声明为'static' –
你的静态方法则需要包含非类的一个实例静态方法。 – NAMS