【问题标题】:How can I use the reference parameter of the inner abstract class in JAVA如何在JAVA中使用内部抽象类的引用参数
【发布时间】:2013-11-14 06:37:08
【问题描述】:

在外部类中有一个内部接口、一个内部抽象类和一个内部类。

当我调用OuterClass的outerMethod()方法时,

AKindBiz 类的方法只能打印列表的内容。

为什么抽象类(CommonKindBiz)的方法不能打印任何东西?

public class OuterClass {

    public void outerMethod( ) throws Exception{
        ArrayList<String> list = new ArrayList<String>();
        list.add("1111");
        list.add("2222");

        KindBiz biz = new AKindBiz();
        biz.execute(list);
    }

    public interface KindBiz 
    {
        public void execute( ArrayList<String> inputList) throws Exception;

        public void preExec( ArrayList<String> inputList) throws Exception;
        public void exec( ArrayList<String> inputList) throws Exception;
        public void postExec( ArrayList<String> inputList) throws Exception;
    }

    abstract public class CommonKindBiz implements KindBiz 
    {

        public void execute( ArrayList<String> inputList) throws Exception{
                System.out.println("KindBiz.CommonKindBiz.execute ### inputList1 : " + inputList ); // Nothing printed.

                this.preExec(inputList);
                this.exec(inputList);
                this.postExec(inputList);
        }

        public void preExec( ArrayList<String> inputList) throws Exception
        {    
                System.out.println("KindBiz.CommonKindBiz.preExec ### inputList  : " + inputList );  // Nothing printed.
        }   

        public abstract void exec( ArrayList<String> inputList) throws Exception;

        public void postExec( ArrayList<String> inputList) throws Exception
        {           
                System.out.println("KindBiz.CommonKindBiz.postExec ### inputList : " + inputList );  // Nothing printed.
        }    
    }

    public class AKindBiz extends CommonKindBiz
    {
        @Override
        public void exec( ArrayList<String> inputList) throws Exception
        {           
                System.out.println("KindBiz.AKindBiz.exec ### inputList  : " + inputList ); // "1111", "2222" printed.
        }

    }

}

提前谢谢你。

【问题讨论】:

  • “不能打印任何东西”是什么意思?目前还不清楚出了什么问题。
  • @JonSkeet 有了接口、类和抽象类的深度嵌套,这似乎不是设计问题吗? =)
  • @LittleChild:这远非理想,但我想知道 OP 遇到的问题并先解决它,然后再解决其他所有问题......
  • 这甚至无法编译,prinfln 不存在,input 在抽象类中未定义,preExec 等也采用 1 个参数而不是 2..
  • 你在单个参数 inputList 中调用所有函数。但是你的 all 函数有 2 个参数。

标签: java reference abstract inner-classes


【解决方案1】:

我编辑了您的代码以使其能够编译。

我测试了它,所有的行都被打印出来了。我不认为有问题。

【讨论】:

  • 我也测试成功了。也许原始代码有另一个与内部抽象类无关的问题。下次我检查代码后会问另一个问题。谢谢。
【解决方案2】:
System.out.prinfln("KindBiz.CommonKindBiz.execute ### inputList1 : " + inputList ); // Nothing printed.  

那条线似乎是问题所在。它是println()。在您的代码中到处都有prinfln()。替换为println()

更新:
正如 RC 和 subash 所指出的,您的方法声明它们将采用 2 个参数,但您在调用它们时只给它们 1。您需要给他们 2 或更改您的方法的签名。

请使用 IDE。 IDE 可以很容易地指出这些错误(例如参数不匹配),并通过正确描述错误原因和处理方法修复它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    相关资源
    最近更新 更多