Java 从类扩展构造函数
我有一个扩展JList的类,我不知道如何使JList构造函数在我的类中工作。我知道这可以通过JList构造函数实现:Java 从类扩展构造函数,java,inheritance,constructor,jlist,Java,Inheritance,Constructor,Jlist,我有一个扩展JList的类,我不知道如何使JList构造函数在我的类中工作。我知道这可以通过JList构造函数实现: DefaultListModel model = new DefaultListModel(); JList list = new JList(model); class DatabaseJList extends JList { public DatabaseJList(ListModel model) { super(model); // Cal
DefaultListModel model = new DefaultListModel();
JList list = new JList(model);
class DatabaseJList extends JList {
public DatabaseJList(ListModel model) {
super(model); // Call through to the super-class constructor
}
...
}
相反,如果我在课堂上也这么做:
DefaultListModel model = new DefaultListModel();
DatabaseJList lista = new DatabaseJList(model);
IDE建议我删除参数以匹配数据库列表
当我扩展类时,不应该是继承到类的Jlist构造函数吗
谢谢你的帮助;) 否。必须明确定义构造函数。任何要“继承”的构造函数都必须在子类中定义,并且很可能必须使用相同的参数调用
super(..)
。否。必须明确定义构造函数。任何要“继承”的构造函数都必须在子类中定义,并且很可能必须使用相同的参数调用super(..)
。您可以使用
super(...)
在类中定义一个构造函数,其参数与超级类的参数相同。然后简单地使用super()调用超类构造函数,然后继续进行初始化
记住,super()应该位于构造函数的第一行您可以使用
super(...)
在类中定义一个构造函数,其参数与超级类的参数相同。然后简单地使用super()调用超类构造函数,然后继续进行初始化
请记住,super()应该位于构造函数的第一行构造函数不是继承的(至少不是您所想的那样)。必须显式编写这样的构造函数:
DefaultListModel model = new DefaultListModel();
JList list = new JList(model);
class DatabaseJList extends JList {
public DatabaseJList(ListModel model) {
super(model); // Call through to the super-class constructor
}
...
}
构造函数不是继承的(至少,不是您想象的那样)。必须显式编写这样的构造函数:
DefaultListModel model = new DefaultListModel();
JList list = new JList(model);
class DatabaseJList extends JList {
public DatabaseJList(ListModel model) {
super(model); // Call through to the super-class constructor
}
...
}
我认为你需要把超级(模型);在您的数据库列表构造函数中,我认为您需要放置super(model);在您的DabaseJeList构造器中,您应该使用接口<代码> ListMead < /C> >而不是类<代码> Debug TrimeStudio(即,程序到接口)。@史提夫:啊,我没想到