2016年12月10日 星期六

EXOPlayer memo

    List&ltClass&lt? extends Extractor&gt&gt extractorClasses = new ArrayList&lt&gt();         
    try {
      extractorClasses.add(
          Class.forName("com.google.android.exoplayer2.extractor.mkv.MatroskaExtractor")
              .asSubclass(Extractor.class));

一、如何得到Class的对象呢?有三种方法可以的获取:
1、调用Object类的getClass()方法来得到Class对象,这也是最常见的产生Class对象的方法。例如:     MyObject x;     Class c1 = x.getClass();

2、使用Class类的中静态forName()方法获得与字符串对应的Class对象。例如:      Class c2=Class.forName("MyObject"),Employee必须是接口或者类的名字。

3、获取Class类型对象的第三个方法非常简单。如果T是一个Java类型,那么T.class就代表了匹配的类对象。例如     Class cl1 = Manager.class;     Class cl2 = int.class;     Class cl3 = Double[].class;     注意:Class对象实际上描述的只是类型,而这类型未必是类或者接口。例如上面的int.class是一个Class类型的对象。由于历史原因,数组类型的getName方法会返回奇怪的名字。

asSubclass

public <U> Class<? extends U> asSubclass(Class<U> clazz)

Casts this Class object to represent a subclass of the class represented by the specified class object. Checks that that the cast is valid, and throws a ClassCastException if it is not. If this method succeeds, it always returns a reference to this class object.

This method is useful when a client needs to "narrow" the type of a Class object to pass it to an API that restricts the Class objects that it is willing to accept. A cast would generate a compile-time warning, as the correctness of the cast could not be checked at runtime (because generic types are implemented by erasure).











沒有留言:

張貼留言