Home > Java > javaTutorial > 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

伊谢尔伦
Release: 2017-07-19 11:38:35
Original
4051 people have browsed it

? 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

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 { }

Among 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.
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>)
Copy after login


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;
    }    
    public void print(){
        System.out.println("T的类型是:"+ob.getClass().getName());
    }
}
Copy after login
 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);
    }
  }
Copy after login
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);
    }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template