Off-topic: summed up many years of learning experience, have to say, sleep is a learner's necessities! The so-called "early to ruin the day" is not unreasonable, especially for coders, a few days is not overtime to Night. well, I admit to beginners java, I yesterday, the head of the road, Java inheritance and polymorphism, see I was a little confused force. In fact, basically understand, but to the specific application, especially the more complex inheritance and call the relationship when a little Lost. well, the words do not say much, tomorrow will inherit and polymorphic to a summary, today first on the polymorphism to a small note.
--------------------------------------------------------------------------------------------------------------- ----
Now we're going to do a media library for CDs and DVDS. You can add CDs and DVDs to your media library, and list all the CDs and DVDs in your media library.
We used the concept of inheritance: the CD is an object, and so is the Dvd. There are a lot of similarities between the two, for example, there are names, play time, comments, and so on, in order to facilitate the management and debugging, we did a parent class called item, it has two sub-classes: Cd,dvd.
Then we need to do a class (object) to manage them, an object that has a ArrayList member variable as a media library, an add function to add a CD or dvd, and a list function to list everything in the Library. We named this class Database.
So we're going to do the following four Java source files:
Source:
1 packagedome;2 3 Importjava.util.ArrayList;4 5 public classDatabase {6 7 PrivateArraylist<item> ListItem =NewArraylist<item>();8 9 public voidAdd (item Item) {Ten Listitem.add (item); one } a - public voidlist () { - for(Item Item:listitem) { the Item.print (); - } - } - + public Static voidmain (string[] Args) { -Database db =NewDatabase (); +Db.add (NewCD ("black dream", 600,true, "Classic Album", "dou wei", 10)); aDb.add (NewDVD ("kung fu", 900,false, "let's do this!", "stephen Chow chi")); atDb.add (NewCD ("Wall", 700,false, "psychedelic Rock", "pinkfloyd", 15)); - db.list (); - } - -}Database.java
1 packagedome;2 3 public classItem {4 5 PrivateString title;6 Private intplayTime;7 Private BooleanGotit =false;8 PrivateString comment;9 Ten publicItem (String title,intplayTime,Booleangotit, String Comment) { one Super(); a this. title =title; - this. PlayTime =playTime; - this. Gotit =gotit; the this. Comment =comment; - } - - public voidprint () { +System.out.print (title+ ":" +playtime+ ":" +gotit+ ":" +comment); - } + a}Item.java
1 packagedome;2 3 public classCdextendsItem {4 5 PrivateString artist;6 Private intnumoftracks;7 8 publicCD (String title,intplayTime,Booleangotit, string comment, string artist,intNumoftracks) {9 Super(title, playTime, gotit, comment);Ten this. Artist =artist; one this. Numoftracks =numoftracks; a } - - public voidprint () { theSystem.out.print ("CD:"); - Super. Print (); -System.out.println (":" +artist+ ":" +numoftracks); - } + -}Cd.java
1 packagedome;2 3 public classDvdextendsItem {4 5 PrivateString director;6 7 publicDVD (String title,intplayTime,Booleangotit, string comment, string Director) {8 Super(title, playTime, gotit, comment);9 this. Director =director;Ten } one a public voidprint () { -System.out.print ("DVD:"); - Super. Print (); theSystem.out.println (":" +director); - } - -}Dvd.java
Run:
Here are two concepts:
polymorphic variables : Here you can say that there are two subclasses under a parent class, the 9th row in Database: item item, which defines an object variable item, this item is the item class, and this item is just a variable, a manager of an object, But there is no specific object to take care Of. You can assign a CD to the item in the main function, or you can assign a DVD to it, so the item variable is to be run according to the program to know what object he is Managing. This item is a polymorphic variable.
up : To shape is to take a subclass of objects to the parent class object variable, or take a subclass of the object when the parent class object to use, as in database db.add, we can take a CD or DVD to the item this item class object Variable. The upward styling is always safe.
--------------------------------------------------------------------------------------------------------------- ----
Then there is polymorphism:
In the process of database running, line 15th, There is a sentence item.print
We can see that this is the print function in the call to item, but we see from the program running results that it is not so, the result of the operation is obviously called Cd.print (), and Dvd.print ();
Item.print ():
Cd.print ():
Dvd.print ():
This is explained by Polymorphism: first, when a function with the same name is in the subclass and parent class, the function in the subclass overwrites (override) the same name function inherited from the parent class, and the function from the parent class is Hidden. Then the item in the database 15th row will see what it manages its own item class or his subclass, and if It's a subclass, then this print function will use the print function of its subclass.
Java Basic learning-polymorphism