【发布时间】:2013-12-23 21:58:41
【问题描述】:
我不太确定public Amoeba myParent; 发生了什么。
据我了解,myParent 的类型是类,我在概念上很难理解。这意味着什么?
public class AmoebaFamily {
private class Amoeba { // more control on when objects will be created because its private
public Amoeba (String name, Amoeba parent) // contructor with arguments
{
myName = name;
myParent = parent;
myChildren = new Vector ();
}
public String myName; // amoeba's name
public Amoeba myParent; // amoeba's parent
public Vector myChildren; // amoeba's children //array that can be resize
public String toString () { // toString is used whenever you require to explore the constructor called value in string form
return myName;
}
public Amoeba parent () { // constructor without argument just return myParent
return myParent;
}
【问题讨论】:
-
public Amoeba parent()不是构造函数;这是一种方法。只是一个术语错误。 -
构造函数在 Java 中没有类型;这不是语言语法的一部分。
-
我认为 OP 对变量声明
public Amoeba myParent;而不是方法或构造函数感到困惑。 -
@Shobit - 查看
parent()方法的注释。这(以及标题中的措辞)表明 OP 对方法和构造函数感到困惑。
标签: java class data-structures constructor