如何在java中访问另一个类的构造函数?

如何在java中访问另一个类的构造函数?,java,inheritance,constructor,overloading,Java,Inheritance,Constructor,Overloading,我有一个超类叫做“问题”。我有两个派生自它的子类,称为“QuestionSA”(简短答案)和“QuestionTF”(真/假) 这就是问题A的样子: public class QuestionSA extends Question { private static char[] givenAnswer; // ======================================================== // Name: constructor // Input: the

我有一个超类叫做“问题”。我有两个派生自它的子类,称为“QuestionSA”(简短答案)和“QuestionTF”(真/假)

这就是问题A的样子:

public class QuestionSA extends Question {

private static char[] givenAnswer;

// ========================================================
// Name: constructor
// Input: the type of question, the level,
// the question and answer, as strings
// Output: none
// Description: overloaded constructor that feeds data
// ========================================================
QuestionSA(String type, String level, String question, String answer) {
    this.type = type;
    this.level = level;
    this.text = question;
    this.answer = answer;
}
我需要从QuestionTF访问QuestionSA中的构造函数。我已经在C++中这样做了:

QuestionTF::QuestionTF(string type, string level, string question, string answer)
: QuestionSA(type, level, question, answer) {}

如何在Java中实现这一点?

如果
QuestionTF
QuestionSA
的子类,您可以使用
super
关键字访问它的构造函数

 QuestionTF(String type, String level, String question, String answer) {
     super(type, level, question, answer);
 }

否则,您不能使用父类构造函数的子类来创建新对象。

在创建类的对象期间,JVM正在调用构造函数。因此,如果要调用类的构造函数,需要创建该类的对象。
如果您想调用子类的父类的构造函数,可以调用子构造函数的第一行中的Sub()。正如QuestionSA所理解的,C++不是初始化C++中的初始化列表的一个超级类,它表示是子库,不是吗?QuestionSA和QuestionTF都是同一级别的派生问题类。QuestionSA不是QuestionTF的超类,反之亦然that@Dima如果QuestionSA不是QuestionTF的超类,那么你的C++就被破坏了,毫无意义。在这种情况下,不可能知道您想要的Java等价物是什么。您还可以添加类QuestionTF吗?我必须编辑以澄清。我知道这个命名表明这是子类,但我必须仔细检查。为什么
givenaswer
是静态的?那么你的意思是说,如果我在QuestionTF类中创建QuestionSA类的实例,那么我为QuestionTF的构造函数传递的任何参数都将传递到QuestionSA的构造函数中?这就是我在C++中完成这一点的全部原因。