从类扩展构造函数
我有一个扩展 JList 的类,但我不知道如何使 JList 构造函数在我的类中工作。我知道这可以使用 JList 构造函数来实现:
DefaultListModel model = new DefaultListModel();
JList list = new JList(model);
相反,如果我对我的类执行相同的操作:
DefaultListModel model = new DefaultListModel();
DatabaseJList lista = new DatabaseJList(model);
IDE 建议我删除参数以匹配 DatabaseJList。
当我扩展它时,不应该将 Jlist 构造函数继承到我的类吗?
感谢您的帮助;)
I have a class that extends JList and I don't know how to make the JList constructors work in my class. I know that this can be made with JList constructor:
DefaultListModel model = new DefaultListModel();
JList list = new JList(model);
Instead, if I do the same with my class:
DefaultListModel model = new DefaultListModel();
DatabaseJList lista = new DatabaseJList(model);
The IDE suggest me to remove argument to match DatabaseJList.
Aren't supposed to be Jlist constructors inherited to my class when I extend it?
Thanks for the help ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不可以。您必须显式定义构造函数。您想要“继承”的任何构造函数都必须在子类中定义,并且很可能必须使用相同的参数调用
super(..)
。No. You must define constructors explicitly. Any constructor that you want to "inherit" must be defined in the subclass, and most likely has to call
super(..)
with the same arguments.构造函数不是继承的(至少不是您所想的那种意义上的)。您必须显式编写这样的构造函数:
Constructors are not inherited (at least, not in the sense that you're thinking of). You must explicitly write such a constructor:
您可以使用在类中使用与超类相同的参数定义构造函数来在类的构造函数中调用超类的构造
函数。然后只需使用 super() 调用超类构造函数,然后继续进行初始化
记住,super() 应该位于构造函数的第一行
You can call the superclass's constructor in your class's constructor using
Define a constructor in your class with the same parameters as the super classes'. Then simply use super() to call the superclass constructor and then continue to do your initializations
Remember, super() should be on the first line of your constructor
我认为你需要把 super(model);在您的 DatabaseJList 构造函数中
I think you need to put super(model); in your DatabaseJList constructor