设计一个抽象类,以便任何人都可以扩展它并以多态方式使用扩展类(design an abstract class so that any one can extend it and use the ex

编程入门 行业动态 更新时间:2024-10-02 10:41:27
设计一个抽象类,以便任何人都可以扩展它并以多态方式使用扩展类(design an abstract class so that any one can extend it and use the extended class polymorphically)

我想要一个像这样的抽象类:

public abstract Operator { public int[] operands; public Operator(int[] operands) { this.operands = operands; } public abstract int getOperatorResult(); }

而一些运营商从中扩展如下:

public class Add extends Operator { public Add(int[] operands) { super(operands); } public int getOperatorResult() { return operands[0] + operands[1]; } }

还有一个OperatorCreator,它获取类似“1 + 1”的表达式并返回相应的Operator子类(在本例中为Add):

public class OperatorCreator { public static Operator getInstance(String expr) { ... } }

主要问题:我希望让其他人通过扩展Operator类来设计自己的运算符。 并以多态方式使用其运算符。 像这样的东西:

public class Main { public static void main(String[] args) { Operator op = OperatorCreator.getInstance("1-2"); System.out.println(op.getOperatorResult()); } }

我怎样才能做到这一点?

编辑:我知道可以使用反射。 但如果有可能,我宁愿不使用反射。 而且我知道我可以让运算符子类的设计者在运行时调用一个方法来注册她的新类。 但我不想使用这种方法。

谢谢!

I want to have an abstract class like this:

public abstract Operator { public int[] operands; public Operator(int[] operands) { this.operands = operands; } public abstract int getOperatorResult(); }

And some operators to extend from it like:

public class Add extends Operator { public Add(int[] operands) { super(operands); } public int getOperatorResult() { return operands[0] + operands[1]; } }

And an OperatorCreator which gets an expression like "1+1" and returns the appropriate Operator's sub-class(in this case Add):

public class OperatorCreator { public static Operator getInstance(String expr) { ... } }

The main problem: And I want to let others to design their own operators by extending Operator class. And use their operators polymorphically. something like this:

public class Main { public static void main(String[] args) { Operator op = OperatorCreator.getInstance("1-2"); System.out.println(op.getOperatorResult()); } }

How can I do this?

Edit: I know it is possible using reflection. But I'd rather not to use reflection if it is possible. And I know I can let the designer of the Operator's sub-class register her new class using invoking a method at runtime. But I don't want to use this method.

Thanks!

最满意答案

基本上你是在问如何实现一个框架; 在以下意义上:

如果其他方可以提供实施运营商的类别; 重点是:您需要一个中心实例,该实例了解哪个类为哪个运算符提供实现。

因此,一个解决方案是“实现者”必须提供类似“getOperator()”的方法......然后返回“ - ”或“+”或其他任何方法。

然后,如果类文件实现了您的接口,则可以使用反射来“扫描”类文件; 如果是这样; 你可以调用“getOperator()”来理解哪个类提供了哪个实现。 稍后,您的OperationCreator可以使用该知识来实例化那些解析“1-2”所需的类的对象。

我更喜欢一种避免使用反射的方法。 相反,“实现者”应该能够调用某些方法,如“OperationCreator.registerOperationProvider()”。 含义:首先,告诉OperationCreator XYZ可以处理对象“ - ”; 然后,OperationCreator可以稍后向XYZ发送“ - ”调用。

Basically you are asking how to implement a framework; in the sense of:

if other parties can provide classes that implement an operator; the main point is: you need a central instance that understands which class provides the implementation for which operator.

So, one solution would be that an "implementer" has to provide a method like "getOperator()" ... which would then return "-", or "+" or whatever.

You could then use reflection to "scan" class files if they implement an interface of yours; and if so; you can call "getOperator()" to understand which class provides which implementation. Later on, your OperationCreator can use that knowledge to instantiate objects of those classes that are required to resolve "1-2" for example.

I would prefer a way that avoids using reflection. Instead, an "implementer" should be able to call some method like "OperationCreator.registerOperationProvider()". Meaning: first, you tell the OperationCreator that object XYZ can handle "-"; then the OperationCreator can dispatch calls for "-" to XYZ later on.

更多推荐

本文发布于:2023-08-04 20:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422247.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并以   任何人都可以   方式   多态   抽象类

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!