动态代理出错问题
这是我的测试代码:
package com.itheima.Test; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import com.itheima.car.Car; public class Test { public static void main(String[] args) { Car car = (Car) Proxy.newProxyInstance(Test.class.getClassLoader(), Car.class.getInterfaces(),new InvocationHandler() { //我把Car对象写过来了,本来要写对象实现的接口的 @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // TODO Auto-generated method stub method.invoke(new Car(), args); return null; } } ); car.start(); car.run(); car.stop(); } }
就是在运行的时候报错了:
Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to com.itheima.car.Car
at com.itheima.Test.Test.main(Test.java:12)
想来想去还是没能解决这个问题:
就把老师的源码导入自己的电脑上看了一下发现:
好了问题也就是解决了:
你不会的东西,觉得难的东西,一定不要躲。先搞明白,后精湛,你就比别人优秀了。因为大部分人都不舍得花力气去钻研,自动淘汰,所以你执着的努力,就占了大便宜。奋斗就是每一天都很难,可一年比一年容易。不奋斗就是每一天都很容易,可一年比一年越难。怕吃苦的人吃苦一辈子,不怕吃苦的人吃苦一阵子。拼一个春夏秋冬,赢一个无悔人生