Public class arraytest{ public static void main (String[] args) { animal[] a=new animal[ 3]; dog d1=new dog (); dog d2=new dog (); cat c1=new cat (); cat c2=new cat (); a[0]=d1; a[1]=d2; a[2]=c1; a[3]=c2; //requirements: Iterate through the array, take out each object, if it is dog, execute the Eat method, and if it is cat, execute the Move method. for (int i=0;i<a.length;i++) { animal an =&nbSp;a[i]; if ( an instanceof dog) { Dog d = (Dog) an; d.eat (); }else if (An instanceof cat) { cat c= (Cat) an; c.move (); } } } }//defines animals, dogs, cats, class animal{}class dog extends animal{ , and the like. Public void eat () { system.out.println ("dog eat! "); }} Class cat extends animal{ public void move () { system.out.println ("dog move! "); }}
This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1661766
Deep understanding of one-dimensional arrays of Java