Practice angry birds (abstract classes, polymorphism, rewriting)

The code is as follows:

//Birds:

Public abstract class bird {/ / birds can fly, but birds of different colors can fly in different ways. Public abstract void fly();

}

//Bluebird class:

/** Blue Ice: blue bird, flight mode: divided into 3*/

Public class Bluebird extensions bird {/ / override the abstract method @ override public void fly() {system.out.println ("blue ice, flight can change from one to three");}}

//Redbird class:

/** Red Fire: red bird, flight mode: normal, * / public class Redbird extensions bird {/ / override the abstract method @ override public void fly() {system.out.println ("red bird, normal flight");}

}

//Yellowbird class:

/** yellow wind: yellow bird, flight mode: accelerate * / public class Yellowbird extensions bird {/ / override the abstract method @ override public void fly() {system.out.println ("yellow wind, can accelerate flight");}

}

//Slingshot class:

/** slingshot: there is a launching method to pop up the bird * / public class slingshot {public void shot (bird bird) {bird. Fly();}

}

//Test demo class:

Public class demo {public static void main (string [] args) {/ / create a slingshot slingshot = new slingshot(); / / create red, yellow and blue birds Redbird = new redbird(); Bluebird = new bluebird(); Yellowbird Yellowbird = new yellowbird(); / / eject slingshot.shot (Redbird); slingshot.shot (Bluebird); slingshot.shot (Yellowbird);}}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>