java, golang, python generics, exceptions, decorators and other examples

java generic

  • The following program will not find errors during compilation, but errors will occur at run time

    java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
    
    List array_list = new ArrayList();
    array_list.add("aaaa");
    array_list.add(100);
    
    for (int i = 0; i < array_list.size(); i++) {
      String item = (String)array_list.get(i);
      Log.d("Generic testing", "item=" + item)
    }
    
  • We want the program to find this error in the compilation phase, and the program can be rewritten as

    List<String> array_list = new ArrayList<String>();
    array_list.add("aaaa");
    array_list.add(100);
    
    for (int i = 0; i < array_list.size(); i++) {
      String item = (String)array_list.get(i);
      Log.d("Generic testing", "item=" + item)
    }
    

In this way, the above program will find errors in the compilation phase to avoid bug s.

  • Properties of generics

    List<String> string_array_list = new ArrayList<String>();
    List<Integer> integer_array_list = new ArrayList<Integer>();
    
    Class class_string_array_list = string_array_list.getClass();
    Calss class_integer_array_list = integer_array_list.getClass(); 
    
    if (class_string_array_list.equals(class_integer_array_list)) {
      Log.d("Generic testing", "The output result is the same type")
    }
    
    

Generic types are logically considered to be many different types, in fact, they are all the same basic types.

There are three ways to use generics: generic class, generic interface and generic method

  • Generic class
# A general generic class

//Here T can be written as any identifier. Common parameters such as T, E, K, V are often used to represent generics
//When instantiating a generic class, you must specify the concrete type of T

public class Generic<T> {
  //The type of the member variable key is t, and the type of T is specified externally  
  private T key;

   //The type of generic constructor parameter key is also t, and the type of T is specified externally
  public Generic(T key) {
    this.key = key;
  }

  //The return value type of the generic method getKey is t, and the type of T is specified externally
  public T getKey() {
    return this.key;
  }
}


// Instantiate the above class

// integer
Generic<Integer> generic_integer = new Generic<Integer>(12345);
Log.d("Generic testing", "key is " + generic_integer.getKey());


// character
Generic<String> generic_string = new Generic<String>("hello");
Log.d("Generic testing", "key is " + generic_string.getKey());


Generic generic = new Generic("111111");
Generic generic1 = new Generic(4444);
Generic generic2 = new Generic(55.55);
Generic generic3 = new Generic(false);

Log.d("Generic testing","key is " + generic.getKey());
Log.d("Generic testing","key is " + generic1.getKey());
Log.d("Generic testing","key is " + generic2.getKey());
Log.d("Generic testing","key is " + generic3.getKey());


D/Generic testing: key is 111111
D/Generic testing: key is 4444
D/Generic testing: key is 55.55
D/Generic testing: key is false

  • generic interface

# Define a generic interface
public interface Generator<T> {
  public T next();
}


/**
 * When a generic argument is not passed in, it is the same as the definition of a generic class. When declaring a class, you need to add the declaration of the generic to the class
 * That is: class fruit generator < T > implements generator < T >{
 * If you do not declare generics, such as: class fruit generator implements generator < T >, the compiler will report an error: "Unknown class"
 */
class FruitGenerator<T> implements Generator<T>{
    @Override
    public T next() {
        return null;
    }
}


public class FruitGenerator implements Generator<String> {
  private String[] fruits = new String[]{"apple", "banana", "pear"};

  @Override
  public String next() {
    Random random = new Random();
    return fruits[random.nextInt(3)];
  }
}

  • Generic wildcard

Keywords: Java

Added by kykin on Sun, 22 Dec 2019 20:19:57 +0200