????????
????Java?????????????????????????????????????Spring?? Mybatis????????????????????????????????????????????????????????????????????????????????????????????????????????????????Java????????????????????????????????????????????????????Java?????????
???????????
????p.s: ???????????????????API???????????????????н??????????????????1???????Quick Start??
????????÷?????????????????????????????λ??????????????????Class????Java??????????????????????????????????
???????getClass????
??????Java?У?????Object???????getClass?????????getClass???????????????????????????????
????String s = "ziwenxie";
????Class<?> c = s.getClass();
???????forName????
????????????????Class?????????forName??
????Class<?> c = Class.forName("java.lang.String");
???????.class
???????????????????????.class??
????Class<?> c = String.class;
??????????????
?????????????????????????????????????????????????????????????????????????????????????????忴????
??????????????typeinfo.interfacea???????????????A??
????package typeinfo.interfacea;
????public interface A { void f(); }
??????????????typeinfo.packageaccess???????????????C?????C???????A?????????????????????????????????????????漸???????????????????
package typeinfo.packageaccess;
import typeinfo.interfacea.A;
class C implements A {
public void f() { System.out.println("public C.f()"); }
public void g() { System.out.println("public C.g()"); }
protected void v () { System.out.println("protected C.v()"); }
void u() { System.out.println("package C.u()"); }
private void w() { System.out.println("private C.w()"); }
}
public class HiddenC {
public static A makeA() { return new C(); }
}
??????callHiddenMethod()???????????????????μ?API??????getDeclaredMethod()???????????????Class?????????????????????????????????invoke()??????????????????????????????????
package typeinfo;
import typeinfo.interfacea.A;
import typeinfo.packageaccess.HiddenC;
import java.lang.reflect.Method;
public class HiddenImplementation {
public static void main(String[] args) throws Exception {
A a = HiddenC.makeA();
a.f();
System.out.println(a.getClass().getName());
// Oops! Reflection still allows us to call g():
callHiddenMethod(a?? "g");
// And even methods that are less accessible!
callHiddenMethod(a?? "u");
callHiddenMethod(a?? "v");
callHiddenMethod(a?? "w");
}
static void callHiddenMethod(Object a?? String methodName) throws Exception {
Method g = a.getClass().getDeclaredMethod(methodName);
g.setAccessible(true);
g.invoke(a);
}
}
????????????????????????????????public??default??protect????pricate??????????????????????????????á????????????????????????????????????????????????????????????
????public C.f()
????typeinfo.packageaccess.C
????public C.g()
????package C.u()
????protected C.v()
????private C.w()
??????????
??????????????????????????????????????????????List<Class<? extends Pet>>???????????????????????????????Pet?ж??????????Java?????????????????List<? extends Pet>?????????????е?????????????????????????????????????JVM???????е???????Pet??????????????Pet??????????Cat????Dog???????????????????????????????????????p.s: ??????????????????????????????????????????????????????????
???????????????????????????????????弸????
public class Pet extends Individual {
public Pet(String name) { super(name); }
public Pet() { super(); }
}
public class Cat extends Pet {
public Cat(String name) { super(name); }
public Cat() { super(); }
}
public class Dog extends Pet {
public Dog(String name) { super(name); }
public Dog() { super(); }
}
public class EgyptianMau extends Cat {
public EgyptianMau(String name) { super(name); }
public EgyptianMau() { super(); }
}
public class Mutt extends Dog {
public Mutt(String name) { super(name); }
public Mutt() { super(); }
}
?????????Pet??????Individual??Individual??????????????????????????Comparable??????????????????????????????????????????й??????????????????????????????????????????й????
public class Individual implements Comparable<Individual> {
private static long counter = 0;
private final long id = counter++;
private String name; // name is optional
public Individual(String name) { this.name = name; }
public Individual() {}
public String toString() {
return getClass().getSimpleName() + (name == null ? "" : " " + name);
}
public long id() { return id; }
public boolean equals(Object o) {
return o instanceof Individual && id == ((Individual)o).id;
}
public int hashCode() {
int result = 17;
if (name != null) {
result = 37 * result + name.hashCode();
}
result = 37 * result + (int) id;
return result;
}
public int compareTo(Individual arg) {
// Compare by class name first:
String first = getClass().getSimpleName();
String argFirst = arg.getClass().getSimpleName();
int firstCompare = first.compareTo(argFirst);
if (firstCompare != 0) {
return firstCompare;
}
if (name != null && arg.name != null) {
int secendCompare = name.compareTo(arg.name);
if (secendCompare != 0) {
return secendCompare;
}
}
return (arg.id < id ? -1 : (arg.id == id ? 0 : 1));
}
}