简体   繁体   English

重载子类构造函数java

[英]overload subclass constructor java

Is it possible to overload constructors of a subclass because I have been racking my brain and I really can't figure it out. 是否可以重载子类的构造函数,因为我一直在绞尽脑汁,但我真的不知道。 Below is the whole of my code so far and the question. 下面是到目前为止的全部代码和问题。

It is school work but its not graded as we have a concepts exam in a few weeks so I'm trying to get through as many exercises as possible. 这是学校的工作,但由于我们在几周内进行概念考试而未给其评分,因此我正在尝试通过尽可能多的练习。

Exercise 4 – Overload constructors : 练习4 –重载构造函数

We have noticed that the majority of the computer labs have a capacity of 20. 我们注意到,大多数计算机实验室的容量为20。

Write a new constructor for the ComputerLab class that only has one parameter, the room number, and initialise the capacity to 20. We will have now overloaded the constructor for the ComputerLab class, so that there are two ways to create instances of the class. 为ComputerLab类编写一个只有一个参数(房间号)的新构造函数,并将容量初始化为20。现在,我们将重载ComputerLab类的构造函数,因此有两种方法可以创建该类的实例。

Week7.java: Week7.java:

package week7;


public class Week7 {
    /*
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        Room nonTech = new Room("t12", 25, true);
        System.out.println("nonTech");
        System.out.println(nonTech.getRoomNumber());
        System.out.println(nonTech.getCapacity());
        System.out.println(nonTech.hasProjector());

        ComputerLab tech = new ComputerLab("r12", 10, true, "win7");
        System.out.println();
        System.out.println("tech");
        System.out.println(tech.getRoomNumber());
        System.out.println(tech.getCapacity());
        System.out.println(tech.hasProjector());
        System.out.println(tech.getOS());

        // hasProjector == false however it will also return true due 
        //to the override method lower down
        LectureRoom mainhall = new LectureRoom("somthing", 100, false);
        System.out.println();
        System.out.println("mainhall");
        System.out.println(tech.hasProjector());

    }
}

class Room {

    String roomNumber;
    int capacity;
    boolean projection;

    public Room(String rm, int n, boolean p) {
        roomNumber = rm;
        capacity = n;
        projection = p;
    }

    public String getRoomNumber() {
        return roomNumber;
    }

    public int getCapacity() {
        return capacity;
    }

    public boolean hasProjector() {
        return projection;
    }
}

class ComputerLab extends Room {
    // RoomNumber, capacity, projection inherited

    private String os;

    public ComputerLab(String rm, int n, boolean p, String os) {
        super(rm, n, p);
        this.os = os;
    }

    public String getOS() {
        return os;
    }

    public void setOS(String update) {
        os = update;
    }

}

class LectureRoom extends Room {
    // RoomNumber, capacity, projection inherited

    public LectureRoom(String rm, int n, boolean p) {
        super(rm, n, p);
    }

    // Overrides Superclass hasProjector
    public boolean hasProjector() {
        return true;
    }
}

You need to add a constructor for the ComputerLab class that sets the capacity to 20: 您需要为ComputerLab类添加一个构造函数,以将capacity设置为20:

public ComputerLab(String roomNumber) {
    super(roomNumber, 20, false); // is false the correct default value?
    this.os = null; // what is the correct default value?
}

super here invokes the superclass constructor ( Room in this case) so you pass to it the argument you want. super在这里调用超类构造函数(在本例中为Room ),以便将所需的参数传递给它。 In this specific case, your ComputerLab instance is a Room with the room number roomNumber (the argument of the new ComputerLab constructor), a capacity set to 20 and a projection set to false . 在这种情况下,您的ComputerLab实例是一个Room ,房间号为roomNumber (新ComputerLab构造函数的参数),容量设置为20 ,投影设置为false

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

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