简体   繁体   English

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

[英]Abstract class inherits method and attributes from another class

I'm trying to make a class called RoomBase and it has an abstract method and two attributes, I need to make my program work so the RoomBase class inherits everything that the getRoomDetailsFromUser() does in the Room class. 我正在尝试创建一个名为RoomBase的类,它具有一个抽象方法和两个属性,我需要使我的程序正常工作,以便RoomBase类继承getRoomDetailsFromUser()Room类中所做的所有操作。

RoomBase class: RoomBase类:

public abstract class RoomBase {

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

Room class: 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;
    }
}

I think you have to think a little bit more on the design than the implementation, let's see for example what the class hierarchy will be, and most important, why. 我认为您必须在设计上比实现多思考一点,例如,让我们看看类的层次结构是什么,最重要的是为什么。

Does it makes any sense that the RoomBase extends or gets logic from the Room class, should not be the other way around ? RoomBase扩展或从Room类获取逻辑是否有意义,不应该反过来吗? so is it really Room Base a base class? 那么,Room Base真的是基础课吗? Now if getRoomDetailsFromUser method's logic should be a common or base code it would not be instead on the base class but in the specific class (Room in this case), and to finish, do you really need an abstract class here at all? 现在,如果getRoomDetailsFromUser方法的逻辑应该是通用代码或基本代码,则不是在基类上而是在特定类(在本例中为Room)上,并且要结束,您是否真的需要一个抽象类? or does a regular base class fulfill you needs ? 还是普通的基础课程可以满足您的需求?

Check the design and let me know if I can help you any further. 检查设计,让我知道是否能为您提供进一步的帮助。

The question is a bit unclear, do you want to instantiate RoomBase? 问题有点不清楚,您要实例化RoomBase吗? Or should it stay abstract. 还是应该保持抽象。 Should Room inherit from RoomBase? Room应该从RoomBase继承吗? Seems so as the names hint this and Roob is not abstract. 好像名称所暗示的那样,Roob不是抽象的。

To answer your question as best as I can guess: The only way I see is to make RoomBase extend Room. 尽我所能回答您的问题:我看到的唯一方法是使RoomBase扩展Room。 With this you would inherit everything in Room, not only the method. 这样,您将继承Room中的所有内容,而不仅是方法。 But only inheriting the method will no work, as this getRoomDetailsFromUser() uses variable only defined in Room, but not in RoomBase. 但是仅继承该方法无效,因为此getRoomDetailsFromUser()仅使用在Room中定义的变量,而在RoomBase中不使用。

If I ignore the names you have given your classes, then the answer is simple: 如果我忽略您为课程指定的名称,那么答案很简单:

I need to make my program work so the RoomBase class inherits everything that the getRoomDetailsFromUser() does in the Room class. 我需要使程序正常工作,以便RoomBase类继承getRoomDetailsFromUser()在Room类中所做的所有事情。

 public abstract class RoomBase extends Room {
 }

Of course you can't instantiate an abstract class, so to make use of this class you'd need to create a concrete subclass of RoomBase : 当然,您不能实例化一个抽象类,因此要使用该类,您需要创建一个RoomBase的具体子类:

 public class BathRoom extends RoomBase {
 }

 RoomBase someBathRoom = new BathRoom();

Although this answers your question exactly, it's an unusual class hierarchy given the names. 尽管这完全可以回答您的问题,但是给定名称是一个不寻常的类层次结构。 One possible more common pattern, would be: 一种可能的更常见的模式是:

 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
 }

Note also that many Java programmers today use inheritance in moderation -- preferring composition . 还要注意,当今许多Java程序员都在适度中使用继承- 倾向于使用组合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM