Java Run-time polymorphism, late-bound initialization order

Source: Internet
Author: User

Package polymorphism.rodent; Import static net.mindview.util.print.*; Class Characteristic {private String s; Characteristic (String s) {this.s = s; print ("Creating characteristic" + s);} Class Description {private String s; Description (String s) {this.s = s; print ("Creating Description" + s);} Class Rodent {private String name = "Rodent"; private characteristic c = new characteristic ("has tail"); private descript Ion D = new Description ("small mammal"); Rodent () {print ("rodent ()");} protected void Eat () {print ("rodent.eat ()");} protected void Run () {print ("Rodent.run") "); } protected void Sleep () {print (' rodent.sleep () ');} public String toString () {return name;}} Class Mouse extends Rodent {private String name = "Mouse"; private characteristic c = new characteristic ("likes cheese"); Private Description D = new Description ("nocturnal"); Mouse () {print ("Mouse ()");} protected void Eat () {print ("mouse.eat ()");} protected void Run () {print ("Mouse.run ()"); } protected void Sleep () {print (' mouse.sleep () ');} public String toString () {return name;}} Class Rat extends Rodent {private String name = "Rat"; private characteristic c = new characteristic ("larger"); private D Escription d = new Description ("Black"); Rat () {print ("Rat ()");} protected void Eat () {print ("rat.eat ()");} protected void Run () {print ("Rat.run ()");} Protec ted void Sleep () {print ("rat.sleep ()"),} public String toString () {return name;}} Class Squirrel extends Rodent {private String name = "Squirrel"; private characteristic c = new characteristic ("climbs tr Ees "); Private Description D = new Description ("Likes nuts"); Squirrel () {print ("Squirrel ()");} protected void Eat () {print ("squirrel.eat ()");} protected void Run () {print ("Squirr El.run () "); } protected void Sleep () {print (' squirrel.sleep () ');} public String toString () {return name;}} public class Rodent12 {private static Randomrodentgenerator Gen = new Randomrodentgenerator (), public static void main (String[] args) {rodent[] rodents = new RODENT[10]; for (rodent r:rodents) {r = Gen.next (); System.out.println (R); System.out.println ("==============================="); } } }

Results:

Creating characteristic has tail
Creating Description small mammal
Rodent ()
Creating characteristic larger
Creating Description Black
Rat ()
Rat
===============================
Creating characteristic has tail
Creating Description small mammal
Rodent ()
Creating characteristic likes cheese
Creating Description Nocturnal
Mouse ()
Mouse
===============================
Creating characteristic has tail
Creating Description small mammal
Rodent ()
Creating characteristic climbs trees
Creating Description Likes Nuts
Squirrel ()
Squirrel
===============================
Creating characteristic has tail
Creating Description small mammal
Rodent ()
Creating characteristic climbs trees
Creating Description Likes Nuts
Squirrel ()
Squirrel
===============================
Creating characteristic has tail
Creating Description small mammal
Rodent ()
Creating characteristic likes cheese
Creating Description Nocturnal
Mouse ()
Mouse
===============================
Creating characteristic has tail
Creating Description small mammal
Rodent ()
Creating characteristic larger
Creating Description Black
Rat ()
Rat
===============================
Creating characteristic has tail
Creating Description small mammal
Rodent ()
Creating characteristic climbs trees
Creating Description Likes Nuts
Squirrel ()
Squirrel
===============================
Creating characteristic has tail
Creating Description small mammal
Rodent ()
Creating characteristic climbs trees
Creating Description Likes Nuts
Squirrel ()
Squirrel
===============================
Creating characteristic has tail
Creating Description small mammal
Rodent ()
Creating characteristic larger
Creating Description Black
Rat ()
Rat
===============================
Creating characteristic has tail
Creating Description small mammal
Rodent ()
Creating characteristic likes cheese
Creating Description Nocturnal
Mouse ()
Mouse
===============================

Description, when a run-time polymorphism occurs when the shape is on, the base class is initialized first, then the subclass is initialized, and then the method of the child class is later bound.

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.