本帖最后由 王琪 于 2014-6-8 08:43 编辑
- package org.demo.java;
- import java.util.*;
- class Person{
- private String name;
- private int age;
- public Person(){};
- public Person(String name,int age){
- this.name=name;
- this.age=age;
- }
- public void setname(String name){
- this.name=name;
- }
- public void setage(int age){
- this.age=age;
- }
- public String getname(){
- return this.name;
- }
- public int getage(){
- return this.age;
- }
- public boolean compare(Person per){
- if(this==per)return true;
- else if(this.name.equals(per.name) && this.age==per.age)return true;
- else return false;
- }
- public String toString(){
- return"姓名"+name+"年龄"+age;
- }
- }
复制代码 public class text {
public static void main(String[]args){
Person person[]=null;
person=new Person[3];
int i=0;
for(Person x : person){
x=new Person(new String("wangqi"+i++),(30+i));
}
for(Person a:person){
System.out.println(a);
}
- for(i=0;i<person.length;i++){
- person[i]=new Person(new String("wangqi"+i),(30+i));
- }
- for(Person a:person){
- System.out.println(a);
- }
复制代码
}
}
null
null
null
姓名wangqi0年龄31
姓名wangqi1年龄32
姓名wangqi2年龄33
|
|