在Person类中创建了实例p1,为什么在另一个包中的类ObjectArray无法调用?
package Person;
import MyDate.MyDate;
public class Person
{
public String name;
public MyDate brithdate;
public String gender,province,city;
private static int count=0;
public Person(String name,MyDate brithdate,String gender,String province,String city)
{
this.set(name,brithdate,gender,province,city);
count++;
}
public Person(String name,MyDate brithdate)
{
this(name,brithdate,"","","");
}
public Person()
{
this("",new MyDate());
}
public Person(Person per)
{
this(per.name,new MyDate(per.brithdate),per.gender,per.province,per.city);
}
public void finalize()
{
System.out.println("释放对象("+this.toString()+")");
Person.count--;
}
public static void howMany()
{
System.out.print(Person.count+"个Persond对象,");
}
public boolean equals(Object obj)
{
if (this==obj) return true;
if (obj instanceof Person)
{ Person s = (Person)obj;
return super.equals(s) &&
this.name.equals(s.name) &&
this.brithdate.equals(s.brithdate) &&
this.gender.equals(s.gender) &&
this.province.equals(s.province) &&
this.city.equals(s.city);
}
return false;
}
public void set(String name,MyDate brithdate,String gender,String province,String city)
{
this.name=name==null?"":name;
this.brithdate=brithdate;
this.gender=gender==null?"":gender;
this.province=province==null?"":province;
this.city=city==null?"":city;
}
public void set(String name,MyDate brithdate)
{
this.set(name, brithdate,"","","");
}
public String toString()
{
return this.name+","+(this.brithdate==null?"":brithdate.toString())+","+this.gender+
","+this.province+","+this.city;
}
public static void main(String[] args)
{
Person p1=new Person("李小明",new MyDate(1994,3,15));
Person p2=new Person(p1);
Person.howMany();
System.out.println();
System.out.println("p1:"+p1+";p2:"+p2+"\np1=p2?"+(p1==p2)+";p1.name==p2.name?"
+(p1.name==p2.name)+",p1.brithdate==p2.drithdate?"
+(p1.brithdate==p2.brithdate));
p2.name="张"+p2.name.substring(1);
MyDate date=p2.brithdate;
date.set(date.getYear()+2,date.getMonth(),date.getDay());
System.out.println("p1:"+p1+";p2:"+p2);
// p1.finalize();
Person.howMany();
System.out.println();
System.out.println(p2.equals(p1));
}
}
以上是person类
package ObjectArray;
import MyDate.MyDate;
import Student.Student;
import Person.Person;
public class ObjectArray
{
public static void print(Object[] objs)
{
if(objs!=null)
for(int i=0;i<objs.length;i++)
System.out.println(objs[i]==null?"null":objs[i].toString());
}
public static Object[] concat(Object[] objs1,Object[] objs2)
{
if(objs1==null)
return objs2;
if(objs2==null)
return objs1;
Object[] result=new Object[objs1.length+objs2.length];
int i=0,j=0;
for(j=0;j<objs1.length;j++)
result[i++]=objs1[j];
for(j=0;j<objs2.length;j++)
result[i++]=objs2[j];
return result;
}
public static void searchPrintAll(Object[] objs,Object key)
{
if(objs!=null&&key!=null)
for(int i=0;i<objs.length;i++)
if(objs[i]!=null&&key.equals(objs[i]))
System.out.println(objs[i].toString());
}
public static void main(String[] args)
{
//Person p1=new Person("李小明",new MyDate(1994,3,15));
Person per=new Person("李小明",new MyDate(1994,3,15));
Person[] pers= {per,new Student(p1,"","计算机","",false)};
Student[] stus= {new Student("张莉",new MyDate(1998,4,5),"","","","","信息管理","",false),
new Student("朱红",new MyDate(1995,4,5),"","","","","通信工程","",false)};
Object[] objs=concat(pers,stus);
stus[1].brithdate.set(new MyDate(2001,10,1));
print(objs);
Person[] keys= {new Person(per),new Student((Student)pers[1])};
for(int i=0;i<keys.length;i++)
{
System.out.println("查找:"+keys[i].toString()+"结果: ");
searchPrintAll(objs,keys[i]);
}
Student.howMany();
}
}
以上是ObjectArray类
Person p1=new Person("李小明",new MyDate(1994,3,15));这条语句不注释掉就不报错,注释了的话 Person[] pers= {per,new Student(p1,"","计算机","",false)};这里面的p1老是报错,仿佛这个p1不存在