zoukankan      html  css  js  c++  java
  • java反射与多态(父类调用子类)的代码演示

    package Test0817;

    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;

    class Base{
    int a;

    Base(){
    a=1;
    System.out.println("Base Construct");
    }
    public void f(){
    System.out.println("Base");
    }
    }

    class Sub extends Base{
    int a;
    int b;
    Sub(){
    a=2;
    b=2;
    System.out.println("Sub Construct");
    }

    Sub(int i){
    a=i;
    b=i;
    System.out.println("Sub Construct.this have one param");
    }
    public void f(){
    System.out.println("Sub");
    }
    public void f(int m){
    System.out.println("Sub,the m = "+m);
    }
    }

    public class TestFanshe {

    public static void main(String[] args) throws InstantiationException, IllegalAccessException, SecurityException {
    Class<?> c;
    try {
    c = Class.forName("Test0817.Sub"); //需要有完整的包名.类名
    Sub s = (Sub) c.newInstance();//实例化
    //得到构造函数
    c.getConstructors();
    //得到方法
    Method method = c.getMethod("f");
    System.out.println("the method is "+method.toString());

    Class[] paramerClass = new Class[1];
    paramerClass[0] = int.class; //这个地方要写int,与形参的参数类型一致,写Integer就会报方法找不到
    Method method1 = c.getMethod("f", paramerClass);
    System.out.println("the method is "+method1.toString());
    //通过反射,调用s对象的方法
    method.invoke(s); //无参数的

    int a=10;
    method1.invoke(s, a); //一个参数的
    method1.invoke(s, 20);

    //实例化,将父类引用指向
    Base b = (Base) c.newInstance();
    Base bs = (Sub) c.newInstance();
    //Sub sb = (Base) c.newInstance(); 报错
    s.f(); //输出 sub
    b.f(); //输出 sub
    //b.f(10);//报错,无法调用父类中没有的子类方法
    bs.f(); //输出 sub
    //bs.f(10);//报错,无法调用父类中没有的子类方法
    } catch (ClassNotFoundException e) {
    System.out.println("发生无该类异常");
    e.printStackTrace();
    }catch(NoSuchMethodException e){
    System.out.println("发生无该方法异常");
    e.printStackTrace();
    }catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    }

    输出结果:

    Base Construct
    Sub Construct
    the method is public void Test0817.Sub.f()
    the method is public void Test0817.Sub.f(int)
    Sub
    Sub,the m = 10
    Sub,the m = 20
    Base Construct
    Sub Construct
    Base Construct
    Sub Construct

    package Test0817;
    
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    class Base{
        int a;
        
        Base(){
            a=1;
            System.out.println("Base Construct");
        }
        public void f(){
            System.out.println("Base");
        }
    }
    
    class Sub extends Base{
        int a;
        int b;
        Sub(){
            a=2;
            b=2;
            System.out.println("Sub Construct");
        }
        
        Sub(int i){
            a=i;
            b=i;
            System.out.println("Sub Construct.this have one param");
        }
        public void f(){
            System.out.println("Sub");
        }
        public void f(int m){
            System.out.println("Sub,the m = "+m);
        }
    }
    
    public class TestFanshe {
    
        public static void main(String[] args) throws InstantiationException, IllegalAccessException, SecurityException {
            Class<?> c;
            try {
                c = Class.forName("Test0817.Sub"); //需要有完整的包名.类名
                Sub s = (Sub) c.newInstance();//实例化 
                //得到构造函数
                c.getConstructors();
                //得到方法
                Method method = c.getMethod("f");
                System.out.println("the method is "+method.toString());
                
                Class[] paramerClass = new Class[1];
                paramerClass[0] = int.class; //这个地方要写int,与形参的参数类型一致,写Integer就会报方法找不到
                Method method1 = c.getMethod("f", paramerClass);
                System.out.println("the method is "+method1.toString());
                //通过反射,调用s对象的方法
                method.invoke(s); //无参数的
                
                int a=10;
                method1.invoke(s, a); //一个参数的
                method1.invoke(s, 20);
                
                //实例化,将父类引用指向
                Base b = (Base) c.newInstance();
                Base bs = (Sub) c.newInstance();
                //Sub sb = (Base) c.newInstance(); 报错
                s.f(); //输出 sub
                b.f(); //输出 sub
                //b.f(10);//报错,无法调用父类中没有的子类方法
                bs.f(); //输出 sub
                //bs.f(10);//报错,无法调用父类中没有的子类方法
            } catch (ClassNotFoundException e) {
                System.out.println("发生无该类异常");
                e.printStackTrace();
            }catch(NoSuchMethodException e){
                System.out.println("发生无该方法异常");
                e.printStackTrace();
            }catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
    
        }
    
    }
    View Code
  • 相关阅读:
    Java实现蓝桥杯七对数字
    Java实现蓝桥杯七对数字
    ubuntu下交叉编译windows c程序
    Linux下开发Windows平台运行的程序
    Fedora 11中用MinGW编译Windows的Qt4程序(在Linux系统下编译Windows的程序)
    C++内存问题大集合(指针问题,以及字符串拷贝问题,确实挺危险的)
    C/C++的编译器|编译环境(非常全面的比较)
    关于 js 中的 call 和 apply使用理解
    看我如何应对业务需求变化,领域模型调整?
    MVC 用扩展方法执行自定义视图,替代 UIHint
  • 原文地址:https://www.cnblogs.com/yytlmm/p/4744597.html
Copyright © 2011-2022 走看看