public interface TT {

}
class D implements TT{
public void add(int i,int j)
{
System.out.println("两数之和为:"+(i+j));
}
public static void main(String[] args)
{
TT a = new D();

}
}TT借口的一变量为什么能new出D的对象;
既然new出D的对象为什么不能调用D这个类里面的add()方法
麻烦说明下。谢谢了

解决方案 »

  1.   


    interface TT { //去掉public 
        
    }
    public class D implements TT{ //加public
        public void add(int i,int j)
        {
            System.out.println("两数之和为:"+(i+j));
        }
        public static void main(String[] args)
        {
            TT a = new D(); //这里其实是D向上转型为TT,由于TT本身没有add接口,所以无法直接调用add.
             //((bb)x).add(1,2);这样便可以调用add方法了
        }
    }
      

  2.   

    因为变量a的类型是TT,TT里没有add()方法如果你使用D a = new D();就可以调用add()方法了
      

  3.   

    //((bb)x).add(1,2); 应该写成 ((D)a).add(1,2);
      

  4.   

    你得复习接口方面的知识了。http://www.cnblogs.com/healerkx/articles/1233252.html
      

  5.   

    public interface TT {
        public void add(int i,int j);
    }
    class D implements TT{
        public void add(int i,int j)
        {
            System.out.println("两数之和为:"+(i+j));
        }
        public static void main(String[] args)
        {
            TT a = new D();
            //想要实现调用下面的方法,应该
             a.add(3,4);//这样就出结果来,继续努力
        }
    }
      

  6.   

    因为变量a的类型是TT,TT里没有add()方法如果你使用D a = new D();就可以调用add()方法了要不你就强制转换一下类型然后调用:((D)a).add(1,2);即可