From:Java中泛型 Class
:https://www.cnblogs.com/zhaoyanhaoBlog/p/9362267.html
Class
Java中泛型 Class
Class 表示 T类型 的 字节码文件,
意思是:
Class 相当于Class c=T.class,T t new T() ;
或者 Class c= t.getClass();
通过以上可以获取类名为 c.getName();
解释:Class
ct 泛型指的是 ct 只能是 T 的字节码,而 c 可以是任何类的字节码。所以用 ct 用法更好
平时看 java源代码的时候,如果碰到了泛型,像 ?、T 、K 、V、 E 这些是经常出现的,但是不知道代表什么意思,今天特地整理下:
E --- Element (在集合中使用,因为集合中存放的是元素)
T --- Type(表示 Java 类型)
K V --- 分别代表 java 键值中的 Key Value。
N --- Number(数值类型)
? --- 表示 不确定的 Java类型
举例说明:
Set 表示 集合里 是 T类的实例
List 表示 集合里 是 E类的实例
List> 表示 集合里的对象类型不确定,未指定
List 同 List> 是一样的。
泛型的作用:
1、用泛型:
List list = new ArrayList();
T t=list.get(0);
2、不用泛型:
List list = new ArrayList();
T t = (T)list.get(0);
就像使用非泛型代码一样,有两种方式:
Class.forName() 被定义为返 回 Class>。另一方面,类常量 X.class 被定义为具有类型 Class
泛型的声明,必须在方法的修饰符(public,static,final,abstract等)之后,返回值声明之前。
public static T request2Bean(HttpServletRequest request,Class clazz){}
其中第一个
Object类 和 Class类 没有直接的关系。
平时看代码时,总是碰到这些即熟悉又陌生的名字,每天都与他们相见,但见面后又似曾没有任何的交集,所以今天我就来认识下这两个江湖侠客的背景:
Object 是所有类的根类,是具体的一个类,使用的时候可能需要类型强制转换的,但是用通配符 ?、T 、K 、V、 E 等这些的话,在实际用之前类型就已经确定了,不需要强制转换。
也就是说,如果一个方法能知道返回的是哪种类型(父类),就用 T,如果完全不知道的就用 ? 。。。用 T 得到的对象就不需要类型转换了,而用 ? 的就必需用强转了。
第一种是固定的一种泛型,第二种是只要是Object类的子类都可以,换言之,任何类都可以,因为Object是>所有类的根基类,固定的泛型指类型是固定的,比如:Interge, String 就是
<? extends Collection> 这里 ? 代表一个未知的类型,但是,这个未知的类型实际上是 Collection 的一个子类,Collection 是这个通配符的上限。
举个例子:class Test
************************ 上面讲的是什么鬼,当你知道引入通配符泛型的由来之后 ************************
当引入泛型之后,遇到这种情况,参数怎么写都不适合,总有2个方法不适用,为了给泛型类写一个通用的方法,这时候就需要引入了 ?通配符的概念。
示例代码:
public class Demo {
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());
}
}
在Java中,每个 class 都有一个相应的 Class 对象。也就是说,当我们编写一个类,编译完成后,在生成的.class文件中,就会产生一个 Class 对象,用于表示这个类的类型信息
获取 Class 实例的三种方式:
在 newInstance() 调用类中缺省的构造方法 ObjectnewInstance()(可在不知该类的名字的时候,创建这个类的实例) Creates a new instance of the class represented by this Classobject.
在运行期间,如果要产生某个类的对象,Java虚拟机(JVM)会检查该类型的Class对象是否已被加载。如果没有被加载,JVM会根据类的名称找到 .class 文件并加载它。一旦某个类型的Class对象已被加载到内存,就可以用它来产生该类型的所有对象
Class对象的生成方式如下:
1. Class.forName("类名字符串") (注意:类名字符串必须是全称,包名+类名)。
2. 类名.class ( Object.class )。
3. 实例对象.getClass() (object.getClass() )。
两者的区别如下:
例如:
ReflectClass.java
package com.android.peter.reflectdemo;
import android.content.Context;
import android.os.IBinder;
import android.util.Log;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Created by peter on 2018/8/22.
*/
public class ReflectClass {
private final static String TAG = "peter.log.ReflectClass";
// 创建对象
public static void reflectNewInstance() {
try {
Class> classBook = Class.forName("com.android.peter.reflectdemo.Book");
Object objectBook = classBook.newInstance();
Book book = (Book) objectBook;
book.setName("Android进阶之光");
book.setAuthor("刘望舒");
Log.d(TAG,"reflectNewInstance book = " + book.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 反射私有的构造方法
public static void reflectPrivateConstructor() {
try {
Class> classBook = Class.forName("com.android.peter.reflectdemo.Book");
Constructor> declaredConstructorBook = classBook.getDeclaredConstructor(String.class,String.class);
declaredConstructorBook.setAccessible(true);
Object objectBook = declaredConstructorBook.newInstance("Android开发艺术探索","任玉刚");
Book book = (Book) objectBook;
Log.d(TAG,"reflectPrivateConstructor book = " + book.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 反射私有属性
public static void reflectPrivateField() {
try {
Class> classBook = Class.forName("com.android.peter.reflectdemo.Book");
Object objectBook = classBook.newInstance();
Field fieldTag = classBook.getDeclaredField("TAG");
fieldTag.setAccessible(true);
String tag = (String) fieldTag.get(objectBook);
Log.d(TAG,"reflectPrivateField tag = " + tag);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 反射私有方法
public static void reflectPrivateMethod() {
try {
Class> classBook = Class.forName("com.android.peter.reflectdemo.Book");
Method methodBook = classBook.getDeclaredMethod("declaredMethod",int.class);
methodBook.setAccessible(true);
Object objectBook = classBook.newInstance();
String string = (String) methodBook.invoke(objectBook,0);
Log.d(TAG,"reflectPrivateMethod string = " + string);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 获得系统Zenmode值
public static int getZenMode() {
int zenMode = -1;
try {
Class> cServiceManager = Class.forName("android.os.ServiceManager");
Method mGetService = cServiceManager.getMethod("getService", String.class);
Object oNotificationManagerService = mGetService.invoke(null, Context.NOTIFICATION_SERVICE);
Class> cINotificationManagerStub = Class.forName("android.app.INotificationManager$Stub");
Method mAsInterface = cINotificationManagerStub.getMethod("asInterface",IBinder.class);
Object oINotificationManager = mAsInterface.invoke(null,oNotificationManagerService);
Method mGetZenMode = cINotificationManagerStub.getMethod("getZenMode");
zenMode = (int) mGetZenMode.invoke(oINotificationManager);
} catch (Exception ex) {
ex.printStackTrace();
}
return zenMode;
}
// 关闭手机
public static void shutDown() {
try {
Class> cServiceManager = Class.forName("android.os.ServiceManager");
Method mGetService = cServiceManager.getMethod("getService",String.class);
Object oPowerManagerService = mGetService.invoke(null,Context.POWER_SERVICE);
Class> cIPowerManagerStub = Class.forName("android.os.IPowerManager$Stub");
Method mShutdown = cIPowerManagerStub.getMethod("shutdown",boolean.class,String.class,boolean.class);
Method mAsInterface = cIPowerManagerStub.getMethod("asInterface",IBinder.class);
Object oIPowerManager = mAsInterface.invoke(null,oPowerManagerService);
mShutdown.invoke(oIPowerManager,true,null,true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void shutdownOrReboot(final boolean shutdown, final boolean confirm) {
try {
Class> ServiceManager = Class.forName("android.os.ServiceManager");
// 获得ServiceManager的getService方法
Method getService = ServiceManager.getMethod("getService", java.lang.String.class);
// 调用getService获取RemoteService
Object oRemoteService = getService.invoke(null, Context.POWER_SERVICE);
// 获得IPowerManager.Stub类
Class> cStub = Class.forName("android.os.IPowerManager$Stub");
// 获得asInterface方法
Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);
// 调用asInterface方法获取IPowerManager对象
Object oIPowerManager = asInterface.invoke(null, oRemoteService);
if (shutdown) {
// 获得shutdown()方法
Method shutdownMethod = oIPowerManager.getClass().getMethod(
"shutdown", boolean.class, String.class, boolean.class);
// 调用shutdown()方法
shutdownMethod.invoke(oIPowerManager, confirm, null, false);
} else {
// 获得reboot()方法
Method rebootMethod = oIPowerManager.getClass().getMethod("reboot",
boolean.class, String.class, boolean.class);
// 调用reboot()方法
rebootMethod.invoke(oIPowerManager, confirm, null, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
MainActivity.java
package com.android.peter.reflectdemo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private final static String TAG = "peter.log.ReflectDemo";
private Button mReboot;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mReboot = findViewById(R.id.btn_shutdown);
mReboot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 由于权限限制,并未生效
ReflectClass.shutDown();
ReflectClass.shutdownOrReboot(true,true);
}
});
}
@Override
protected void onResume() {
super.onResume();
try {
// 创建对象
ReflectClass.reflectNewInstance();
// 反射私有的构造方法
ReflectClass.reflectPrivateConstructor();
// 反射私有属性
ReflectClass.reflectPrivateField();
// 反射私有方法
ReflectClass.reflectPrivateMethod();
} catch (Exception ex) {
ex.printStackTrace();
}
Log.d(TAG," zenmode = " + ReflectClass.getZenMode());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
public class MainActivity extends AppCompatActivity {
private static final String TAG = "sakura";
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(stringWithJNI("sakura"));
// Log.d(TAG, stringFromJNI());
// Log.d(TAG, stringWithJNI("sakura"));
try {
testClass();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void testClass() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Test sakuraTest = new Test();
// 获得Class的方法(三种)
Class testClazz = MainActivity.class.getClassLoader().loadClass("myapplication.example.com.ndk_demo.Test");
Class testClazz2 = Class.forName("myapplication.example.com.ndk_demo.Test");
Class testClazz3 = Test.class;
Log.i(TAG, "Classloader.loadClass->" + testClazz);
Log.i(TAG, "Classloader.loadClass->" + testClazz2);
Log.i(TAG, "Classloader.loadClass->" + testClazz3.getName());
// 获得类中属性相关的方法
Field publicStaticField = testClazz3.getDeclaredField("publicStaticField");
Log.i(TAG, "testClazz3.getDeclaredField->" + publicStaticField);
Field publicField = testClazz3.getDeclaredField("publicField");
Log.i(TAG, "testClazz3.getDeclaredField->" + publicField);
//对于Field的get方法,如果是static,则传入null即可;如果不是,则需要传入一个类的实例
String valueStaticPublic = (String) publicStaticField.get(null);
Log.i(TAG, "publicStaticField.get->" + valueStaticPublic);
String valuePublic = (String) publicField.get(sakuraTest);
Log.i(TAG, "publicField.get->" + valuePublic);
//对于private属性,需要设置Accessible
Field privateStaticField = testClazz3.getDeclaredField("privateStaticField");
privateStaticField.setAccessible(true);
String valuePrivte = (String) privateStaticField.get(null);
Log.i(TAG, "modified before privateStaticField.get->" + valuePrivte);
privateStaticField.set(null, "modified");
valuePrivte = (String) privateStaticField.get(null);
Log.i(TAG, "modified after privateStaticField.get->" + valuePrivte);
Field[] fields = testClazz3.getDeclaredFields();
for (Field i : fields) {
Log.i(TAG, "testClazz3.getDeclaredFields->" + i);
}
// 获得类中method相关的方法
Method publicStaticMethod = testClazz3.getDeclaredMethod("publicStaticFunc");
Log.i(TAG, "testClazz3.getDeclaredMethod->" + publicStaticMethod);
publicStaticMethod.invoke(null);
Method publicMethod = testClazz3.getDeclaredMethod("publicFunc", java.lang.String.class);
Log.i(TAG, "testClazz3.getDeclaredMethod->" + publicMethod);
publicMethod.invoke(sakuraTest, " sakura");
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native String stringWithJNI(String context);
}
...
public class Test {
private static final String TAG = "sakura_test";
public static String publicStaticField = "i am a publicStaticField";
public String publicField = "i am a publicField";
private static String privateStaticField = "i am a privateStaticField";
private String privateField = "i am a privateField";
public static void publicStaticFunc() {
Log.d(TAG, "I`m from publicStaticFunc");
}
public void publicFunc(String str) {
Log.d(TAG, "I`m from publicFunc" + str);
}
private static void privateStaticFunc() {
Log.i(TAG, "I`m from privateFunc");
}
private void privateFunc() {
Log.i(TAG, "I`m from privateFunc");
}
}
...
...
12-26 23:57:11.784 17682-17682/myapplication.example.com.ndk_demo I/sakura: Classloader.loadClass->class myapplication.example.com.ndk_demo.Test
12-26 23:57:11.784 17682-17682/myapplication.example.com.ndk_demo I/sakura: Classloader.loadClass->class myapplication.example.com.ndk_demo.Test
12-26 23:57:11.784 17682-17682/myapplication.example.com.ndk_demo I/sakura: Classloader.loadClass->myapplication.example.com.ndk_demo.Test
12-26 23:57:11.785 17682-17682/myapplication.example.com.ndk_demo I/sakura: testClazz3.getDeclaredField->public static java.lang.String myapplication.example.com.ndk_demo.Test.publicStaticField
12-26 23:57:11.785 17682-17682/myapplication.example.com.ndk_demo I/sakura: testClazz3.getDeclaredField->public java.lang.String myapplication.example.com.ndk_demo.Test.publicField
12-26 23:57:11.785 17682-17682/myapplication.example.com.ndk_demo I/sakura: publicStaticField.get->i am a publicStaticField
12-26 23:57:11.785 17682-17682/myapplication.example.com.ndk_demo I/sakura: publicField.get->i am a publicField
12-26 23:57:11.785 17682-17682/myapplication.example.com.ndk_demo I/sakura: modified before privateStaticField.get->i am a privateStaticField
12-26 23:57:11.785 17682-17682/myapplication.example.com.ndk_demo I/sakura: modified after privateStaticField.get->modified
12-26 23:57:11.785 17682-17682/myapplication.example.com.ndk_demo I/sakura: testClazz3.getDeclaredFields->private java.lang.String myapplication.example.com.ndk_demo.Test.privateField
12-26 23:57:11.785 17682-17682/myapplication.example.com.ndk_demo I/sakura: testClazz3.getDeclaredFields->public java.lang.String myapplication.example.com.ndk_demo.Test.publicField
12-26 23:57:11.785 17682-17682/myapplication.example.com.ndk_demo I/sakura: testClazz3.getDeclaredFields->private static final java.lang.String myapplication.example.com.ndk_demo.Test.TAG
12-26 23:57:11.785 17682-17682/myapplication.example.com.ndk_demo I/sakura: testClazz3.getDeclaredFields->private static java.lang.String myapplication.example.com.ndk_demo.Test.privateStaticField
12-26 23:57:11.785 17682-17682/myapplication.example.com.ndk_demo I/sakura: testClazz3.getDeclaredFields->public static java.lang.String myapplication.example.com.ndk_demo.Test.publicStaticField
12-26 23:57:11.785 17682-17682/myapplication.example.com.ndk_demo I/sakura: testClazz3.getDeclaredMethod->public static void myapplication.example.com.ndk_demo.Test.publicStaticFunc()
12-26 23:57:11.785 17682-17682/myapplication.example.com.ndk_demo D/sakura_test: I`m from publicStaticFunc
12-26 23:57:11.786 17682-17682/myapplication.example.com.ndk_demo I/sakura: testClazz3.getDeclaredMethod->public void myapplication.example.com.ndk_demo.Test.publicFunc(java.lang.String)
12-26 23:57:11.786 17682-17682/myapplication.example.com.ndk_demo D/sakura_test: I`m from publicFunc sakura