Java 方法如下所示: public static >密集向量 valueOf(F... elements)...它们是什么?

发布于 2024-08-09 06:57:55 字数 903 浏览 6 评论 0 原文

因此,在有人深思熟虑地向我指出在 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

踏雪无痕 2024-08-16 06:57:55

您应该能够调用此方法来创建向量,如下所示:

Real r1 = Real.ONE, r2 = Real.valueOf(2D), r3 = Real.ZERO;
DenseVector<Real> v = valueOf(r1, r2, r3);

在此示例中,类型参数 FRealReal 遵守约束“extends Field”,因为它实现了 Field

对于不同的应用程序,可能会使用不同的字段。例如,安全应用程序可能使用 ModuloInteger 字段。这有点令人困惑,因为这是一个数学场,而不是像物理学中所说的“矢量场”。

通过使用类型变量,该库有助于确保您在给定字段内执行所有操作。例如,给定像上面那样声明为 DenseVectorv,如果您尝试将其乘以 Complex 数字,编译器会发出警告。

You should be able to invoke this method to create a vector, like this:

Real r1 = Real.ONE, r2 = Real.valueOf(2D), r3 = Real.ZERO;
DenseVector<Real> v = valueOf(r1, r2, r3);

In this example, the type argument F is Real. Real obeys the constraint "extends Field<F>" because it implements Field<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 a DenseVector<Real> like above, the compiler will complain if you try to multiply it by a Complex number.

明月夜 2024-08-16 06:57:55

这是一个通用的返回类型。有关 Java 泛型的教程,请参阅此处

It's a generic return type. See here for a tutorial on Java Generics.

温柔戏命师 2024-08-16 06:57:55

这些称为通用类型。它们已添加到 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.

浅笑轻吟梦一曲 2024-08-16 06:57:55
public static <F extends Field<F>> DenseVector<F> valueOf(F... elements)

让我们分解一下:

public static

它是一个公共静态方法。

<F extends Field<F>>

它是任何类 F 的通用方法,其中 F 是 Field 的扩展。

DenseVector<F>

它为 F 返回一个(通用)DenseVector

valueOf(F... elements)

名为 valueOf 的方法,其中参数为零或多个 F。

public static <F extends Field<F>> DenseVector<F> valueOf(F... elements)

Lets break this down:

public static

Its a public static method.

<F extends Field<F>>

Its a generic method for any class F where F is an extention of Field

DenseVector<F>

It returns a (generic) DenseVector for F

valueOf(F... elements)

A method named valueOf where parameters are zero or more Fs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文