Redian新闻
>
博士后(mechanics of materials/biomechanics/mechanobiology)
avatar
博士后(mechanics of materials/biomechanics/mechanobiology)# Engineering - 工程
j*y
1
在The Practice of Programming(中文名:《编程实践》)的第74~75页上看到下面的
代码:
static final int MULTIPLIER = 31; // for hashCodeO
// Prefix hashCode: generate hash from all prefix words
public int hashCode()
{
int h = 0;
for (int i = 0; i < pref.sizeO; i++)
h = MULTIPLIER * h + pref.elementAt(i).hashCodeO;
return h;
}
// Prefix equals: compare two prefixes for equal words
public boolean equals(Object o)
{
Prefix p = (Prefix) o;
for (int i = 0; i < pref.sizeO; i++)
if (!pref.elementAt(i).equals(p.pref.elementAt(i)))
return false;
return true;
}
这两个method里面的pref都是个vector,里面装的是string。这两个method貌似都是递
归函数。
我的问题是,hashCode()和equals()都是class Prefix(见下面的代码)的method,但
从调用方式看,怎么又成了string的成员函数呢?不解啊。
class Prefix {
public Vector pref; // NPREF adjacent words from input
// Prefix constructor: duplicate existing prefix
Prefix(Prefix p)
{
pref = (Vector) p.pref.clone();
}
// Prefix constructor: n copies of str
Prefix(int n, String str)
{
pref = new VectorO;
for (int i = 0; i < n; i++)
pref.addElement(str);
}
//下面应该是hashCode()和equals()的声明
}
请高手答疑,谢了先。
avatar
J*R
2
One postdoctoral research fellow position is available from Oct 1 or
earlier. The successful candidate will work on one of the following topics
depending on his/her background and expertise: novel morphing structures
using smart materials, mechanics of morphogenesis in embryos, fast motion of
plants (e.g., the Venus flytrap's rapid closure), and mechanical
instability of thin structures (e.g., DNA, plant tendrils, self-assembly of
nanostructures, etc.). The candidate should have received (or expect to
receive very soon) a Ph.D. degree in Mechanical Engineering, Materials
Science and Engineering, Biomedical Engineering, or any related field,
having ether strong background in theoretical/computational mechanics
, or experimental experience in smart materials, such as shape memory or
piezoelectric materials, nanofabrication, biomechanics, and/or
mechanobiology.
Interested party is invited to email Dr. Zi Chen, Society in Science -
Branco Weiss Fellow, Research Scientist, at c****[email protected] http://www.society-in-science.org/zi-chen.html with the current CV and the contact information of 2-3 references. The position remains open until filled. The original appointment will be for one year and may be renewable by the end of the first year.
avatar
r*l
3
equals and hashCode are defined in Object class. Every Java class extends
Object, thus every class has equals and hashCode.
In your example, there is no recursive call.

【在 j***y 的大作中提到】
: 在The Practice of Programming(中文名:《编程实践》)的第74~75页上看到下面的
: 代码:
: static final int MULTIPLIER = 31; // for hashCodeO
: // Prefix hashCode: generate hash from all prefix words
: public int hashCode()
: {
: int h = 0;
: for (int i = 0; i < pref.sizeO; i++)
: h = MULTIPLIER * h + pref.elementAt(i).hashCodeO;
: return h;

avatar
x*p
4
Agree with the upper level.
avatar
j*y
5
谢谢大家啊,是我搞错了。之前还以为这两个函数都是递归调用呢。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。