数组society中包含着若干元素,这些元素属于自创的$person类每个person类的元素都有自己的ID,年龄(age),以及一个子数组,子数组中包含该person的孩子,孩子也是一个person类。现在要给这个社会里的每个人,既包括大人也包含孩子,都逐渐地加上年龄,一旦某到人到一百岁,就把他踢出这个社会,直到把所有人都踢光。求问这些的代码应该怎么处理?以下是已经写好的一部分代码:多谢各位大侠了//这是定义部分
class Person {
var $id;
var $age;
var $children;
function Person( $id, $age) {
$this->id = $id;
$this->age = $age;
// all children if any are added will be places into this array
$this->children = array();
}
function addChild( $child) {
array_push( $this->children, $child);
}
function delete() { // This doesn't seem to be very useful funtion, but I just left it here.
unset( $this);
}
function delChild() { //This uses the provided function array_shift to make a child independent.
array_shift( $this->children);
}
function agepeople( $itemage) {
$this->age++;
}
}?>以下是创建起来整个社会的代码:// this array contains the entire society
global $society;
global $nop; // variable NOP means THE NUMBER OF PEOPLE in this society
$society = array();
$id = 0; // this will be used to give unique id to everyone in society
// create 100 people of age
for ( $i = 0; $i < 100; $i++) {
// create a new person with a random age between 20 and 50
// mt_rand is a PHP random number generator function
$person = &new Person( $id, mt_rand( 20, 50));
$nop=$id+1;
$id++; // next person will have another id
if ( mt_rand( 0, 1) == 0) { // 50/50 change of adding children to this person
$count = mt_rand( 1, 5); // add between 1 and 5 chidren to this person
for ( $y = 0; $y < $count; $y++) {
$person->addChild( new Person( $id, mt_rand( 0, 10)));
$nop=$id+1;
$id++; // next child or person will have another id
}
}
array_push( $society, $person);
}后面主要实现两个功能就可以了,一个是为每个人(大人以其所领的小孩),都加上年龄然后每有一个人到100岁时,就把他踢出这个社会。如何是好啊?
class Person {
var $id;
var $age;
var $children;
function Person( $id, $age) {
$this->id = $id;
$this->age = $age;
// all children if any are added will be places into this array
$this->children = array();
}
function addChild( $child) {
array_push( $this->children, $child);
}
function delete() { // This doesn't seem to be very useful funtion, but I just left it here.
unset( $this);
}
function delChild() { //This uses the provided function array_shift to make a child independent.
array_shift( $this->children);
}
function agepeople( $itemage) {
$this->age++;
}
}?>以下是创建起来整个社会的代码:// this array contains the entire society
global $society;
global $nop; // variable NOP means THE NUMBER OF PEOPLE in this society
$society = array();
$id = 0; // this will be used to give unique id to everyone in society
// create 100 people of age
for ( $i = 0; $i < 100; $i++) {
// create a new person with a random age between 20 and 50
// mt_rand is a PHP random number generator function
$person = &new Person( $id, mt_rand( 20, 50));
$nop=$id+1;
$id++; // next person will have another id
if ( mt_rand( 0, 1) == 0) { // 50/50 change of adding children to this person
$count = mt_rand( 1, 5); // add between 1 and 5 chidren to this person
for ( $y = 0; $y < $count; $y++) {
$person->addChild( new Person( $id, mt_rand( 0, 10)));
$nop=$id+1;
$id++; // next child or person will have another id
}
}
array_push( $society, $person);
}后面主要实现两个功能就可以了,一个是为每个人(大人以其所领的小孩),都加上年龄然后每有一个人到100岁时,就把他踢出这个社会。如何是好啊?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货