声明一个一维数组,数组的每一个元素具有相同属性的对象,既对象数组:
//源代码
class animals
{
int legs,ears,eyes,mouth;
char fur;
String name;
animals(int slegs,int sears,int seyes,int smouth,char sfur,String sname)
{
legs=slegs;
ears=sears;
eyes=seyes;
mouth=smouth;
fur=sfur;
name=sname;
}
void bark()
{
System.out.println("WANG!WANG!WANG!");
}
protected void run()
{
System.out.println("GO!GO!GO!");
}
void setfur(char sfur)
{
fur=sfur;
}
void setname(String sname)
{
name=sname;
}
void sprint()
{
System.out.print("myname is:"+name+"\t");
System.out.print("I have:"+legs+"legs\t");
System.out.print(", "+ears+"ears\t");
System.out.print(", "+eyes+"eyes\t");
System.out.print(", "+mouth+"mouth\t");
System.out.println(",and I'm "+fur);
bark();
run();
}
}
public class zuoye2 extends animals
{
zuoye2(int slegs, int sears, int seyes, int smouth, char sfur, String sname) {
super(slegs, sears, seyes, smouth, sfur, sname);
// TODO Auto-generated constructor stub
}
public static void main(String arg[])
{
animals[] sanim=new zuoye2[3];
sanim[0]=new zuoye2(4,2,2,1,'B',"小黑");
sanim[1]=new zuoye2(4,2,2,1,'W',"小白");
sanim[2]=new zuoye2(4,2,2,1,'G',"小灰");
for(int i=0;i!=3;++i)
{
sanim[i].sprint();
}
}
}