package typeinfo;
class Base {
static { System.out.println("加载Base类"); }
}
class Derived extends Base {
static { System.out.println("加载Derived类");}
}
public class Test {
static void printerInfo(Class c) {
System.out.println("类名: " + c.getName() +
"是否接口? [" + c.isInterface() + "]");
}
public static void main(String[] args) {
Class c = null;
try {
c = Class.forName("typeinfo.Derived");
} catch (ClassNotFoundException e) {
System.out.println("找不到Base类");
System.exit(1);
}
printerInfo(c);
Class up = c.getSuperclass(); // 取得c对象的基类
Object obj = null;
try {
obj = up.newInstance();
} catch (InstantiationException e) {
System.out.println("不能实例化");
System.exit(1);
} catch (IllegalAccessException e) {
System.out.println("不能访问");
System.exit(1);
}
printerInfo(obj.getClass());
} /* 输出:
加载Base类
加载Derived类
类名: typeinfo.Derived是否接口? [false]
类名: typeinfo.Base是否接口? [false]
*/
}
Class intClass = int.class;
Class<Integer> genericIntClass = int.class;
genericIntClass = Integer.class; // 等价
intClass = double.class; // ok
// genericIntClass = double.class; // Illegal!
class Base {}
class Derived extends Base {}
class Base2 {}
public class Test {
public static void main(String[] args) {
Class<? extends Base> cc = Derived.class; // ok
// cc = Base2.class; // Illegal
}
}
class Base {}
class Derived extends Base {}
public class Test {
public static void main(String[] args) {
Derived derived = new Derived();
System.out.println(derived instanceof Base); // 输出true
}
}
// 使用反射展示类的所有方法, 即使方法是在基类中定义的
package typeinfo;
// Print类的print方法等价于System.Out.Println,方便减少代码量
import static xyz.util.Print.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.regex.Pattern;
// {Args: typeinfo.ShowMethods}
public class ShowMethods {
private static String usage =
"usage:\n" +
"ShowMethods qualified.class.name\n" +
"To show all methods in class or:\n" +
"ShowMethods qualified.class.name word\n" +
"To search for methods involving 'word'";
// 去掉类名前面的包名
private static Pattern p = Pattern.compile("\\w+\\.");
public static void main(String[] args) {
if (args.length < 1) {
print(usage);
System.exit(0);
}
int lines = 0;
try {
Class<?> c = Class.forName(args[0]);
// 反射获得对象c所属类的方法
Method[] methods = c.getMethods();
// 反射获得对象c所属类的构造
Constructor[] ctors = c.getConstructors();
if (args.length == 1) {
for (Method method : methods)
print(p.matcher(method.toString()).replaceAll(""));
for (Constructor ctor : ctors)
print(p.matcher(ctor.toString()).replaceAll(""));
}
} catch (ClassNotFoundException e) {
print("No such class: " + e);
}
} /*
public static void main(String[])
public final void wait() throws InterruptedException
public final void wait(long,int) throws InterruptedException
public final native void wait(long) throws InterruptedException
public boolean equals(Object)
public String toString()
public native int hashCode()
public final native Class getClass()
public final native void notify()
public final native void notifyAll()
public ShowMethods()
*/
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
class DynamicProxyHandler implements InvocationHandler {
private Object proxied; // 代理对象
public DynamicProxyHandler(Object proxied) {
// TODO Auto-generated constructor stub
this.proxied = proxied;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
System.out.println("代理类: " + proxy.getClass() + "\n"
+ "代理方法: " + method + "\n"
+ "参数: " + args);
if (args != null)
for (Object arg : args)
System.out.println(" " + arg);
return method.invoke(proxied, args);
}
}
interface Interface { void doSomething(); }
class RealObject implements Interface {
@Override
public void doSomething() {
// TODO Auto-generated method stub
System.out.println("doSomething");
}
}
public class DynamicProxyDemo {
public static void consumer(Interface iface) {
iface.doSomething();
}
public static void main(String[] args) {
RealObject realObject = new RealObject();
// 使用动态代理
Interface proxy = (Interface)Proxy.newProxyInstance(
Interface.class.getClassLoader(),
new Class[] { Interface.class },
new DynamicProxyHandler(realObject));
consumer(proxy);
} /* 输出:
代理类: class $Proxy0
代理方法: public abstract void Interface.doSomething()
参数: null
doSomething
*/
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有