/** a person with name and age*/
public class Person {
private Name name;
private int age;
public Person(){
name=null;
age=0;
}//end default constructor
public Person(Name personName,int personAge){
name=personName;
age=personAge;
}//end constructor
public void setName(Name personName){
name=personName;
}//end setName
public Name getName(){
return name;
}//end getName
public void setAge(int personAge){
age=personAge;
}//end setAge
public int getAge(){
return age;
}//end getAge
public boolean equals(Person otherPerson){
if(name==otherPerson.name && age==otherPerson.age)
return true;
else
return false;
}
public boolean older(Person otherPerson){
if(age>=otherPerson.age)
return true;
else
return false;
}
public String toString(){
return name+" "+age;
}
public static void main(String[] args){
Name jack1Name=new Name("Jack","John");
Name jack2Name=new Name("Jack","Johnson");
Person jack1=new Person(jack1Name,22);
Person jack2=new Person(jack2Name,21);
if(jack1.equals(jack2))
System.out.println("The same person.");
else
System.out.println("Not the same person.");
if(jack1.older(jack2))
System.out.println(jack1.getName().toString()+" is older than "
+jack2.getName().toString()+".");
else
System.out.println(jack1.getName().toString()+" is younger than " +
"or with the same age of "+jack2.getName().toString()+".");
}
}
Result
Not the same person.
Jack John is older than Jack Johnson.
回复Comments
{commenttime}{commentauthor}
{CommentUrl}
{commentcontent}