繁体   English   中英

下传图片的问题

[英]Issue with downcasting Picture

我陷入与Java的垂头丧气的问题。

这是情节: MyPicture类扩展了抽象类BufferedImage 关键是要向BufferedImage添加一些方法。 然后,我有了MyWindow类,它设置了一个用户友好的窗口。 在这个类中,我要加载的图片MyPic ,它在复制MyPic_filtered ,使用中所使用的方法MyPictureMyPic_filtered终于显示MyPicMyPic_filtered在分离的窗口(不过这最后一部分是OK ^^)。 我不知道应该为MyPicMyPic_filtered使用哪种类型。 我尝试将其投射为正确的类型,但可以构建但无法运行。

这是代码:

//Loading the picture
BufferedImage MyPic = ImageIO.read(new File(URL)); //URL is a string
//Copy the picture 
MyPicture myPic_filtered = myPic;               
//Use the method from MyPicture
myPic_filtered.method_from_MyPicture();`

有人可以帮我吗?

尝试将基类实例传递给扩展实例时,可以添加一个caster,例如:

MyPicture myPic_filtered = (MyPicture)myPic; 

然后,您可以使用关键字访问“ myPic”。


或者,也许您不需要扩展BufferedImage。 只需将bufferedImage视为实例变量,例如:

class MyPicture { 
    BufferedImage bi;
    //other variables
    ......;

    public MyPicture(BufferedImage input) {
          this.bi = input;
    }

    public BufferedImage method_from_MyPicture() {
         //Do something with bi and output
         ........
    }
}

不知道哪种结构更好。 但这两种方法都能解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM