【问题标题】:Family Tree, method confusion家谱,方法混乱
【发布时间】:2018-10-02 05:49:53
【问题描述】:

我目前正在从事一个项目,该项目涉及创建一个可以输入和显示一个人的家谱的程序。但是,在使用构造函数创建了一个名为 Person 的类来设置该人的名称和调用该类的方法之后,我意识到我不知道如何为调用“人”的方法输入正确的参数班级。下面是我的代码-

public class FamilyTree

{
    private class Person
    {
        private String name;

        public Person(String name)
        {
            this.name = name;

        }
    }
public void InputInformationfor(Person person)
{
//also do not know what would go here
}

public static void main(String[] args)
{
    FamilyTree famtree = new FamilyTree();
    famtree.InputInformationfor(??????);

}
}

任何帮助将不胜感激。我真的在努力推动我对 Java 的机制和基本要素的理解,因为这是我在编码方面的野心肯定会停止的地方。

附:不知道为什么 stackOverflow 将我的代码的前几行格式化为普通文本...

【问题讨论】:

  • 您最好阅读一些OOP(面向对象编程)书籍,以了解使用类和对象的含义和正确方法。如果您没有足够的时间这样做,也许可以搜索在线课程或一些教程。
  • 我一定会的。作为一名大学生,有时只是此时此地是我的首要任务,但我计划尽可能挤出时间来掌握基础知识。感谢您的意见。

标签: java class methods parameters tree


【解决方案1】:

也许你可以使用这样的东西: 1. 为Person类定义变量和为一个人添加信息的方法,例如:

public class Person {
      String name;
      String age;
      int age;
      String father;
      String mother;

//Define constructor
      public Person(){
      //What you want initialize.
      }

      public void setname(String nameinput){
      name = nameinput;
      }
}
  1. 定义您想要的家谱,就像您为一个人所做的那样。
  2. 我会使用一个虚拟的 Person 类将 Persons 添加到家谱中。因此,在您的主要功能中,您可以将人员添加到树中。

      public static void main(String[] args){
      Person dummy = new Person();
      //Add data to the person.
      dummy.setname("Mike");
      //Add the information you want to the dummy variable.
      //Then you can add the person to the tree.
      FamilyTree famtree = new FamilyTree();
      famtree.InputInformationfor(dummy);}
    

我希望这可以帮助你。也许你必须阅读关于课程的必读内容。 Java for dummy 是一个好的开始。

【讨论】:

  • 非常感谢,您的解释让我明白了很多。
【解决方案2】:

首先,你不需要私有类,你可以如下定义类,

public class FamilyTree {
//....
}
class Person {
//....
}

在 FamilyTree 类中,您需要有一个结构来存储人员。虽然 FamilyTree 是一棵,但您可以使用 list 来简单起见。您应该定义此列表并在构造函数中对其进行初始化。

public class FamilyTree {
    List<Person> personList;
    public FamilyTree(){
        personList = new ArrayList();
    }
    //...
}

当你想在personList中添加一个人时,你可以调用InputInformationfor方法。然而,这是一个坏名字 imo,你可以说 addPerson。

public void addPerson(Person person){
    personList.add(person);
}

最后,在 main 方法中,您可以先创建一个人,然后将其添加到列表中。所以,最终的代码是这样的,

import java.util.ArrayList;
import java.util.List;


public class FamilyTree {
    List<Person> personList;
    public FamilyTree(){
        personList = new ArrayList();
    }

    public void addPerson(Person person){
        personList.add(person);
    }


    public static void main(String[] args){
        FamilyTree famtree = new FamilyTree();
        Person person = new Person("Emre");
        famtree.addPerson(person);
    }
}

class Person {
    private String name;

    public Person(String name){
        this.name = name;
    }
}

【讨论】:

  • 非常感谢。所以换句话说,调用一个以类为参数的方法需要创建该类的实例,即'person'或'person1'?
  • 是的,您也可以在一行中执行此操作。 famtree.addPerson(new Person("Emre"));
  • 哦,更好。真的很感激,Emre。
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 2015-12-12
  • 2013-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
相关资源
最近更新 更多