是否有一个 java 方法的名称被视为与任何特定类分开?

发布于 2024-10-07 00:34:39 字数 658 浏览 3 评论 0原文

这是一个术语问题,很难问!

让我举个例子。假设我正在编写一个符号微分算法。我有一个抽象类 Exp,它是一堆表达式类型(总和、积分等)的超类。有一个抽象方法导数,使得 e.derivative() 应该是表达式 e 的导数。 Exp 的所有具体子类(想象一下这里的整个层次结构)都实现了一个派生方法,该方法捕获如何区分该类型的表达式的知识。给定方法通常通过组合子表达式的导数来计算表达式的导数。

示例的细节并不重要。重要的是,所有这些分散的方法都可以被视为一个(递归)算法的一部分。并非所有方法都是如此,但其中许多方法都是如此(并且使用重载为根本不同的操作重用相同的方法名称被认为是一个坏主意)。问题是,被视为单一函数的“导数”术语是什么?这不是一种方法;而是一种方法。在另一种语言中,它将是一个函数,并且各个子句(如何处理和,如何处理积分)将位于同一位置。我不关心哪种方法或语言更好,或者那种风格是否可以在 Java 中使用。我只想知道使用什么术语来表示“导数”被视为单个函数或过程(这个想法不仅限于函数式编程,递归也不是关键功能)。当我告诉别人我今天做了什么时,我想说“我试图实现符号微分__,但我想到的每个算法都不起作用。”空白处填什么?

我认为其他 OO 语言也会出现同样的问题,但 Java 是我最熟悉的一种。我对它非常熟悉,所以我很确定没有标准术语,但我想在得出这个结论之前我应该​​先问问我们这里的优秀专家。

This is a terminological question, which makes it hard to ask!

Let me give an example. Suppose I am writing a symbolic-differentiation algorithm. I have an abstract class Exp that is a superclass of a bunch of expression types (sums, integrals, whatever). There is an abstract method derivative such that e.derivative() is supposed to be the derivative of the expression e. All the concrete subclasses of Exp (imagine a whole hierarchy here) implement a derivative method that captures knowledge of how to differentiate expressions of that type. A given method will typically compute the derivative of an expression by combining derivatives of subexpressions.

The details of the example are not important. The important thing is that all of these scattered methods can be considered pieces of one (recursive) algorithm. This is not true of all methods, but it's true of many of them (and the use of overloading to reuse the same method name for fundamentally different operations is considered a Bad Idea). The question is, what is the term for 'derivative,' considered as a single function? It's not a method; in another language it would be a function, and the various clauses (what to do with sums, what to do with integrals) would be in the same place. I don't care which approach or languaage is better, or whether that style can be used in Java. I just want to know what term to use for 'derivative' considered as a single function or procedure (the idea is not limited to functional programming, nor is recursion a key feature). When I tell someone what I did today, I'd like to say "I tried to implement a symbolic-differentation __, but every algorithm I thought of didn't work." What goes in the blank?

I assume the same issue comes up for other OO languages, but Java is the one I'm most familiar with. I'm so familiar with it that I'm pretty sure there is no standard term, but I thought I would ask our fine battery of experts here before jumping to that conclusion.

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

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

发布评论

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

评论(4

转角预定愛 2024-10-14 00:34:39

这听起来像是“正常”子类型多态性。子类/实现完成工作,但接口是在基类型中定义的。这种“分散”方法与访问者模式(“Java 得到的一样好”)形成鲜明对比。 ") 或模式匹配(Java 中没有)或一个大的 manky switch/if-else 控制器。我不确定我真的会把它称为其他任何东西作为一个集合。

附录:您可能会发现Scala 案例类是一个失败的实验吗? 读得很好。特别是,谈论“列”与“行”组织以及每种方法的“局部性差异”的评论有:

<块引用>

...在面向对象中,您按行进行划分。每一行都是一个模块,称为类。与该数据变体相关的所有功能都分组在一起。这是一种合理的组织方式,而且很常见。优点是很容易添加数据变体...但缺点是很难添加因数据类型而异的新函数。您必须遍历每个类才能添加新方法。

That sounds like "normal" subtype polymorphism. The subclasses/implementations do the work but the interface is defined in a base-type. This "scatter" method is in contrast to say, the Visitor Pattern ("as good as Java gets") or Pattern Matching (not in Java) or a big manky switch/if-else controller. I'm not sure I really would call it anything else as an aggregate.

Addendum: you may find Are Scala case-classes a failed experiment? a nice read. In particular, the comments which talk about "column" vs. "row" organization and the "difference of locality" each approach has:

...in OO, you divide by rows. Each row is a module, called a class. All the functions pertaining to that data variant are grouped together. This is a reasonable way of organizing things, and it's very common. The advantage is that's easy to add a data variant ... However the disadvantage is that it's hard to add new functions that vary by data type. You have to go through every class to add a new method.

柏拉图鍀咏恒 2024-10-14 00:34:39

我不确定这是否是您正在寻找的,但我想我可以用设计模式术语来回答这个问题。你的例子听起来有点像 GoF 策略模式。这是用 Java 实现的策略模式示例

I'm not sure if this is what you're looking for but I think I can answer this in terms of design pattern terminology. Your example sounds vaguely like the GoF Strategy Pattern. Here is an example of the Strategy Pattern implemented in Java.

余生一个溪 2024-10-14 00:34:39

相反,我认为“方法”是 Java 上下文中的标准术语。

On the contrary, I think that "method" is the standard term for this in the Java context.

放手` 2024-10-14 00:34:39

多态函数可以应用于不同类型的值。该功能可以由多个Java方法来实现。

A polymorphic function can be applied to values of different types. The function may be implemented by more than one Java method.

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