开发者

How do I create a instance of this Method here?

public LeNo generate (PrintStream stream) {

    prepareOperands(stream);
    LeNo l = (LeNo)left;
    LeNo r = (LeNo)right;

    if (l instanceof NumNo && r instanceof NumNo) {
        return new NumNo((Integer.getInteger(l.name()).intValue()*Integer.getInteger(r.name())));
    }
    if ((l instanceof NumNo && l.name().equals("0"))||(r instanceof NumNo && r.name().equals("0"))) {
        return new NumNo(0); // cut of rest of code here....

Is there a way I can create a new NumNo method without having to create it when I return?

The thing is I still want to开发者_开发百科 return NumNo, but without creating a new NumNo doing it.


It is just return new NumNo(0); that you don't want to create right? Because it is the same every time? If so, you can create a static instance of that object. For example, private static final NewNo ZERO = new NewNo(0);

This is called the flyweight pattern, where you create commonly used instances once.


return NumNo.getClass();

To return a class instead of an instance.

Create an instance with java.lang.Class.newInstance()

for example:

klass = generate(stream);
object = klass.newInstance();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜