📄 person.java.txt
字号:
/* The class that represents a Person object
* Person "live" in a Society
*
* Author: Dmitry Gakhovich
* E-mail: jim_g@lycos.com or d.gakhovich@witt.ac.nz
* (C) 2004
*/
import java.util.ArrayList;
public class Person{
String gender; // Person's gender
String name; // Person's name
int age; // Person's age
Person partner = null; // husband or wife - reference to another Person object
Person father = null;
Person mother = null;
ArrayList children = new ArrayList(); // Person may have more than one child -
// so we need a list
Society society = null; // After the Person is added to a Society (see Society class)
// the "society" objects will not be null.
// And all persons in the "society" will refer to the same
// "society"
// At the end of the day, we all live in the same society...
public Person(){ // constructor
this.name = "";
// gender is selected randomly - 50/50
if(Math.random()<=0.5)
this.gender = "female";
else
this.gender = "male";
this.age = 0;
}
public Person(String name, String gender, int age){// one more constructor
this.name = name;
this.gender = gender;
this.age = age;
}
// bunch of get - set methods:
public String getGender(){
return gender;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Person getPartner(){
return partner;
}
public void setPartner(Person partner){
this.partner = partner;
}
public Person getFather(){
return father;
}
public Person getMother(){
return mother;
}
public void setFather(Person father){
this.father = father;
}
public void setMother(Person mother){
this.mother = mother;
}
// married female Person may have a baby
public Person getBaby(){ // method to get new baby :)
if(age < 18 || partner == null || gender.equals("male"))
return null;
else{
Person baby = new Person(); // new Person!
baby.setMother(this); //
baby.setFather(partner); // the baby isn't only mom's baby!
setChildren(baby);
partner.setChildren(baby);
return baby;
}
}
public void setChildren(Person baby){
children.add(baby);
}
public ArrayList getChildren(){
return children;
}
public void setSociety(Society society){
this.society = society;
}
// a Person may fall in love with a Person of opposite sex
// E.g., if the person is a male he may fall in love with any
// adult female Person from the Society. The lucky lady
// is chosen randomly. And vice versa.
public Person fallInLove(){
ArrayList choice;
if(gender.equals("male")){
choice = society.getAdultFemales();
}
else{
choice = society.getAdultMales();
}
int n = choice.size();//
if(n>0){// can select only if size>0
// generate random arrayList index:
int option = (int)(Math.random()*n );
return (Person)choice.get(option); //the current Person will love
// this "option"
}
return null; // may happen if there are no Persons of opposite gender...
}
// If somebody (another Person) falls in love, the current person may accept
// this feelings or not. Chances - 50/50. This is a life.... :(
public boolean acceptLove(){
if(Math.random()<=0.5)
return true;
else
return false;
}
public String toString(){//override toString() method for the object
String s = "Name: " + getName() + "; Gender: " + getGender() + "; Age: " + getAge();
return s;
}
}// end Person
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -