• 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
  • 相关阅读:
    Qt Creator 安装SDK,在MSVC编译模式下使用CDB调试器
    QString与std::string的相互转换
    白话代码中的复杂度分析-大O复杂度表示法 时间,空间复杂度分析 最好,最坏,平均复杂度
    QT 如何使窗体初始最大化
    make_ext4fs
    Qt5.4中遇到找不到头文件<QApplication>等
    MariaDB 数据库的备份
    MariaDB -- 数据类型
    MariaDB基础操作
    keepalived + lvs 网站高可用集群
  • 原文地址:https://www.cnblogs.com/yytlmm/p/4744597.html
Copyright © 2020-2023  润新知