And What does the distinction mean? What's the difference?

Source: Internet
Author: User
? Extends t> first of all, you can easily misunderstand the collection of all the classes that inherit from T, which is quite wrong, and I believe that you must have seen or used list&lt. Extends t> it. Why do I say that understanding a set is wrong. If understood as a set, then why not use list<t> to express. So, what? Extends t> is not a set, but the meaning of a certain seed of T, remember is a kind of, a single kind, the problem comes, because even which one is uncertain, bring uncertainty, so it is impossible to add () to add elements. You may also think why Add (T) not? Extends t> is a seed of T, a container that can be put into subclasses does not necessarily have to be in a superclass, that is, it is not possible to put T.

2.<? Super T> is easier to use here, No. Extends t> so many restrictions, this means that a class with a lower limit of t is simply a superclass of the T class. But why add (T) can do it. Because the container that can be put into a certain class can be put into its subclasses, the concept of polymorphism

If you have Java programming ideas, see the generics chapter for a couple of sections. Use and distinction of wildcard characters.

For example, to use a generic list, the elements of which are added to another list, you can use this way to prototype.

public static <T> void copy (list<? extends t> src, list<? Super t> Dest)



Analysis of Java Paradigm ****************************************

http://blog.csdn.net/andycpp/article/details/1748731


Starting with jdk1.5, Java is starting to support generics. Paradigm is a very useful programming tool, which brings us great flexibility. After reading "Java core programming", I have a small harvest, write to share with you.
The so-called paradigm, my feeling is that, regardless of the object's specific type, you can do a certain operation on the object, any object can do the same operation. That's where flexibility lies. However, precisely because the specific type of object is not considered, therefore, it is generally not possible to use the object's own interface functions, because different objects carry the interface function is not the same, you use Object a interface function, in case someone else to pass an object B to the paradigm, then the program will have errors, this is the limitations of the paradigm. So the best use of the paradigm is to implement the container class and implement a generic container. The container can store objects, or you can remove objects, regardless of the object's specific type. Therefore, when learning the paradigm, be sure to understand this, you can not expect the paradigm is omnipotent, to fully consider the limitations of the model. Let's look at the principles of paradigm and advanced applications. First, give a generic class: public class pair<t>
{
Public Pair () {i = null; second = NULL;}
Public Pair (T-second) {this.first = i; This.second = second; }

Public T GetFirst () {return A.}
Public T Getsecond () {return second;}

public void Setfirst (T newvalue) {i = newvalue;}
public void Setsecond (T newvalue) {second = newvalue;}

Private T-A;
Private T second;
}
We see that the above pair class is a container class (I would stress that the paradigm is inherently for the convenience of the container Class) and holds 2 data, but the 2 data types are indeterminate and are represented by a generic T. As for how the generic class is used, it is the most basic content and is not discussed here.
Let's discuss the implementation principle of the Java Paradigm class. In Java, generics are implemented in the compiler, not in virtual machines, and virtual machines are ignorant of the paradigm. Therefore, the compiler must change the generic class to a generic class to be able to execute in the virtual machine. In Java, this technique is called "erase", which is to replace the paradigm with the object type. The above code is erased and becomes the following form: public class Pair
{
Public Pair (Object A, object second)
{
This.first = A;
This.second = second;
}

Public Object GetFirst () {return a
Public Object Getsecond () {return second;}

public void Setfirst (Object newvalue) {i = newvalue;}
public void Setsecond (Object newvalue) {second = newvalue;}

Private Object A;
Private Object second;
}
As you can see, this is a generic class, all of which are replaced by the object type, which is called the native class. Whenever you instantiate the paradigm with a specific class, the compiler satisfies the requirements by enforcing the constraints and adding the enhanced conversion code where needed, but does not generate more specific classes (this is completely different from C + +), based on the native class. Let's illustrate this point: pair<employee> buddies = new pair<employee> ();

In the above native code, where the parameter type is object, the various types are theoretically acceptable, but the compiler passes the Force constraint
You can only use an employee (and subclass) type of argument here, and all other types of compilers will complain
Buddies.setfirst (New Employee ("John"));

In the native code above, the return value of GetFirst () is an object type that is not directly assigned to a buddy of type employee
But the compiler has done the hands and feet, added a mandatory conversion code, the actual code should be employee Buddy = (employee) Buddies.getfirst ();
This is legal. But you can't see the code that has been tampered with by the compiler, and he's done it in byte-code form.
Employee buddy = Buddies.getfirst ();
        let's look at a more complicated scenario, if our pair class is going to make sure that the second attribute must be greater than the first attribute. This involves a comparison of two properties, but these 2 property types are unknown, can be compared. As we have said before, it is generally not relevant to type specific information. But now to compare 2 attributes, you have to deal with the specific information of the type. Java still takes this into account, that is, the generic class can inherit from a parent class, or implement an interface, or simultaneously inherit the parent class and implement the interface. In this way, you can call the parent class or the method defined in the interface on the type. The code is as follows:

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.