The relationship of JavaScript and generics in object-oriented mode

Source: Internet
Author: User
Tags object extend interface string

When I was reading this article, I was reading "Dark Time" written by Mr. Liu Weipeng, and if you are lucky enough to see this blog, I hope you can also read this book. All right, gossip less, get to the point.

The idea of generics and polymorphism is embodied in object-oriented collection, but there is a certain similarity between generics and polymorphism, so let's first recognize generics and polymorphism before entering the topic.

Polymorphism is one of the most basic concepts of object-oriented, that is, to blur the difference between the parent class and the like, How to blur? Let's take that example on the Web again:

Class aninal{called;}//is a method of animal.

Class cat extend animal{called;

Class dog extend animal{called;

Animal dog = new Dog ()//Here the dog is defined by the Animal, it is not defined by the dog, it is the dog barking or the cat barking.

Animal cat = new Cat ()//Look at the object given to it, so that the interface reuse is truly implemented, an object can call

The dog is called ();//animal Let it be called, and the specific who is barking, then it depends on what the subclass is assigned to it, and at the same time

The cat. Call ()//expand, imagine that when the chicken is added, the previous method of invoking the animal object does not have to be modified.

The iterator of collection in Java is the most typical example of polymorphism, imagine how many kinds of collection (Arraylist,linkedlist,hashset,treeset, etc.) If you write a Iterarot for each class, you should know how bloated it will be. But let collection inherit from iterator (they all belong to the interface), and the other classes inherit from iterator. As an example:

Collection books = new HashSet ();
Books.add (15);
Iterator it = books.iterator ()//interface iterator when defining it, but it is implemented in the while (It.hasnext ())//hashset class in the initialization process Itera TR () method, that is, to fully see the subclass giving itself
{//What is, so that you can manipulate the objects in books. and easy to expand
int book = (int) it.next ();
SYSTEM.OUT.PRINTLN (book);
}

Then we know the concept of polymorphism and then look at generics. list list = new arraylist; This is the most common usage of generics, where T corresponds to the base type or custom class, such as int,string. In my opinion, the essence of introducing generics is to be safe and not to bring the errors that can be found at compile time to the runtime. For example, in the case of the books above, as long as objects that inherit from object in collection can be added, so you can save int, you can save string, you can also save custom class, when you save an object that cannot be cast to int, the int book = (int) it.next (); This statement is not able to discover errors at compile time and can only be found at run time, which is unstable for the entire system. After introducing generics, the type of the object in the list is restricted with T, and when the type of T is passed in, the program prompts for an error, allowing us to discover the error earlier.

Having said so much, let's look at the relationship between generics and polymorphism. Today, I saw a person on the internet said: Generics is a special case of polymorphism, the use of polymorphic to achieve generics. At first glance it seems to make sense that T can determine its behavior based on the type of incoming, is that polymorphism? But again, polymorphism is on the inheritance level, that is, according to the actual runtime to determine the specific implementation, and generics is when we use this generic class to determine the members of this class specific what type, the two are not a level, not to mention the use of polymorphic implementation of generics. As for the specific realization, we can check it out on our own.



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.