The meaning of Java down transformation

Source: Internet
Author: User

When you start learning Java, you don't focus on down transformation. Have been confused about the meaning and purpose of the downward transformation, it is not clear that it is not, the development process must not unexpectedly use downward transformation.

In fact, the upward transformation and downward transformation are very important, we can usually see the upward transformation of a little more, upward transformation is also better understanding.
But the downward transformation, will not feel very silly, I was to use the subclass instance object, first generate subclass instance assignment to the parent class reference, in the parent class reference down to the child class reference, this is not superfluous? I do not transition upward or downward transformation, directly with the subclass instance on the line.
When I started to learn Java, I thought it was a mistake that led me to think that the downward transformation was useless.
With the improvement of technology, I am looking at Open source project learning, found that many places have used the technology of downward transformation, which let me pay attention to, want to re-review (learning) this knowledge point. Also searched a lot of blog posts, but did not specify the downward transformation, just gave an example of how to use, instead of upward transformation spoke a bunch (maybe I did not find).

This blog is about the downward transformation, then we will learn the downward transformation, to understand the significance of this feature and the use of the scene

Create an electronic product interface as follows:

public interface Electronics{}

It's simple, there's no way.

Create a new ThinkPad notebook class and implement an electronic product interface:

public class Thinkpad implements Electronics{ //Thinkpad引导方法 public void boot(){ System.out.println("welcome,I am Thinkpad"); } //使用Thinkpad编程 public void program(){ System.out.println("using Thinkpad program"); }}

Create a new mouse mouse class, and implement the electronic Product interface:

public class Mouse implements Electronics{ //鼠标移动 public void move(){ System.out.println("move the mouse"); } //鼠标点击 public void onClick(){ System.out.println("a click of the mouse"); }}

Create a new keyboard keyboard class, and implement the electronic Product interface:

public class Keyboard implements Electronics{ //使用键盘输入 public void input(){ System.out.println("using Keyboard input"); }}

There are many analogies here, for a better understanding. The logical implementation of the methods of each class is also simple. Print a line of information

Next, we imagine a scenario: we go to the mall to buy electronic products, many electronic products, such as laptops, mice, keyboards, step by step high reading machine where will not point where, we use the cell phone, and so on, these belong to electronic products. Electronic products are abstract. Well, then we decided to buy a ThinkPad, a mouse and a keyboard.
At this point, we need a shopping cart to install these electronic products. We can add into the shopping cart, and then through the shopping cart can also know the number of electronic products stored, can get the corresponding electronic products.
So, a shopping cart class comes out, as follows:

import java.util.arraylist;import Java.util.List; public class shopcar{private    list<electronics> mlist = new arraylist<electronics> (); public void add (Electronics Electronics) {Mlist.add (electronics);} public int getSize () {return mlist.size (); } public Electronics getlistitem ( int position) {return mlist. Get (position); }} 

The List collection is used to store electronic products, and the Add method is used to add electronic products to the shopping cart, the GetSize method is used to obtain the number of electronic products stored, and the Getlistitem method is used to obtain the corresponding electronic products.

You can see the List<Electronics> knowledge of generics, and why use generics? This does not introduce, generics are very important.
And I think the more puzzled is why is put Electronics generics, rather than put thinkpad,mouse,keyboard,phone etc?
So if it is List<Thinkpad> , it must not be put into the mouse mouse, do you want to generate 3 sets? Here is the definition of 3 electronic product classes, but if I have 100 kinds of electronic products, to define 100 sets?
This is horrible. So before we wrote a electronics interface that provided a electronics standard, and then let every electronics subclass implement this interface.

In fact, this involves the upward transformation of the knowledge point, although we in the Add method to pass the subclass instance into the store, but the subclass instance in the process of the transfer has also been the upward transformation
Therefore, the sub-class instance object that is stored in the shopping cart, due to the transformation into electronics, has lost the subclass unique method, with the above example to analyze, thinkpad instance is lost Boot () and program () These two methods, The mouse instance is missing the move () and the OnClick () two methods

But when you actually use ThinkPad or mouse or keyboard, it's definitely not what we want.

And then we write a test class test that tests the electronic products in the shopping cart.

The testing class test is as follows:

PublicClass test{PublicStatic finalint THINKPAD =0;PublicStatic finalint MOUSE =1;PublicStatic finalint KEYBOARD =2;PublicStaticvoidMain (string[] args) {Add into cart shopcar Shopcar =New Shopcar (); Shopcar.add (new Thinkpad ()); Shopcar.add (new Mouse ()); Shopcar.add (new Keyboard ()); //gets the size System. out.println ( "Shopping Cart stores the number of electronic products" +shopcar.getsize ()); //start testing Thinkpad computer Thinkpad Thinkpad = (Thinkpad) shopcar.getlistitem (Thinkpad); Thinkpad.boot (); Thinkpad.program (); System. out.println ( "-------------------"); //start test Mouse mouse Mouse Mouse = (Mouse) shopcar.getlistitem (Mouse); Mouse.move (); Mouse.onclick (); System. out.println ( "-------------------"); //start test Keyboard keyboard Keyboard Keyboard = (Keyboard) shopcar.getlistitem (Keyboard); Keyboard.input (); }}

Run:


Just an example of that.

//开始测试thinkpad电脑Thinkpad thinkpad = (Thinkpad)shopcar.getListItem(THINKPAD);thinkpad.boot();thinkpad.program();

shopcar.getListItem(THINKPAD)This code is obtained to an instance of the electronics type. Not an instance of ThinkPad.

Assign a reference to a subclass by shifting down

Thinkpad thinkpad = (Thinkpad)shopcar.getListItem(THINKPAD);

This way the class instances regain the Lost method (boot and program) because of the upward transformation

To sum it up, many times we need to throw all kinds of instance objects into a collection. (This sentence is very important)
In this example is the ThinkPad notebook, mouse mouse, keyboard keyboard and other instances of the object, all thrown into a shopcar shopping cart collection.
But it's impossible to give them every kind of a separate set to store it, this time we should find a standard, interface is a standard. These are all kinds of electronic products, abstracted into electronic products. Then a electronics interface comes out.
Back in the moment, we threw many kinds of instance objects into a collection. Maybe it's better to understand: Throw many kinds of subclass instance objects into the collection that holds the parent class instance.
After this process, the subclass instance has been assigned to the parent class reference (that is, it has completed the upward transformation), but unfortunately the method of the subclass extension has been lost.
Well, the Java language has a downward transformation feature that allows us to regain the Lost method, that is, strong back to the subclass
So when we need to use the subclass instance, we take it out of the parent collection and we can use the subclass instance object.

......

When I search for the meaning of Java down transformation, I get a better answer:
The greatest use of Java is the generic programming, the use of a large, Java collection classes are like this.

While in Android development, we are in the Layout folder, with XML-written controls. Why can I find it through the Findviewbyid () method in components such as activity? Why does the Findviewbyid (R.id.textview) method have to be transformed into TextView after passing in the TextView ID? This is one application of Java down transformation

The meaning of Java down transformation

Related Article

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.