This commit is contained in:
tensorflow-jumao
2021-07-10 01:50:21 +08:00
parent 4bf855c36d
commit 806de33657
7 changed files with 645 additions and 143 deletions

113
Java/code/ClassEx.java Normal file
View File

@@ -0,0 +1,113 @@
package ;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
enum E { A, B };//枚举类型E
public class ClassEx {
public static void main(String[] args) throws Exception {
//第1种获取Class实例的方法 :对象名.getClass()原始类型、接口类型对应的Class实例不能用这种方法获取
//类类型
Class c = "foo".getClass();
System.out.println(c);
c = System.out.getClass();
System.out.println(c);
Date date=new Date();
c=date.getClass();
System.out.println(c);
//接口类型
List<Date> array=new ArrayList<Date>();
c=array.getClass();
System.out.println(c);
//数组类型
int[] a=new int[10];
c=a.getClass();
System.out.println(c+" "+c.getCanonicalName());
char[] b=new char[10];
c=b.getClass();
System.out.println(c+" "+c.getName());
double[] d=new double[10];
c=d.getClass();
System.out.println(c);
long[] l=new long[10];
c=l.getClass();
System.out.println(c);
Date[] f=new Date[10];
c=f.getClass();
System.out.println(c+" "+c.getCanonicalName());
//枚举类型
E e=E.A;
c=e.getClass();
System.out.println(c);
System.out.println("*****************************");
//基本类型,编译错误
//int a=10;
//c=a.getClass();
//第2种获取Class实例的方法 :类型名.class所有类型对应的Class实例都可以用这种方法获取
c=String.class;
System.out.println(c);
c=Date.class;
System.out.println(c);
c=List.class;
System.out.println(c);
c=int.class;
System.out.println(c);
c=int[][].class;
System.out.println(c);
c=Date[][].class;
System.out.println(c);
c=E.class;
System.out.println(c);
System.out.println("*****************************");
//第3种获取Class实例的方法 Class.forName(类全名字符串)原始类型对应的Class实例不可以用这种方法获取
c=Class.forName("java.lang.String");
System.out.println(c);
c=Class.forName("java.util.List");
System.out.println(c);
c=Class.forName("[I");//int数组类型int[]
System.out.println(c);
c = Class.forName("[D");//double型数组double[]
System.out.println(c);
c = Class.forName("[[Ljava.lang.String;");//String二维数组String[][]
System.out.println(c);
c=Class.forName("类.E");
System.out.println(c);
System.out.println("*****************************");
//基本类型包装类中的TYPE成员的值是基本类型对应的Class类实例
c=Double.TYPE;
System.out.println(c);
c=Void.TYPE;
System.out.println(c);
}
}

54
Java/code/Deet.java Normal file
View File

@@ -0,0 +1,54 @@
package 方法;
import static java.lang.System.out;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Locale;
public class Deet<T> {
private boolean testDeet(Locale l) {
out.format("Locale = %s, Language Code = %s%n", l.getDisplayName(), l.getLanguage());
return true;
}
private boolean testFoo(Locale l) {
return false;
}
private boolean testBar() {
return true;
}
public static void main(String... args) {
try {
//获取Deet类的Class实例
Class<?> c = Deet.class;
//有Class实例创建一个类对象
Object t = c.newInstance();
//获取Class实例对应的类型声明的所有的方法
Method[] allMethods = c.getDeclaredMethods();
for (Method m : allMethods) {
String mname = m.getName();//获取方法的名字
//如果方法的名字不以test开头或者返回类型不是boolean型
if (!mname.startsWith("test") || (m.getReturnType() != boolean.class)) {
continue;
}
//获取方法的所有参数类型
Type[] pType = m.getGenericParameterTypes();
//如果有参数并且第一个参数是Locale类型
if ((pType.length != 1) || Locale.class.isAssignableFrom(pType[0].getClass())) {
continue;
}
out.format("调用 %s()%n", mname);
try {
//调用方法o是方法返回值
Object o = m.invoke(t, Locale.CHINA);
out.format("%s() 返回 %b%n%n", mname, (Boolean) o);
} catch (InvocationTargetException x) {
x.printStackTrace();
} }
} catch (InstantiationException x) {
x.printStackTrace();
} catch (IllegalAccessException x) {
x.printStackTrace();
}
}
}

78
Java/code/RefEx.java Normal file
View File

@@ -0,0 +1,78 @@
package test;
import static java.lang.System.out;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
public class RefEx {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="hello";
Class c=s.getClass();//获取类型
System.out.println(c);
String modifiers=Modifier.toString(c.getModifiers());//获取修饰符
System.out.println(modifiers);
TypeVariable<?>[] tv = c.getTypeParameters();//获取泛型类型参数
if (tv.length != 0) {
out.format(" ");
for (TypeVariable<?> t : tv)
out.format("%s ", t.getName());
out.format("%n%n");
} else {
out.format(" -- No Type Parameters --%n%n");
}
Class superclass=c.getSuperclass();//获取类型的父类
System.out.println(superclass);
Type[] intfs = c.getGenericInterfaces();//获取类实现的接口
if (intfs.length != 0) {
for (Type intf : intfs)
out.format(" %s%n", intf.toString());
out.format("%n");
} else {
out.format(" -- No Implemented Interfaces --%n%n");
}
Annotation[] ann = c.getAnnotations();//获取类的注解
if (ann.length != 0) {
for (Annotation a : ann)
out.format(" %s%n", a.toString());
out.format("%n");
} else {
out.format(" -- No Annotations --%n%n");
}
Constructor[] cons=c.getDeclaredConstructors();//获取类的构造方法
for(Constructor con:cons){
System.out.println(con.toGenericString());
}
out.format("%n%n");
Field[] fs=c.getDeclaredFields();//获取类的数据成员(成员变量)
for(Field f:fs){
System.out.println(f.toGenericString());
}
out.format("%n%n");
Method[] ms=c.getMethods();//获取类的公有成员方法(包含从父类继承的)
for(Method m:ms){
System.out.println(m.toGenericString());
}
out.format("%n%n");
}
}

87
Java/code/Test.java Normal file
View File

@@ -0,0 +1,87 @@
package test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
public static String classname="类.Student";
public static String fieldname="name";
public static String methodname="setName";
public static String[] mptypes= {"java.lang.String"};
public static Object fvalue=new String("xiaolin");
public static Object mp1=new String("xiaohong");
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class c=Class.forName(classname);//根据类名字符串获得类对应的Class实例
//第一种用反射创建类对象的方法,相当于调用类的无参数构造方法
Object o=c.newInstance();//Student o=new Student();
//使用反射访问类对象的属性(给属性设置值,获取属性的值)
Field f=c.getDeclaredField(fieldname);//根据Field名获得Field对象
f.setAccessible(true);
f.set(o,fvalue);//o.name="xiaolin";使用反射给o对象的f代表的属性设置值为发value的值
System.out.println(f.get(o));//获取o对象的f属性对应的值并输出
@SuppressWarnings("unchecked")
//使用反射调用对象的方法
//使用反射根据方法名,和参数类型、参数数量来获取具体某个方法
Method m=c.getDeclaredMethod(methodname, Class.forName(mptypes[0]));//String.class
//方法的调用调用o对象的m方法方法的实际参数是mp1mo是方法调用的返回值
Object mo=m.invoke(o,mp1);//o.setName("xiaohong");
System.out.println(mo);
//调用了o对象名为setAge参数有1个类型是int型的方法实际参数是21
m=c.getDeclaredMethod("setAge", int.class);
m.invoke(o, 21);
System.out.println(o);
/*Method m=c.getDeclaredMethod(methodname);
Object mo=m.invoke(o);
System.out.println(mo);
System.out.println(f.get(o));*/
@SuppressWarnings({ "unchecked", "rawtypes" })
//第二种用反射创建类对象的方法先获取具体的Constructor再用Constructor去创建对象
Constructor con=c.getDeclaredConstructor(String.class,int.class);//根据构造方法参数类型来获取某个构造方法
Object o1=con.newInstance("Mary",20);
//用Constructor创建类对象相当于Student o1=new Student("Mary",20);
System.out.println(o1);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}