Java示例输出程序

时间:2015-09-20 10:37:16

标签: java

我用Java编写了一个带有2个接口和类的程序。

接口都具有相同的方法名称。

在主类中,我实现了两个接口并调用了方法。 我想知道调用哪种接口方法......

以下是示例代码: -

  java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
        at android.widget.ListView.dispatchDraw(ListView.java:3307)
        at android.view.View.draw(View.java:15174)
        at android.widget.AbsListView.draw(AbsListView.java:4318)
        at android.view.View.updateDisplayListIfDirty(View.java:14096)
        at android.view.View.getDisplayList(View.java:14119)
        at android.view.View.draw(View.java:14895)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
        at android.view.View.draw(View.java:15174)
        at android.view.View.updateDisplayListIfDirty(View.java:14096)
        at android.view.View.getDisplayList(View.java:14119)
        at android.view.View.draw(View.java:14895)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
        at android.view.View.updateDisplayListIfDirty(View.java:14091)
        at android.view.View.getDisplayList(View.java:14119)
        at android.view.View.draw(View.java:14895)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
        at android.view.View.updateDisplayListIfDirty(View.java:14091)
        at android.view.View.getDisplayList(View.java:14119)
        at android.view.View.draw(View.java:14895)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
        at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1089)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
        at android.view.View.updateDisplayListIfDirty(View.java:14091)
        at android.view.View.getDisplayList(View.java:14119)
        at android.view.View.draw(View.java:14895)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
        at android.view.View.updateDisplayListIfDirty(View.java:14091)
        at android.view.View.getDisplayList(View.java:14119)
        at android.view.View.draw(View.java:14895)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
        at android.view.View.updateDisplayListIfDirty(View.java:14091)
        at android.view.View.getDisplayList(View.java:14119)
        at android.view.View.draw(View.java:14895)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
        at android.view.View.updateDisplayListIfDirty(View.java:14091)
        at android.view.View.getDisplayList(View.java:14119)
        at android.view.View.draw(View.java:14895)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
        at android.view.View.updateDisplayListIfDirty(View.java:14091)
        at android.view.View.getDisplayList(View.java:14119)
        at android.view.View.draw(View.java:14895)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
        at android.view.View.updateDisplayListIfDirty(View.java:14091)
        at android.view.View.getDisplayList(View.java:14119)
        at android.view.View.draw(View.java:14895)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3407)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3201)
        at android.view.View.draw(View.java:15174)
        at android.widget.FrameLayout.draw(FrameLayout.java:592)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2695)
        at android.view.View.updateDisplayListIfDirty(View.java:14096)
        at android.view.View.getDisplayList(View.java:14119)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:266)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:272)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:311)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2531)
        at android.view.

4 个答案:

答案 0 :(得分:2)

public interface A {
    void print();
}

public interface B {
    void print();
}

在上面的代码中,接口AB是抽象接口,因为声明了一些/所有方法但未定义。

因此,在你的C课程中,你没有调用这两个中的任何一个(这很简单,你怎么能调用一个从未定义过的方法?)。你正在做的是定义print方法(因此给它一个体),然后调用它(在main中)。

答案 1 :(得分:0)

你调用了类的方法

 public void print() {
    // TODO Auto-generated method stub
    System.out.println("sample");
} 

答案 2 :(得分:0)

这是C的{​​{1}}。 print()方法没有实现,也没有调用本身,它是被调用的实现之一。

您可能希望研究所谓的静态绑定和动态绑定。在这种情况下编译器&运行时一切都在编译时,因此使用前者。基本上,我们将静态地确定您要调用的方法/实现是abstract的{​​{1}}。

动态绑定仍然意味着调用方法的具体实现,因此它本身不会选择接口方法,而是选择要在运行时调用哪个方法。

答案 3 :(得分:0)

首先以接口方法将调用的方式实现的接口,如果另一个接口与第一个接口相邻,则具有相同的方法名称,根本不会调用。