简体   繁体   English

Java:是否允许向java.awt.image添加自定义类?

[英]Java: allowed to add custom class to java.awt.image?

I'm pretty shure the answer is somewhere, but either im lacking the correct key-words or simply should get some sleep, but here it is ;~) : 我很确定答案在某个地方,但是要么我缺少正确的关键字,要么应该睡一会儿,但这是;〜):

I need to extend BufferedImage and the only way to do this w/o MacGyvering the whole class would be to let the new class rest in java.awt.image, because it contains a package-visible-only field i need to access. 我需要扩展BufferedImage,并且不进行MacGyvering整个类的唯一方法是让新类驻留在java.awt.image中,因为它包含我需要访问的仅包可见字段。 Normaly, there would be a reason to dont to such a thing, but if you check the source, you'll see that this is not true in this special case. 通常,有理由不做这种事情,但是如果您检查源代码,您会发现在这种特殊情况下,这是不正确的。

So I tested it, it compiles and it seems to run. 所以,我测试了它,它编译,它似乎运行。

But can i rely on it? 但是我可以依靠它吗? Is it allowed? 可以吗 Is there anything that could prevent me from adding classes to java's predefined system packages? 有什么可以阻止我将类添加到Java的预定义系统包中的吗? Or is this simply the correct way to do it and no one told me? 还是这仅仅是正确的方法,没有人告诉我?

UPDATE 更新

The answeres already provided stated the possibility of legal concerns (if doing something correctly means breaking some laws than my default solution frequently is beeing uninterested in those laws;-) and the possibility of changed behaviour in the super-class (very unlikely in that case). 已经提供的答案表明存在法律问题的可能性(如果做正确的事意味着违反某些法律,而不是我的默认解决方案经常对那些法律不感兴趣;-)以及超类行为发生改变的可能性(在这种情况下极不可能) )。 If that's all, i'd just do it that way… 如果仅此而已,我会那样做...

UPDATE2 更新2

Great! 大! Java is a protected package name… So i really have to MacGyver this… Java是受保护的程序包名称……所以我真的必须要MacGyver这样……

UPDATE3 更新3

I was just wondering why i cant get the property hash-keys. 我只是想知道为什么我无法获取属性哈希键。 The fn getPropertyNames is documented just so: Returns an array of names recognized by getProperty(String) … let's look into the sourcecode: fn getPropertyNames的记录如下: 返回由getProperty(String)识别的名称数组 …让我们看一下源代码:

public String[] getPropertyNames() {
     return null;
}

Best code i've ever read… 我读过的最好的代码…

Ok, here's how I would handle this: 好的,这是我的处理方式:

public class MyBi extends BufferedImage
{
    private final BufferedImage realBufferedImage;

    public MyBi(BufferedImage bi)
    {
        super(0, 0, TYPE_INT_ARGB);
        realBufferedImage = bi;
    }

    // Add methods for managing your extra data

    // For every method in BufferedImage, override it like so:
    public void setData(Raster r)
    {
        realBufferedImage.setData(r);
    }
}

This is tedious to do by hand, but NetBeans has support for delegating. 手工完成这很繁琐,但是NetBeans支持委派。 Press Alt Insert , select Delegate Method... , select realBufferedImage as the field in the left panel, check BufferedImage in the right panel and click OK . Alt 插入 ,选择Delegate Method... ,选择realBufferedImage作为左侧面板中的字段,在右侧面板中检查BufferedImage并单击OK Instant delegation. 即时委派。

This should work because, although the fields of BufferedImage are at package scope, nothing else in the package accesses them. 这应该起作用,因为尽管BufferedImage的字段在包范围内,但包中的其他任何内容都无法访问它们。

You don't "add" to java.awt.Image. 您无需“添加”到java.awt.Image。 In your own package you extend the class and do as you please. 在您自己的包中,您可以扩展该类并随心所欲。

Sometimes, in open source projects, you can submit your class back to the project and have it become part of the project. 有时,在开源项目中,您可以将类提交回项目,并使其成为项目的一部分。

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

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