JAVA - 从另一个类访问方法和类

时间:2015-01-22 09:20:44

标签: java netbeans

到目前为止我得到了这个:

package hotel;
import java.util.Scanner; 

public class Hotel {

/**
 * @param args the command line arguments
*/
public static void main(String[] args) {
Prices describe1 = new Prices();
Scanner user_in = new Scanner(System.in);
String method;

System.out.println("Welcome to Hotel HIV!");
System.out.println("We are happy to see that you would like to stay with    us.");
System.out.println("Please, type which room you would like to book: ");
System.out.println("Single Bed, Double Bed, President Suit");
method = user_in.nextLine();

if ("Single Bed".equals(method)) {
    System.out.println(Prices.describe1);
} else {
    System.out.println("Please choose one of the rooms above.");
    }
  }
}

第二节课是:

package hotel;
import java.util.Scanner;
/**
*
* @author Defalt
*/
public class Prices {
Scanner user_in = new Scanner(System.in);
int price1 = 300;
int price2 = 600;
int price3 = 2500;

class describe1 {      
System.out.println("You Choose the Single Bed.");
System.out.println("This room Contains 1 Bed, 1 Fridge but no View on  the Ocean.");
System.out.println("This room will cost CHF " + price1 + ".-.");
System.out.println("Would you like to book this room?");
  }
}

如您所见,我只想为3个房间制作非GUI酒店预订计划。

我的问题:

如何访问第二类的“describe”参数,以便我可以将它们链接到main函数中。如果我做公开void describe1(),它主要说“这里不允许空白”。

3 个答案:

答案 0 :(得分:0)

重写你的代码:

System.out.println(Prices.describe1);

变为

System.out.println(new Prices().describe1());

并且

class describe1 {      
    System.out.println("You Choose the Single Bed.");
    System.out.println("This room Contains 1 Bed, 1 Fridge but no View on  the Ocean.");
    System.out.println("This room will cost CHF " + price1 + ".-.");
    System.out.println("Would you like to book this room?");
}

变为

public void describe1() {      
    System.out.println("You Choose the Single Bed.");
    System.out.println("This room Contains 1 Bed, 1 Fridge but no View on  the Ocean.");
    System.out.println("This room will cost CHF " + price1 + ".-.");
    System.out.println("Would you like to book this room?");
}

答案 1 :(得分:0)

你没打算写:

// This is a (public) method of hotel.Prices
public void describe1()
{      
    System.out.println("You Choose the Single Bed.");
    System.out.println("This room Contains 1 Bed, 1 Fridge but no View on  the Ocean.");
    System.out.println("This room will cost CHF " + price1 + ".-.");
    System.out.println("Would you like to book this room?");
}

而不是:

 // This is a inner (non-static nested) class...you must have a (public) method to write the "printing logic"...
 class describe1 
 {      
     System.out.println("You Choose the Single Bed.");
     System.out.println("This room Contains 1 Bed, 1 Fridge but no View on  the Ocean.");
     System.out.println("This room will cost CHF " + price1 + ".-.");
     System.out.println("Would you like to book this room?");
  }

希望它有所帮助。

答案 2 :(得分:0)

正如其他答案已经指出的那样,describe1需要是一种方法而不是一种类。作为方法,它需要static或在对象实例上调用。

您使用describe1()作为println的参数。在这种情况下,它不能返回void。你有两个选择。

在这两种情况下,您首先创建一个实例:

 Prices prices = new Prices();

您已经在代码中使用令人困惑的名称describe1执行此操作。我假设这里的名称价格,因此对象实例和方法具有不同的名称。这不是一个要求,只是为了更容易识别哪个。

选项1:make describe1返回一个String。

public String describe1() {
    return "You chose ....\nThis room Contains....";
}

并继续通过

调用它
System.out.println(prices.describe1());

选项2:让方法执行输出并调用它

if ("Single Bed".equals(method)) {
   prices.describe1();
} else {
   // ...
}