【问题标题】:What does it mean when a constructor has a type that is the class?当构造函数的类型是类时,这意味着什么?
【发布时间】: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


【解决方案1】:

变形虫的父母是……变形虫。假设我们刚出生的 Amoeba 被称为 Amy Jr.,它的父级是 Amy Sr.,您将为 Amy Jr. 创建一个 Amoeba 类型的对象,并指定其父级 Amy Sr. 也是一个 Amoeba,方法是使用与孩子相同的类型。

如果它的父母是其他东西(我不知道它在生物学上是如何可能的),比如 Parrot,你会用这样的东西来创建一个新的变形虫,它的父母是鹦鹉

public Amoeba (String name, Parrot parent) // contructor with arguments 
    {
        myName = name;
        myParent = parent;
        myChildren = new Vector ();
    }
    public Parrot myParent;

我希望这能回答你的问题。

【讨论】:

    【解决方案2】:

    不是构造函数:

    public Amoeba parent () 
    { 
        return myParent;
    }
    

    这只是一个名为parent 的方法,它返回一个Amoeba 对象。构造函数:

    public Amoeba (String name, Amoeba parent) 
    {
        myName = name;
        myParent = parent;
        myChildren = new Vector ();
    }
    

    初始化一个名为myParentAmoeba 实例成员。 parent 方法返回所述实例成员。

    【讨论】:

      【解决方案3】:

      一个类可以有自己类型的成员,没问题。这一直用于创建链接列表。

      Try this.

      【讨论】:

        【解决方案4】:

        这只是意味着通过给出变形虫的名称及其父级来构造变形虫。它的父母恰好是另一个变形虫。就像文件系统中的一个文件夹有另一个文件夹作为其父文件夹一样。或者一个人有两个人作为父母。

        【讨论】:

          【解决方案5】:

          似乎意味着每个 Amoeba 对象在实例化时都将使用对相同 Amoeba 类型的父对象的引用来构造...我想如果这些对象有这样的父子结构Amoeba 对象最终将有一个没有父级的 Amoeba ......并且在父级处具有空值......而其余的都将在其父级的某个地方调用一些 Amoeba 对象......如果这有意义的话。

          【讨论】:

            【解决方案6】:

            把它想象成一棵家谱。

            public class Person {
                public Person(String name, Person father, Person mother) { ... }
            }
            

            这只是意味着要实例化一个新的Amoeba,您需要首先为其提供一个名称和父级。

            【讨论】:

              猜你喜欢
              • 2014-07-08
              • 1970-01-01
              • 2021-11-30
              • 1970-01-01
              • 2013-01-16
              • 1970-01-01
              • 2020-05-31
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多