首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

难道这个是多态?解决办法

2012-04-26 
难道这个是多态?[codeJava][/code]interface Inter{void method()}class Test{static class Demo implem

难道这个是多态?
[code=Java][/code]interface Inter  
{  
  void method();  
}  
class Test  
{  
  static class Demo implements Inter  
  {  
  public void method()  
  {  
  System.out.println("哈哈哈哈");  
  }  
  }  
  static Inter function()  
  {  
  return new Demo();  
  }  
}  
class Demodemo7  
{  
  public static void main(String[] args)  
  {  
  Test.function().method();  
  }  
}  


---------------------------------------------------
 static Inter function()  
 {  
  return new Demo();  
 }  

我想问下 为什么Inter 作为返回值也能编译通过? Demo不是只是实现了Inter 又没有继承Inter


[解决办法]
这种用法很多见 啊,面向接口编程嘛,比如说
List getList(){
return new ArrayList();
}
实现也是继承的一种啊

热点排行