日期:2014-05-20  浏览次数:20771 次

奇怪了,抽象方法居然可以被实际调用?
函数原型: public abstract boolean drawImage(Image img, int x, int y,
int width, int height, 
ImageObserver observer);

public static BufferedImage convert(Image image,int scale){
int width=image.getWidth(null),height=image.getHeight(null);
  BufferedImage bufimg=new BufferedImage(width/scale,height/scale,
BufferedImage.TYPE_INT_RGB);
bufimg.getGraphics().drawImage(image,0,0,width,height,null); //为什么可以被调用。。。。。
return bufimg;
}

------解决方案--------------------
bufimg.getGraphics()得到的是个实例
------解决方案--------------------
在内部实现了
------解决方案--------------------
肯定有实现的地方,就是你没有注意,找找吧

------解决方案--------------------
这是多态。

在这个类的子类中实现了这个抽象方法。


------解决方案--------------------
相信编译器吧,编译能通过就说明一定有实现过这个方法,只是你没找到而已
------解决方案--------------------
bufimg.getGraphics() 返回的对象一定实现了drawImage方法

你再仔细看看。
------解决方案--------------------
接口能用来声明!
接口也能用来创建吗?!!
------解决方案--------------------
探讨
这个实例是Graphics2D的对象,
但Graphics2D这个类本身并没有实现drawImage这个方法。。。

------解决方案--------------------
探讨
这个子类能在API帮助里找到吗?

------解决方案--------------------
System.out.println(bufimg.getGraphics().getClass());
看看实际的类型,其实没有必要理会其具体实现类型。
------解决方案--------------------
顶一楼和十一楼,让我想起了很多东西!
------解决方案--------------------
探讨
引用:
这个子类能在API帮助里找到吗?

未必,有些只是内部实现(非public的),返回引用的。
多了解一下多态即可。