Java - 扩展类

标签 java inheritance

我有一个 Meeting 类,我想创建一个扩展 Meeting 的 BusinessMeeting 类。

这是我的 session 类:

public class Meeting {

private String name;
private String location;
private int start, stop;

public Meeting(String name, String location, int start, int stop) {

    this.name = name;

    this.location = location;

    this.start = start;

    this.stop = stop;

}

这是我的 BusinessMeeting 类:

public class BusinessMeeting extends Meeting {

    public BusinessMeeting() {

        super();

    }

}

在我的 BusinessMeeting 类中,我收到错误:

类 session 中的构造函数 session 不能应用于给定类型; 必需:字符串、字符串、整数、整数 发现:没有参数

我不太确定为什么会收到该错误消息。 BusinessMeeting 类不应该从我的 Meeting 类继承所有变量吗?

最佳答案

public class Meeting {
    public Meeting(String name, String location, int start, int stop) {

        this.name = name;

        this.location = location;

        this.start = start;

        this.stop = stop;

    }
}

现在假设您想使用 businessId 扩展您的 BusinessMeeting 类。

public class BusinessMeeting {
    private Integer businessId;

    public BusinessMeeting(String name, String location, int start, int stop, int business) {
        // because super calls one of the super class constructors(you can overload constructors), you need to pass the parameters required.
        super(name, location, start, stop);
        businessId = business;
    }
}

关于Java - 扩展类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19501638/

相关文章:

c++ - 我应该避免从 std::iostream 继承吗?

java - Activity 是 MainFragmentListener 的实例,是真的吗?

java - Maven 项目导入向导不显示子项目 pom

java - 使用 java 8 流式传输 2 循环并从内循环返回外循环对象

java - 抽象方法和带有 UnsupportedException 的方法哪个更好?

识别父类(super class)构造函数的 Javadoc

java - Quarkus/microprofile读取pom.xml属性

java - 在Java中读取32位无符号整数中的位值

java - 如何从 jQuery ajax 成功函数返回一个数组并在循环中使用它?

python - 如何从子类中的父表单中删除字段?