1、主类 hello.java
package gensoku;
import java.lang.reflect.*;
import java.lang.reflect.Constructor;
public class hello
{
public static void main(String[] args)
{
//多态
reimu x = new reimu();
human y = new reimu();
human z = new human();
//x、y对比:不管父类还是子类引用,子类方法将覆盖父类方法
//y、z对比:动态绑定,依赖隐式参数的实际类型
x.say();
y.say();
z.say();
//函数覆盖需要相同的参数
x.sayOld();
x.sayOld(5);
//父类引用不能调用子类特有的函数
y.sayOld();
//报错:y.sayOld(4);
try
{
//反射机制,需要处理异常
//类型判断
Class xc = x.getClass();
Class yc = y.getClass(); // reimu.class
Class zc = z.getClass();
System.out.println(xc == yc);
System.out.println(xc == zc);
//类信息读取
showClass(xc, x);
//函数指针调用
Method fpt1 = xc.getMethod("show2X", int.class);
Method fpt2 = xc.getMethod("showXX", int.class);
goFunc(4, fpt1);
goFunc(4, fpt2);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//函数指针示例函数
static private void goFunc(int x, Method f)
{ try
{
f.invoke(null, x);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//利用反射的类展示函数
static private void showClass(Class cl, Object obj)
{
try
{
Class superCl = cl.getSuperclass();
//从父到子递归展示类信息
if(superCl != null)
{
showClass(superCl, obj);
}
_showClass(cl, obj);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//利用反射机制打印类信息
static private void _showClass(Class cl, Object obj)
{
try
{
System.out.println(">> " + cl.getName());
//读取构造器
Constructor[] conList = cl.getDeclaredConstructors();
for(Constructor con : conList)
{
System.out.println(" FUNC :" + Modifier.toString(con.getModifiers()) + " " + con.getName());
}
//读取函数
Method[] medList = cl.getDeclaredMethods();
for(Method med : medList)
{
System.out.println(" FUNC :" + Modifier.toString(med.getModifiers()) + " " +
med.getReturnType().getName() + " " + med.getName());
}
//读取变量(无法读取继承得到的变量)
Field[] fieList = cl.getDeclaredFields();
for(Field fie : fieList)
{
System.out.print(" VAL :" + Modifier.toString(fie.getModifiers()) + " " +
fie.getType().getName() + " " + fie.getName() + " : ");
fie.setAccessible(true);
System.out.println(fie.get(obj));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
2、父类 human.java
package gensoku;
public class human
{
private String name = "";
protected String myid = "666";
public human()
{
this.name = "nobody";
}
public human(String aName)
{
this.name = aName;
}
public String retName()
{
return this.name;
}
public void say()
{
System.out.println("father 1");
}
public void sayOld()
{
System.out.println("father 2");
}
}
3、子类 reimu.java
package gensoku;
public class reimu extends human
{
public reimu()
{
//调用父类构造器
super("Reimu");
//不能直接读写父类private域,要用super调用访问器
System.out.println(super.retName() + " : Hello?");
//可以直接读写父类protected域
System.out.println("Key : " + myid);
}
//覆盖父类函数
public void say()
{
System.out.println("son 1");
}
public void sayOld(int x)
{
System.out.println("son 2");
}
static public void show2X(int x)
{
System.out.println("2*x = " + 2 * x);
}
static public void showXX(int x)
{
System.out.println("x*x = " + x * x);
}
}
4、输出结果
Reimu : Hello?
Key : 666
Reimu : Hello?
Key : 666
son 1
son 1
father 1
father 2
son 2
father 2
true
false
>> java.lang.Object
FUNC :public java.lang.Object
FUNC :protected void finalize
FUNC :public final void wait
FUNC :public final void wait
FUNC :public final native void wait
FUNC :public boolean equals
FUNC :public java.lang.String toString
FUNC :public native int hashCode
FUNC :public final native java.lang.Class getClass
FUNC :protected native java.lang.Object clone
FUNC :public final native void notify
FUNC :public final native void notifyAll
FUNC :private static native void registerNatives
>> gensoku.human
FUNC :public gensoku.human
FUNC :public gensoku.human
FUNC :public void say
FUNC :public void sayOld
FUNC :public java.lang.String retName
VAL :private java.lang.String name : Reimu
VAL :protected java.lang.String myid : 666
>> gensoku.reimu
FUNC :public gensoku.reimu
FUNC :public void say
FUNC :public void sayOld
FUNC :public static void show2X
FUNC :public static void showXX
2*x = 8
x*x = 16