【问题标题】:Classes, Objects, and Methods help in Java类、对象和方法在 Java 中的帮助
【发布时间】:2015-04-14 03:12:27
【问题描述】:

NetBeans 不允许我运行我的程序,因为公共类 HotelManagement 和公共类 Room 存在问题。谁能帮我解决这个问题,这样我的程序才能顺利运行。谢谢

public class HotelManagement {

    public static void main(String[] args) {
        Room room1 = new Room(3348, 80, "Single Bed"); 
        Room room2 = new Room(3347, 90, "Double Bed"); 
        Room room3 = new Room(3346, 140, "Suite");
        room1.Book(); 
        room2.printRoomInfo(); 
        room3.setRate(180); 
        room1.Release(); 
        room1.printRoomInfo(); 
        room3.printRoomInfo();
    }

    }



    public class Room {


    private final int number;
    private final String type;
    private int rate;
    private boolean isBooked;


    public Room(int number, int rate, String type) {

        this.number = number;
        this.type = type;
        this.rate = rate;
        this.isBooked = false; 
    }


    public void Book() {
        System.out.println("Room " + this.number + " has successfully been booked.");
        this.isBooked = true;
    } 

    // This method "releases" the booking on the room by setting the instance variable isBooked to false
    public void Release() {
        System.out.println("Room " + this.number + " has successfully been released.");
        this.isBooked = false;
    }

    // Print basic info about the current Room objects state 
    public void printRoomInfo() {
        String state; 
        if (this.isBooked == true) { 
            state = "Unavailable"; 
        }

        else { 
            state = "Available";
        }

        System.out.println("Room " + this.number + " is a " + this.type + ". Rate is: " + this.rate + "$ per night. " + state + ".");

    }


    public void setRate(int newRate) {
        this.rate = newRate;
    }
    }

【问题讨论】:

  • 您的程序对我来说运行良好,您是否将所有代码放在一个文件中?
  • 是的,每个文件都有一个顶级类。
  • @RobertMoskal 您在一个文件中只能有一个“公共”类。因此,您应该将除“HotelManagement”以外的所有课程设为非公开课程或遵循以下答案。

标签: java class object methods


【解决方案1】:

如果你想把两个类放到一个文件中,你需要:

  1. 在 HotelManagement 类中移动 Room 类声明
  2. 使其成为静态的,例如public static class Room {

所以,最终的结构是这样的:

public class HotelManagement {
    public static void main(String[] args) {
...
    }

    public static class Room {
....
    }
}

【讨论】:

    【解决方案2】:

    您只能在 .java 文件中拥有一个公共类,该类将是具有您的公共静态 void main 的类。同一文件中的其他类不能公开。

    public class HotelManagement {
    
        public static void main(String[] args) {
            Room room1 = new Room(3348, 80, "Single Bed"); 
            Room room2 = new Room(3347, 90, "Double Bed"); 
            Room room3 = new Room(3346, 140, "Suite");
            room1.Book(); 
            room2.printRoomInfo(); 
            room3.setRate(180); 
            room1.Release(); 
            room1.printRoomInfo(); 
            room3.printRoomInfo();
        }
    
        }
    
    
    
         class Room {
    
    
        private final int number;
        private final String type;
        private int rate;
        private boolean isBooked;
    
    
        public Room(int number, int rate, String type) {
    
            this.number = number;
            this.type = type;
            this.rate = rate;
            this.isBooked = false; 
        }
    
    
        public void Book() {
            System.out.println("Room " + this.number + " has successfully been booked.");
            this.isBooked = true;
        } 
    
        // This method "releases" the booking on the room by setting the instance variable isBooked to false
        public void Release() {
            System.out.println("Room " + this.number + " has successfully been released.");
            this.isBooked = false;
        }
    
        // Print basic info about the current Room objects state 
        public void printRoomInfo() {
            String state; 
            if (this.isBooked == true) { 
                state = "Unavailable"; 
            }
    
            else { 
                state = "Available";
            }
    
            System.out.println("Room " + this.number + " is a " + this.type + ". Rate is: " + this.rate + "$ per night. " + state + ".");
    
        }
    
    
        public void setRate(int newRate) {
            this.rate = newRate;
        }
        }
    

    【讨论】:

      【解决方案3】:

      每个源文件只能有一个公共类。所以要让你的代码工作,你需要做一件事

      1. 将每个公共类放在一个单独的源文件中。无论包是什么,都可以从任何地方访问公共类。
      2. 删除publicRoom 的访问修饰符

      另一种方法是将Room 类设为静态并将其放入HotelManagement 类中。但我建议您在非常擅长基本概念的情况下学习 Java 中的静态嵌套类和内部类等高级概念。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多