日期:2014-05-20 浏览次数:20895 次
public interface A {
void sample();
}
public class B implements A {
@Override
public void sample() {
System.out.println("B implements A");
}
public static void main(String[] args){
A a=new B();
a.sample();
}
}
------解决方案--------------------
并不是通过接口调用实现类,而是调用实现类的方法
//测试类
public class TestInterface
{
public static void main(String[] args)
{
//调用
A aa = new B(); //实际上aa指向的是实现类B的一个实例,
aa.getString();//调用方法
}
}
//定义接口A
interface A {
public String getString();
}
//B类实现了接口A
class B implements A{
//实现了接口中的方法
public String getString()
{
return "hello";
}