因此,在有人深思熟虑地向我指出在 java 中获取向量(即数学向量)之后,我正在浏览一个 java 库(JScience)。
不幸的是,我以前从未见过像这样的东西:
public static <F extends Field<F>> DenseVector<F> valueOf(F... elements)
作为可以在 DenseVector 类中调用的方法。这……到底是什么意思啊。它是否返回“>
”(如果是这样,为什么 Eclipse 认为它是一个输入?)
http://jscience.org/api/org/jscience/mathematics/vector/DenseVector.html #valueOf(F...)
这真的让我很困惑。我无法创建一个 new DenseVector()
因为只有超类具有该功能,并且它受到保护,并且尝试执行 DenseVector.valueOf()
显然只有在我这样做时才有效给它...那个...奇怪的东西作为输入。
我见过人们在尝试实例化对象(或类似的东西)时必须实例化方法...是这样的(还是那样?)) API 试图让我做什么?
我有点困惑,我在学校学过java(并且在工作中使用了一点,尽管除了java之外我们还使用了很多不同的东西),但从未遇到过这样的东西。这是做什么用的?它到底想逼我做什么?是新的吗?老的?朦胧?
-珍妮
So, I'm looking through a java library (JScience) after someone here thoughfully pointed me towards it for getting Vectors (mathematical ones, that is) in java.
Unfortunately, I've never seen anything in my life before like:
public static <F extends Field<F>> DenseVector<F> valueOf(F... elements)
as a method you can call in the DenseVector class. What...does that even mean. Is it returning a "<F extends Field<F>>
" (and if so, why does Eclipse think it's an input?)
http://jscience.org/api/org/jscience/mathematics/vector/DenseVector.html#valueOf(F...)
It really confuses me. I can't make a new DenseVector()
because only the super class has that, and it's protected, and trying to do DenseVector.valueOf()
apparently only works if I give it...that...weird thing as an input.
I've seen people having to instantiate methods when trying to instantiate objects (or something like that)...is that like that (or IS it that?)) What is the API trying to get me to do?
I'm kind of confused that I've learned java in school (and used it a bit at work, though we use a lot of differnet stuff besides just java), and never came across anything like this. What's it for? What's it trying to get me to do? Is it new? Old? Obscure?
-Jenny
发布评论
评论(4)
您应该能够调用此方法来创建向量,如下所示:
在此示例中,类型参数
F
是Real
。Real
遵守约束“extends Field
”,因为它实现了Field
。对于不同的应用程序,可能会使用不同的字段。例如,安全应用程序可能使用
ModuloInteger
字段。这有点令人困惑,因为这是一个数学场,而不是像物理学中所说的“矢量场”。通过使用类型变量,该库有助于确保您在给定字段内执行所有操作。例如,给定像上面那样声明为
DenseVector
的v
,如果您尝试将其乘以Complex
数字,编译器会发出警告。You should be able to invoke this method to create a vector, like this:
In this example, the type argument
F
isReal
.Real
obeys the constraint "extends Field<F>
" because it implementsField<Real>
.For different applications, different fields are likely to be used. For example, security applications might use the
ModuloInteger
field. It's a little confusing because this is a mathematical field, not a "vector field" like one talks about in physics.By using type variables, this library helps to make sure you perform all operations within a given field. For example, given
v
declared as aDenseVector<Real>
like above, the compiler will complain if you try to multiply it by aComplex
number.这是一个通用的返回类型。有关 Java 泛型的教程,请参阅此处。
It's a generic return type. See here for a tutorial on Java Generics.
这些称为通用类型。它们已添加到 Java 5 中,与 C++ 模板类似。
这个想法是,您定义特定类型的项目的集合,而不是一般的项目。
这可以帮助你避免频繁的沮丧。在较旧的 Java 代码中,假设您知道向量将仅包含 X。一旦您从该集合中检索了项目,您将只获得 Object,并且必须显式地向下转换它。
它也更安全,因为您不能将 Y 放入 X 的向量中,并且出于同样的原因,它更易于阅读。
这些括号中的“扩展”背后的故事是,您可以定义“X 及其所有子类型”的集合,这些集合仍然接受 X 的子类型但拒绝 Y。
These are called Generic types. They've been added in Java 5 and are similar to C++ templates.
The idea is that you define a collection of items of a particular type rather than something general.
This helps you avoid frequent downcasting. In older Java code, suppose that you knew your vector would contain only X's. Once you retrieved items out of that collection, you would just get Object, and you had to explicitly downcast it.
It is also safer because you can't put Ys into a vector of Xs, and clearer to read for the same reasons.
The story behinds the "extends" in these brackets is that you can define collections of "Xs and all their subtypes" that would still accept subtypes of X but reject Y.
让我们分解一下:
它是一个公共静态方法。
它是任何类 F 的通用方法,其中 F 是 Field 的扩展。
它为 F 返回一个(通用)DenseVector
名为 valueOf 的方法,其中参数为零或多个 F。
Lets break this down:
Its a public static method.
Its a generic method for any class F where F is an extention of Field
It returns a (generic) DenseVector for F
A method named valueOf where parameters are zero or more Fs.