What is the difference between class and class type

Source: Internet
Author: User
What's the difference between class<t> and class<?> types?



T




object is the root class of all classes, a specific class, which may require type casts, but with T. When this is done, the type is determined before it is actually used, and no casting is required.
Ask:
In other words, this method can know which type (parent) to return, and use T to do it. Use it if you don't know it at all. The object obtained with T is not required for type conversion. Must be used with a strong turn.
Chase Answer:
 The first is a fixed generic, and the second is as long as subclasses of the object class can, in other words, any class can, because object is the base class of all classes 
Fixed generic reference types are fixed, such as: Interge,string. It's <t. Extends Collection>

<. Extends collection> here. Represents an unknown type,
However, this unknown type is actually a subclass of collection, and collection is the upper bound of the wildcard character.
For example
Class Test <t extends Collection> {}

<t extends collection>, where T is a certain type (concrete type) when constructing instances of this class. This type implements the collection interface,
But there are a lot of classes that implement the collection interface, and if you want to write specific subclass types for each one, that's too much trouble, and you might as well use the
object for general purposes.
The extends collection> of which,? is an unknown type, is a wildcard generic, this type is the implementation of the Collection interface.

_________________________ What ghosts are above, when you know the origin of the wildcard generics (the following code is provided by java1234.com) _______________________________ __________________________________________________________

The method take (Animal) in the type Test isn't applicable for the arguments (demo<dog>)
The method take (Animal) in the type Test isn't applicable for the arguments (demo<cat>)
The method take (Animal) in the type Test isn't applicable for the arguments (demo<animal>)

When generics are introduced, it is not appropriate for parameters to be written, and there are 2 methods that do not apply, and in order to write a generic method for generic classes, this is the time to introduce them. The concept of wildcard characters.


public class Demo <t extends animal>{

    private T ob;

    Public T Getob () {return
        ob;
    }

    public void Setob (T ob) {
        this.ob = ob;
    }

    Public Demo (T ob) {
        super ();
        THIS.OB = OB;
    }
    
    The type of public void print () {
        System.out.println ("t) is:" +ob.getclass (). GetName ());
    }

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.