package io; import java.io.File; public class GetAllSubclass { public static void main(String[] args) { File rootFile = new File(GetAllSubclass.class.getResource("/").getFile().replaceFirst("/", "")); setSonList(rootFile, rootFile.getPath() + "\\", GetAllSubclass.class); } public static <T> void setSonList(File rootFile, String parentDirectory, Class<T> parentClass) { if (rootFile.isDirectory()) { File[] files = rootFile.listFiles(); for (File file : files) { setSonList(file, parentDirectory, parentClass); } } else { String className = null; try { if (rootFile.getPath().indexOf(".class") != -1) { className = rootFile.getPath().replace(parentDirectory, "").replace(".class", "").replace("\\", "."); Class<?> classObject = Class.forName(className); classObject.asSubclass(parentClass); System.out.println(className + " 是 " + parentClass + " 的子类"); } } catch (ClassNotFoundException e) { System.err.println("找不到类 " + className); } catch (ClassCastException e) { System.err.println(className + " 不是 " + parentClass + " 的子类"); } } } } class Sub1 extends GetAllSubclass { } class Sub2 extends GetAllSubclass { }
Java中数组有自己的方法和成员变量,数组绝逼是对象,同事Super[] superArr;Sub[],arraylist是源码解析
public class ArrrAndGenericTest {
public static void main(String[] args){
test();
}
public static void test(){
Byte a = 127;
Byte b = 127;
System.out.println(++a);
add(++a);
System.out.println("a =" + a);
add(b);
System.out.println("b =" + b);
}
public static void add(Byte b){
b++;
}
public static void isArrayMethod() {
// 判断Java对象是否为数组
Integer[] ia = new Integer[6];
Number[] na;
String[] strs = new String[7];
System.out.println(ia instanceof Number[]);
System.out.println(ia instanceof Integer[]);
System.out.println(ia.getClass().isArray());
System.out.println(strs.getClass());
}
/**
* Java的设计原则是,代码只要是在编译时没有出现警告,
* 就不会遇到运行时ClassCastException
* 1处引发ArrayStoreException
*/
public static void ArrClassCastTest(){
Integer[] ia = new Integer[6];
Number[] na = ia;
na[0] = 0.5;//1
List<Integer> iList = new ArrayList<Integer>();
List<Number> nlist = iList;//出现变异错误
}
}