java中类的使用

来源:百度知道 编辑:UC知道 时间:2024/06/20 18:54:09
外教布置了一份作业:
This program has to consist of two classes, PersonInfo.java and FindYoungest.java. The PersonInfo class must contain a method ReadNewPerson to read in the details of a person and a method DisplayPerson to print these details when needed. The details are,
Name Age
Rocket id Favorite car
All the above details must be declared as “private” variables.

The class must also contain the methods
GetName, GetAge, GetId, GetCar
which return respectively the name, age, rocket id, and favorite car of a person. All the methods in this class must be declared as “public”.

The FindYoungest class should have the “main” method. It should read the details of 3 persons, using the method of the PersonInfo class to set the details of each person, and display all the details of the youngest person.

我做出来的虽然能运行出来一样的结果,但是我并没有完全按照他的要求来做。首先他要求在PersonInfo这个class里有一个ReadNewPerson和 DisplayPerson的method,我就没有。其次我在输出最小年龄人的数据的时候是用

你这几个没有实现的功能,其实才是这个程序题的精髓,是java面向对象和类封装性的体现,你看看我给的代码:

public class PersonInfo
{
private String name;

private int age;

private String rocketId;

private String favoriteCar;

public int getAge()
{
return age;
}

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

public String getFavoriteCar()
{
return favoriteCar;
}

public void setFavoriteCar(String favoriteCar)
{
this.favoriteCar = favoriteCar;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public String getRocketId()
{
return rocketId;
}

public void setRocketId(String rocketId)
{