查找有关 java 类、软件设计的信息?
我有一堆扩展抽象基类的类。 每个子类都采用一个数组,如构造函数中所示(不同的长度取决于类)。 这些类可以由其他人编写。
计算类所需数组长度的最佳方法是什么? 我可以: (A) 要求每个派生类都有一个静态方法,返回长度。 然而,基类不能强制执行这一点,因为抽象静态方法在 java 中不起作用。 (B) 每个派生类都有一个不带参数的构造函数,我构造 这样的类只是为了能够调用 countParameters() 方法,即 我可以从基类强制执行。这感觉“笨拙”,因为我对创建这样的对象不感兴趣,而只需要一些有关它的信息。
原因是我正在创建一个 GUI,它使用户能够创建 派生类的实例,但每个派生类采用不同数量的参数。 也就是说,我需要知道如何绘制 GUI,然后才能创建类。
编辑: 我可以只要求每个派生类都有一个私有的 构造函数,不带参数,并使用反射,我可以调用 countParameters() 方法。
EDIT2:实际上,我感兴趣的是参数的名称。 也就是说,如果 Derived 类具有构造函数,
public Derived(double name1,double name2,...)
我需要一种生成 String[] 数组的方法,
{name1,name2,...}
我想如果不创建该类的实例,这是不可能做到的, 但为了让用户能够创建这样的类,他/她需要参数名称! 第 22 时刻。
I have a bunch of classes extending an abstract Base class.
Each subclass takes an array as in the constructor, (different length depending on class).
These classes could be written by other people.
What is the best way to figure out the length of the array the class needs?
I could:
(A) Require that each derived class have a static method, returning the length.
However, the base class cannot enforce this, since abstract static methods does not work in java.
(B) Each derived class have a constructor with no arguments, and I construct
such classes just to be able to call the countParameters() method, that
I can enforce from the Base class. This feels "cludgy", since I am not interested in creating such object, but only need some info about it.
The reason is that I am creating a GUI, that gives the user the ability to create
instances of Derived classes, but each Derived class takes different number of parameters.
That is, I need to know how to draw the GUI before I can create the classes.
EDIT:
I could just require that each Derived class have a private
constructor, with no arguments, and using reflection I can call the countParameters() method.
EDIT2: Actually, what I am interested in, is what the names of the parameters are.
That is, if the class Derived have the constructor
public Derived(double name1,double name2,...)
I need a way to generate the String[] array
{name1,name2,...}
I guess this would be impossible to do without creating an instance of the class,
but for the user to be able to create such class, he/she needs the parameter names!
Moment 22.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来您需要工厂模式。
一般来说,让基类知道其后代的集合是一个坏主意。因此,您定义了另一个类,其工作就是了解这一点。
如果您有类似 Shape 的东西,并且以 ThisShape 和 ThatShape 作为派生类,那么 ShapeCreator 将处理创建程序支持的特定形状集的工作,并为每个形状提供所需的参数。
It sounds like you need the Factory Pattern.
In general, it's a bad idea for a base class to know the set of it's descendant's. So you define another class whose job it is to know that.
If you have something like a Shape, with ThisShape and ThatShape as derived classes, then a ShapeCreator will handle the job of creating the specific set of shapes your program supports, giving each one the arguments it needs.
目前还不太清楚您想要实现的目标,但我想知道:子类是否真的必须采用带有数组的单个参数,而不是参数列表?
It's not quite clear what you're trying to achieve, but I wonder: Do the subclasses really have to take a single parameter with an array, as opposed to a list of parameters?
每个子类都需要实现 size 方法。
希望有帮助。
each child class needs to implement size method.
hope its help.
我会选择方法 A。您无法让编译器强制执行这种方法的存在,但您当然可以在程序中强制执行它 - 没有方法,就没有工作!
说真的,整个计划有点脆弱,我想不出一种方法可以让它变得更好。这些子类的错误实现将会失败,这就是生活。
一种可能的补救措施是您为这些子类提供一组接口,例如
并要求您的子类实现其中一个接口作为标记接口。但这只不过是更多的官僚主义,并没有什么效果。
I'd go with method A. You can't get the compiler to enforce the existence of such a method, but you can certainly enforce it in your program - no method, no work!
Seriously, this whole scheme is a bit brittle and I can't think of a way to make it significantly better. An incorrect implementation of those subclasses will bomb out, that's life.
A possible remedy would be for you to provide a set of interfaces for those subclasses, such as
and requiring your sub's to implement one of those as a marker interface. But that's just more bureaucracy with little more effect.