Deep understanding of one-dimensional arrays of Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.