Home  >  Article  >  Java  >  Detailed sample code explanation of the difference between class and class in java

Detailed sample code explanation of the difference between class and class in java

伊谢尔伦
伊谢尔伦Original
2017-07-19 11:38:353992browse

? Represents an undefined java type.

#T represents the java type.

K V respectively represent the Key Value in the java key value.

E stands for Element.

What is the difference between Object and the java types represented by these things?
Object is the root class of all classes and is a specific class. Type casting may be required when using it, but using T? If you wait for these, the type has been determined before actual use, and no forced conversion is required.

The first is a fixed generic type, and the second is as long as it is a subclass of the Object class. In other words, any class can be used, because Object is the root class of all classes.
Fixed The generic type refers to a fixed type, such as: Interge, String. Is it 2a9303dff3eed1b561b75d0dce9e5a56

4925b01b063da9c0d68a748519ed1936 here? Represents an unknown type,
However, this unknown type is actually a subclass of Collection, and Collection is the upper limit of this wildcard.
For example
class Test 2a9303dff3eed1b561b75d0dce9e5a56 { }

2a9303dff3eed1b561b75d0dce9e5a56Among them, T is a certain type (specific type) when constructing such an instance. This type implements the Collection interface,
but there are many classes that implement the Collection interface. , if you have to write a specific subclass type for each type, it would be too troublesome. It is better to use
Object to make it universal.
4925b01b063da9c0d68a748519ed1936 Among them, ? is an unknown type and a wildcard generic. This type only needs to implement the Collection interface.

The method take(Animal) in the type Test is not applicable for the arguments (Demo<Dog>)
The method take(Animal) in the type Test is not applicable for the arguments (Demo<Cat>)
The method take(Animal) in the type Test is not applicable for the arguments (Demo<Animal>)


public class Demo 5ad81ab85c1e7d5abd1bd5c95f1521b6{    private T ob;    public T getOb() {        return ob;
    }    public void setOb(T ob) {        this.ob = ob;
    }    public Demo(T ob) {        super();        this.ob = ob;
    }    
    public void print(){
        System.out.println("T的类型是:"+ob.getClass().getName());
    }
}
 public <T> Class<T> resolveAlias(String string) {    
    try {      
       if (string == null) {        
       return null;
      }      
      // issue #748
      String key = string.toLowerCase(Locale.ENGLISH);
      Class<T> value;      
      if (TYPE_ALIASES.containsKey(key)) {        
      value = (Class<T>) TYPE_ALIASES.get(key);
      } else {        
      value = (Class<T>) Resources.classForName(string);
      }      
      return value;
    } catch (ClassNotFoundException e) {      
    throw new TypeException("Could not resolve type alias &#39;" + string + "&#39;.  Cause: " + e, e);
    }
  }
public class Base {    
private static Map<String, Class<?>> map = new HashMap<>();    
static {        
map.put("string", String.class);        
map.put("integer", Integer.class);
    }
    @SuppressWarnings("unchecked")    
    public <T> Class<T> get(String str) {        
    return (Class<T>) map.get(str);
    }
    @Test    
    public void t() throws IllegalAccessException, InstantiationException {
        Base base = new Base();
        Class<Integer> string = base.get("string");
        System.out.println(string);
    }
}

The above is the detailed content of Detailed sample code explanation of the difference between class and class in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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