JAVA - 访问方法和来自另一个 Class 的 Classes

JAVA - Access Methods and Classes from another Class

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

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.");
    }
  }
}

第二个class是:

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?");
  }
}

如您所见,我只想制作一个非 GUI 的 3 间客房的酒店预订程序。

我的问题:

我如何访问第二个 class 上的 "describe" 参数,以便我可以 link 将它们添加到主函数中。如果我做 public void describe1() 它主要说 "no void is allowed here".

像这样重写你的代码:

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?");
}

你不是想写:

// 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?");
  }

希望对您有所帮助。

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

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

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

 Prices prices = new Prices();

您已经在您的代码中使用令人困惑的名称 describe1 做到了这一点。我在这里假定名称价格,以便对象实例和方法具有不同的名称。这不是必须的,只是为了更容易识别哪个是哪个。

选项 1:将 describe1 return 设为字符串。

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

并通过

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

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

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