是否有一个 java 方法的名称被视为与任何特定类分开?
这是一个术语问题,很难问!
让我举个例子。假设我正在编写一个符号微分算法。我有一个抽象类 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这听起来像是“正常”子类型多态性。子类/实现完成工作,但接口是在基类型中定义的。这种“分散”方法与访问者模式(“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:
我不确定这是否是您正在寻找的,但我想我可以用设计模式术语来回答这个问题。你的例子听起来有点像 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.
相反,我认为“方法”是 Java 上下文中的标准术语。
On the contrary, I think that "method" is the standard term for this in the Java context.
多态函数可以应用于不同类型的值。该功能可以由多个Java方法来实现。
A polymorphic function can be applied to values of different types. The function may be implemented by more than one Java method.