Java - 在另一个实例中添加抽象类的实例

标签 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.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() 是抽象的,无法实例化!

我该如何解决这个问题?为大家干杯:)

最佳答案

疯狂猜测,因为这取决于你的代码......试试这个(更不用说 Piotr 所说的了)

profilo_2.add(profilo_2.createButton());

关于Java - 在另一个实例中添加抽象类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4155625/

相关文章:

java - 通配符符号的主要意图或目的?延伸 T'

java - Morphia中无需嵌入类直接加载嵌入类

java - Jar 不在 linux 上执行

java - 支柱 2 : parameters between Actions

types - Ocaml : Passing constructor type between modules

java - 接口(interface)和抽象类的属性继承 - Java

java - Lambda 表达式的 Eclipse Java 8 自动完成

java - 如何修复 "syntax error on token abstract"错误?

java - 如何在一个类中创建对象并将它们保存在另一个类的数组中?

java - 创建抽象 Activity 类