Home > 类库下载 > java类库 > Java basic learning

Java basic learning

高洛峰
Release: 2016-10-09 16:50:56
Original
2052 people have browsed it

Now we are going to make such a media library to store CDs and DVDs. You can add CDs and DVDs to the media library, and list all CDs and DVDs in the media library.

We use the concept of inheritance: CD is an object, and so is DVD. The two have many similarities, such as names, playing time, comments, etc. In order to facilitate management and debugging in the future, we made a parent class called Item, which has two subclasses: CD and DVD.

Then we need to make a class (object) to manage them. This object has a member variable of ArrayList as the media library, an add function to add CDs or DVDs, and a list function to list them. Everything in the media library. We name this class Database.

In this way we have to make the following four java source files:

Java basic learning

Source code:

package dome;

import java.util.ArrayList;

public class Database {
    
    private ArrayList<Item> listItem = new ArrayList<Item>();
    
    public void add(Item item) {
        listItem.add(item);
    }
    
    public void list() {
        for(Item item:listItem) {
            item.print();
        }
    }
    
    public static void main(String[] args) {
        Database db = new Database();
        db.add(new CD("黑梦",600,true,"Classic Album","窦唯",10));
        db.add(new DVD("功夫",900,false,"Let&#39;s do this!","周星驰"));
        db.add(new CD("Wall",700,false,"Psychedelic Rock","PinkFloyd",15));
        db.list();
    }
    
}

Database.java
Copy after login
package dome;

public class Item {

    private String title;
    private int playTime;
    private boolean gotIt = false;
    private String comment;
    
    public Item(String title, int playTime, boolean gotIt, String comment) {
        super();
        this.title = title;
        this.playTime = playTime;
        this.gotIt = gotIt;
        this.comment = comment;
    }
    
    public void print() {
        System.out.print(title+":"+playTime+":"+gotIt+":"+comment);
    }
    
}

Item.java
Copy after login
package dome;

public class CD extends Item {

    private String artist;
    private int numofTracks;
    
    public CD(String title, int playTime, boolean gotIt, String comment, String artist, int numofTracks) {
        super(title, playTime, gotIt, comment);
        this.artist = artist;
        this.numofTracks = numofTracks;
    }
    
    public void print() {
        System.out.print("CD:");
        super.print();
        System.out.println(":"+artist+":"+numofTracks);
    }
    
}

CD.java
Copy after login
package dome;

public class DVD extends Item {
    
    private String director;

    public DVD(String title, int playTime, boolean gotIt, String comment, String director) {
        super(title, playTime, gotIt, comment);
        this.director = director;
    }
    
    public void print() {
        System.out.print("DVD:");
        super.print();
        System.out.println(":"+director);
    }
    
}

 DVD.java
Copy after login

Run:

Java basic learning

There are two concepts here:

Polymorphic variables: It can be said here, one There are two subclasses under the parent class. Line 9 in Database: Item item defines an object variable item. This item is the Item class. This item is just a variable and is the manager of an object, but there is no specific requirement yet. The object to take care of. In the main function, you can assign a CD or DVD to the item, so the item variable can only know what object it wants to manage according to when the program is running. This item is a polymorphic variable.

Upward modeling: Think of upward modeling as giving a subclass object to an object variable of the parent class, or using a subclass object as an object of the parent class. Just like db.add in Database, we can use A CD or DVD is given to the Item class object variable of item. It's always safe to style upward.

------------------------------------------------ -------------------------------------------------- ------------------

Then there is polymorphism:

In the process of Database running, on line 15, there is the sentence item.print

Java basic learning

We can see It seems that this is calling the print function in Item, but we can see from the program running results that this is not the case. The running results show that it is clearly the result of calling CD.print(); and DVD.print();.

Item.print():

Java basic learning

CD.print():

Java basic learning

DVD.print():

Java basic learning

This needs to be explained using polymorphism: First, when the subclass and parent When there is a function with the same name in the class, the function in the subclass will override the function with the same name inherited from the parent class, so the function from the parent class will be hidden and will not work. Then the item in line 15 of the Database will see whether it manages its own Item class or its subclass. If it is its subclass, then the print function will use the print function of its subclass.

Java basic learning

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template