class A{
public void tell(){
System.out.println("111");
}
}
class B extends{
public void tell(){
System.out.println("222");
}
}
public class Operation{
public static void main(String args[]){
A a=new B();
a.tell();
}
}
最后输出的是222
代理模式用到这个原理,远程调用RMI也用到代理模式。反射就是动态加载,我new了一个对象,可是我怎么知道这个对象可以调用哪些属性,方法呢,那就用到了反射。当一个实体的对象在另外一个类里面新建后,准备调用时,我就动态加载这个实体里面的方法。程序编译时生成一个CLASS,CLASS就这个反射有关。Spring的控制翻转用到反射,mybaties也用到。下面是Object类
package java.lang;
public class Object {
public Object() {
}
private static native void registerNatives();
public final native Class<?> getClass();
public native int hashCode();
public boolean equals(Object var1) {
return this == var1;
}
protected native Object clone() throws CloneNotSupportedException;
public String toString() {
return this.getClass().getName() + "@" + Integer.toHexString(this.hashCode());
}
public final native void notify();
public final native void notifyAll();
public final native void wait(long var1) throws InterruptedException;
public final void wait(long var1, int var3) throws InterruptedException {
if(var1 < 0L) {
throw new IllegalArgumentException("timeout value is negative");
} else if(var3 >= 0 && var3 <= 999999) {
if(var3 > 0) {
++var1;
}
this.wait(var1);
} else {
throw new IllegalArgumentException("nanosecond timeout value out of range");
}
}
public final void wait() throws InterruptedException {
this.wait(0L);
}
protected void finalize() throws Throwable {
}
static {
registerNatives();
}
}