English 中文(简体)
在整个方案中使用基于一开始价值的某些物体
原标题:Use a certain objects throughout the program based on a value at start
  • 时间:2011-02-26 17:09:38
  •  标签:
  • java
  • oop

I m 撰写节目,在节目开始时,我视图像的深度。 一旦我有线性深度,就会保证总有8、16或32个轨道。 一旦有了比喻的深度,我就对图像进行了一些处理,并根据产出制作了一些新形象。 为了制作图像,我需要使用特定类别,IE FloatProcessor ShortProcessor ByteProcess,及其相应的阵列,浮标短[]和沥青。

我想做的是,如果在我需要确定哪一个地方需要使用的话,不要开一个开关或一个bu子。 这三类都属于一类,但即使我以某种方法这样做,我仍不得不回到基类,我仍然不知道哪类东西可以像我所使用。

Edit: What I really want is something along the lines of if(depth == 8) #define type ByteProcessor etc for 16 and 32

问题回答

如何使用基因而不是遗产? 我的 Java是空洞的,因此我用C++来证明:

template<class DataT>
class Foo
{
    public:

        DataT data

        void processData()
        {
            // Do something here
        };
};

如果您仍需要一份通向书,在这一过程中作转录。 数据功能,你仍会避免将其全部置于你的法典之上。 您可以与因果法模式相结合,获取你想要的东西。

顺便说一句,这三人都继承了基类加工剂:

abstract class TypeProcessor {
    public abstract Image ProcessImage(Image input);
}

并且,你有特殊班子:

class ByteProcessor extends TypeProcessor{
    public byte data[];
    public Image ProcessImage(Image input) {
        //do stuff
        return ret;
    }
}

显然,如果你的方案有<代码>TypeProcessor的参考资料,你可以直接查阅data(不做一刀切的打字和 cast)。

适当的解决办法是将需要检索<代码><>数据>/代码>的代码移至类别本身:

class ByteProcessor extends TypeProcessor{
    public byte data[];
    public Image ProcessImage(Image input) {
        //do stuff

        data = whatever;

        FrobData

        Image ret = new Image(data);

        return ret;
    }

    void FrobData() {
        for(i = 0; i < data.length; i++) {
            data[i] = (data[i] + 1) % 64;
        }
    }
}

这显然是一个有争议和非常不正确的例子,但应该给你这个总的想法。

这将引入一些法典上的重复,但我假定,不同类型之间的计算存在足够的差异,不能需要更复杂的解决办法。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签