我正在尝试用Java编写一个personclass和personTester

时间:2014-02-13 17:23:33

标签: java tostring

到目前为止,对于我的personClass,我有以下内容:

package personclass;

public class Personclass {
private static boolean Personclass;
private int PersonCount;
    public int getPersonCount()
    {
        return PersonCount;
    }

    private String FirstName;
    private String LastName;
    private int Age;
    private Double Height;
    private String Gender;

    public Personclass(String foreName, String surName, int age, Double height, String gender)
    {
        FirstName = foreName;
        LastName = surName;
        Age = age;
        Height = height;
        Gender = gender;
    }

    private String getFirstName()
        {
    return FirstName;
}

private void setFirstName(String foreName)
{
    this.FirstName = foreName;
}

private String getLastName()
{
    return LastName;
}

private void setLastName(String surName)
{
    this.LastName = surName;
}

private int getAge()
{
    return Age;
}

private void setAge(int age)
{
    this.Age = age;
}

private Double getHeight()
{
    return Height;
}

private void setHeight(Double height)
{
    this.Height = height;
}

private String getGender()
{
    return Gender;
}

private void setGender(String gender)
{
    this.Gender = gender;
}

/**
 *
 * @param FirstName
 * @param LastName
 * @param Age
 * @param Height
 * @param Gender
 */
public Personclass(String FirstName, String LastName, int Age, double Height, String Gender)
{
    this.FirstName = FirstName;
    this.LastName = LastName;
    this.Age = Age;
    this.Height = Height;
    this.Gender = Gender;
            ++PersonCount;
}

/**
 *
 * @return 
 */

@Override
public String toString()
{
        return "Person[forename=" + getFirstName() + ", surname=" +  getLastName() +  ", age=" + getAge() + ", height=" + getHeight() +"m" + ", gender=" + getGender() +"]";
}
public String format()
{
        return String.format("%10s %10s %10d %10.2f %10s", getFirstName() , getLastName() , getAge() , getHeight() , getGender());
}

public static void main(String[] args) 
{

}
}

对于我的personTester,我有以下代码:

package personclass;

public class PersonTester
{
public static void main(String[] args) 
{

        Person person1 = new Person("Joe","Smith",25,1.57,"Male");

        Person person2 = new Person("Sain","Davies",18,1.73,"Female");

        Person person3 = new Person("John","White",22,1.60,"Male");

        Person person4 = new Person("Martin","Taylor",26, 1.54,"Male");

        Person person5 = new Person("Jessica","Clarke",19,1.70,"Female");

        System.out.println(person1.toString());
        System.out.println(person2.toString());
        System.out.println(person3.toString());
        System.out.println(person4.toString());
        System.out.println(person5.toString());

}
}

当我尝试运行personTester时,我遇到麻烦的事情并不是要打印出任何东西。我将如何尝试将五个不同的人打印出来?

2 个答案:

答案 0 :(得分:0)

您需要在类PersonTester类型Personclass中的main()中创建对象。这应该使您能够通过该类中的toString()方法打印有关人员的信息。

答案 1 :(得分:0)

可以有两种可能的解决方案。

  1. 您应该确保您正在运行PersonTester课程,而不是Personclass。即,如果从命令行运行,则应使用java PersonTester命令。

  2. 您的班级名称为Personclass,但在PersonTester班级中,您指的是某个Person班级。您应该将Personclass更改为以下内容。

    package personclass;

        public class PersonTester{
    
          public static void main(String[] args) {
    
                Personclass person1 = new Personclass ("Joe","Smith",25,1.57,"Male");
                Personclass person2 = new Personclass ("Sain","Davies",18,1.73,"Female");
                Personclass person3 = new Personclass ("John","White",22,1.60,"Male");
                Personclass person4 = new Personclass ("Martin","Taylor",26, 1.54,"Male");
                Personclass person5 = new Personclass ("Jessica","Clarke",19,1.70,"Female");
    
                System.out.println(person1.toString());
                System.out.println(person2.toString());
                System.out.println(person3.toString());
                System.out.println(person4.toString());
                System.out.println(person5.toString());
            }
        }