抽象类从另一个类继承方法和属性

时间:2017-04-05 20:46:05

标签: java abstract-class abstract-methods

我正在尝试创建一个名为RoomBase的类,它有一个抽象方法和两个属性,我需要让我的程序工作,以便RoomBase类继承getRoomDetailsFromUser()的所有内容} Room类。

RoomBase上课:

public abstract class RoomBase {

    public abstract void getRoomDetailsFromUser();
    private int roomNumber;
    private int NumberOfSeats;
}

Room上课:

public abstract class Room implements Bookable {
    Scanner input = new Scanner(System.in);
    private static int roomNumber;
    private static int numberOfSeats;
    private static String reservedBy = "";
    private static boolean reserved;
    private static boolean hasSmartBoard;

    /**
     * Get the attribute values from the user.
     */

    public Room(int roomNumber) {
        this.roomNumber = roomNumber;

    }

    public void getRoomDetailsFromUser() {

        System.out.print("Enter number of seats: ");
        numberOfSeats = input.nextInt();
        input.nextLine();
        System.out.print("Does this classroom have a smart board? (Y/N)");
        hasSmartBoard = input.nextLine().equalsIgnoreCase("y");

    }

    public boolean isHasSmartBoard() {
        return hasSmartBoard;
    }

    public void setHasSmartBoard(boolean hasSmartBoard) {
        this.hasSmartBoard = hasSmartBoard;
    }

    public static int getNumberOfSeats() {
        return numberOfSeats;
    }

    public void setNumberOfSeats(int numberOfSeats) {
        this.numberOfSeats = numberOfSeats;
    }

    public String getReservedBy() {
        return reservedBy;
    }

    public void setReservedBy(String reservedBy) {
        this.reservedBy = reservedBy;
    }

    public boolean isReserved() {
        return reserved;
    }

    public void setReserved(boolean reserved) {
        this.reserved = reserved;
    }


    public int getRoomNumber() {
        return roomNumber;
    }

    /**
     * Update the room to reserved and get the reserved by.
     */

    public void reserveThisRoom(){
        this.reserved = true;
        System.out.println("Enter the name of the person reserving this room: ");
        reservedBy = input.nextLine();
    }

     /**
     * Update the room to not reserved and clear the reserved by.
     */

    public void releaseThisRoom(){
        this.reserved = false;
        reservedBy = "";
        System.out.println("Room has been released\n");
    }

    public String toString() {
        String output = "\n\n******************************"
            + "\nRoom Number: " + roomNumber
            + "\nNumber of Seats: " + numberOfSeats
            + "\nReserved By: " + reservedBy
            + "\nReserved: " + reserved
            + "\nSmart Board: "+ hasSmartBoard;
        return output;
    }
}

3 个答案:

答案 0 :(得分:0)

我认为你必须在设计上多考虑实现,让我们看看例如类层次结构将是什么,最重要的是,为什么。

RoomBase扩展或从Room类获取逻辑是否有任何意义,不应该相反?所以它真的是房间基地基类吗?现在,如果getRoomDetailsFromUser方法的逻辑应该是公共或基本代码,它将不会在基类上而是在特定类(在本例中为Room)中,并且为了完成,你真的需要一个抽象类在这里所有?或者常规基类能满足您的需求吗?

检查设计并告诉我是否可以为您提供更多帮助。

答案 1 :(得分:0)

问题有点不清楚,你想实例化RoomBase吗?或者它应该 保持抽象。 Room应该继承RoomBase吗?似乎这个名字暗示了这一点,Roob并不是抽象的。

尽我所能地回答你的问题: 我看到的唯一方法是让RoomBase扩展Room。有了这个,您将继承Room中的所有内容,而不仅仅是方法。但是只继承该方法将无效,因为此getRoomDetailsFromUser()使用仅在Room中定义的变量,但不在RoomBase中定义。

答案 2 :(得分:0)

如果我忽略了你给你的课程的名字,那么答案很简单:

  

我需要让我的程序工作,以便RoomBase类继承getRoomDetailsFromUser()在Room类中所做的一切。

 public abstract class RoomBase extends Room {
 }

当然你不能实例化一个抽象类,所以要使用这个类,你需要创建一个RoomBase的具体子类:

 public class BathRoom extends RoomBase {
 }

 RoomBase someBathRoom = new BathRoom();

虽然这完全回答了你的问题,但鉴于这些名称,这是一个不寻常的类层次结构。一种可能更常见的模式是:

 public interface Room extends Bookable {
      method signatures that Rooms have
 }

 public abstract class RoomBase implements Room { 
      methods common to all rooms go here 
 }

 public class BathRoom extends RoomBase {
      methods implementing abstract methods from RoomBase
      methods unique to BathRoom
 }

另请注意,如今许多Java程序员都在审核中使用继承 - preferring composition