我觉得是不带的,但我无法解释下面的程序了?接口调用了不属于它的方法啊!
interface A{

}
public class B implements A{
public static void main(String [] args){
A a = new B();
System.out.println(a.toString());
}
}

解决方案 »

  1.   

    接口的实现类总是继承自Object,所以调用Object的方法是没错的
      

  2.   

    接口回调是指:可以把使用某一接口类创建的对象的引用赋给该接口声明的接口变量,那么该接口变量就可以调用被类实现的接口中的方法。
    定义说是可以调用被类实现的接口中的方法!但接口中没有Object中的方法啊?比如说toString()
      

  3.   

    猎人:是不能调用实现类中新增的方法的,这样多态性就得不到保证了,你可以试一下下面的代码,是会报错的
    interface A{}
    public class B implements A{
            public void xxx(){};
    public static void main(String [] args){
    A a = new B();
                      a.xxx();
    System.out.println(a.toString());
    }
    }
      

  4.   

    恩,是不能,是我记错了,Sorry啊
      

  5.   

    这个就不知道了,可能是Object中的方法特殊吧
      

  6.   

    应该是Class的原因
    Class 类的实例表示正在运行的 Java 应用程序中的类和接口,而Class 继承Object,所以接口也天生既有Object类的方法
      

  7.   

    A a = new B();
    得从这里分析起,A是接口  a 是引用,   new B() 是对象.
    a这个引用, 指向一个实现了 A接口的对象, 也就是 B()
    因为所有的对象都继承Object, 所以任何接口的引用, 指向的对象一定会是Object
    所以 a.equals()  等绝对是合法的调用, 所以java默认允许这么写.
    就好象默认import java.lang.*;  不需要你再声明
      

  8.   

    接口就是抽象类的抽象
    实质还是抽象类不是
    JDK默认加载java.lang包
    然后大家继续争
      

  9.   

    呵呵,回去再加你吧,现在在上班,只能用msn:[email protected]。我看你回了很多贴哦!应该很不错啊,不要谦虚撒!
      

  10.   

    我觉得是不带的,但我无法解释下面的程序了?接口调用了不属于它的方法啊!
    interface A{}
    public class B implements A{
    public static void main(String [] args){
    A a = new B();
    System.out.println(a.toString());
    }
    }  楼主要弄清一个问题,就是a是对象,所有对象都继承Object,所以a.to
    String()方法一点问题都没有,这跟接口无关.
      

  11.   

    上面的代码可以写成这样,interface A
    {
    }public class B extends Object implements A
    {
    public static void main(String[] args)
    {
    A a = new B();
    System.out.println(a.toString());
    }
    }那么A就相当于B的父类,父类可以调用子类的方法这不是正是多态的体现吗?
      

  12.   

    楼主,去看Sun的官方文档TJLS(The Java Language Specification)吧!其中第9章9.2节关于接口有这么一段话:If an interface has no direct superinterfaces, then the interface implicitly
    declares a public abstract member method m with signature s, return type r,
    and throws clause t corresponding to each public instance method m with
    signature s, return type r, and throws clause t declared in Object, unless a
    method with the same signature, same return type, and a compatible throws
    clause is explicitly declared by the interface. It is a compile-time error if the
    interface explicitly declares such a method m in the case where m is declared to
    be final in Object.好好理解一下就明白了。