Java—在另一个实例中添加抽象类的实例
我有一段代码:Java—在另一个实例中添加抽象类的实例,java,abstract,Java,Abstract,我有一段代码: public class Profile extends Container{ public Profile(String value) { Container profilo_1 =new Container(); Container profilo_2 =new Container(); // (1) THIS ADD A BUTTON TO THE MAIN CLASS this.createButt
public class Profile extends Container{
public Profile(String value) {
Container profilo_1 =new Container();
Container profilo_2 =new Container();
// (1) THIS ADD A BUTTON TO THE MAIN CLASS
this.createButton().setLabel("Modifica Profilo");
// (2) NOW I NEED TO ADD A BUTTON INTO THE INSTANCE OF profilo_2 BUT IT FAILS
profilo_2.add(this.createButton());
this.add(profilo_1);
this.add(profilo_2);
}
}
第(2)点失败了,因为它说我将要向这个容器添加一个子容器,但它已经是一个容器的所有者
事实上,如果我这样做:
ILabel label=new ILabel();
profilo_2.add(label);
它告诉我ILabel()是abract,无法实例化
我怎样才能修好它?为大家干杯:)可能,setLabel()
返回一些无法传递到Container::add(…)
的内容。请为容器提供代码可能,setLabel()
返回无法传递到容器::add(…)
的内容。请提供您的容器的代码
尝试更改为
Button button2 = this.createButton();
button2.setLabel("EDIT");
profilo_2.add(button2);
顺便说一句,从我看来,这与抽象类无关
编辑:虽然您说#1“向主类添加一个按钮”,但这是否意味着createButton()会这样做。添加(按钮)?如果是这样,那么您可能应该更改该功能,这样就不会在每次创建按钮时都这样做。尝试更改为
Button button2 = this.createButton();
button2.setLabel("EDIT");
profilo_2.add(button2);
顺便说一句,从我看来,这与抽象类无关
编辑:虽然您说#1“向主类添加一个按钮”,但这是否意味着createButton()会这样做。添加(按钮)?如果是这样的话,那么您可能应该更改该函数,这样就不会在每次创建按钮时都这样做。问题可能是,当您创建带有“this.createButton”的按钮时,该按钮的父项设置为“this”(在此上下文中),当您尝试将其添加到profilo_2中时,它会抛出一个错误。相反,您应该直接在profilo_2上创建按钮,那么父按钮将是正确的(也许您也不必添加()它?问题可能是,当您创建带有“this.createButton”的按钮时,该按钮的父按钮设置为“this”(在此上下文中),并且当您尝试将其添加到profilo_2时,它抛出了一个错误。相反,您应该直接在profilo_2上创建Button,然后父级将是正确的(也许您也不必添加它?)猜测,因为这取决于您的代码。。。试试这个(更不用说Piotr说的了)
profilo_2.add(profilo_2.createButton());
疯狂猜测,因为这取决于您的代码。。。试试这个(更不用说Piotr说的了)
profilo_2.add(profilo_2.createButton());
如果我这样做了,那么是的。createButton();与此相同。添加(按钮);如果我这样做,则是。createButton();与此相同。添加(按钮);是的,但是我怎么做呢?看这个主题,我用更多的信息编辑了它!是的,但是我怎么做呢?看这个主题,我用更多的信息编辑了它!您是否遇到异常,或者它根本不起作用?顺便问一下,您使用的是什么框架?我猜这是某种UI框架?在这种情况下,如果您告诉我们是哪一个,我们可能会帮助您。我解决了:)使用这个profilo_2.createButton().setLabel(“编辑”);它起作用了!!我直接调用容器的实例。谢谢,是的,这是一个UI框架:)p.s.作为信息,我如何在这个stackoverflow评论框中“加粗”我的文本?我不知道bbcode HEHE,但现在(例如)如果我需要访问该对象的方法(调用profilo_2.createButton()时创建到profilo_2中的按钮),我该如何操作?按钮b=profilo_2.createButton();b、 设置标签(“编辑”);b、 setXYZ();等等“类名”按钮取决于createButton()返回的内容。您是得到异常还是根本不起作用?顺便问一下,您使用的是什么框架?我猜这是某种UI框架?在这种情况下,如果您告诉我们是哪一个,我们可能会帮助您。我解决了:)使用这个profilo_2.createButton().setLabel(“编辑”);它起作用了!!我直接调用容器的实例。谢谢,是的,这是一个UI框架:)p.s.作为信息,我如何在这个stackoverflow评论框中“加粗”我的文本?我不知道bbcode HEHE,但现在(例如)如果我需要访问该对象的方法(调用profilo_2.createButton()时创建到profilo_2中的按钮),我该如何操作?按钮b=profilo_2.createButton();b、 设置标签(“编辑”);b、 setXYZ();等等类名按钮取决于createButton()返回的内容。