生成java类并动态调用其方法 [英] Generate java class and call its method dynamically

查看:47
本文介绍了生成java类并动态调用其方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package reflection;

import java.io.*;

import java.lang.reflect.*;

class class0
{
  public void writeout0()
  {
    System.out.println("class0");
  }
}

class class1
{
  public void writeout1()
  { 
    System.out.println("class1");
  }
}

class class2
{
  public void writeout2()
  {
    System.out.println("class2");
  }
}

 class class3

{

  public void run()

  {

    try

    {    

        BufferedReader reader= new BufferedReader(new InputStreamReader

(System.in)); 

          String line=reader.readLine(); 

          Class cls=Class.forName(line);
                 //define method here



    }

    catch(Exception ee)

    {

   System.out.println("here "+ee);

    }


  }
  public void writeout3()
  {
      System.out.println("class3");
  }
}

class class4
{
  public void writeout4()
  {
    System.out.println("class4");
  }
}

class class5
{
  public void writeout5()
  {
    System.out.println("class5");
  }
}

class class6
{
  public void writeout6()
  {
    System.out.println("class6");
  }
}

class class7
{
  public void writeout7()
  {
    System.out.println("class7");
  }
}

class class8
{
  public void writeout8()
  {
    System.out.println("class8");
  }
}

class class9
{
  public void writeout9()
  {
    System.out.println("class9");
  }
}



class testclass {

    public static void main(String[] args) {

       System.out.println("Write class name : ");

        class3 example=new class3();

        example.run();
    }

}

问题是;第三个类将从控制台读取类的名称作为字符串.读取类名后,会自动动态生成该类并调用其writeout方法.如果该类没有从输入中读取,则不会被初始化.

Question is ; third class will read the name of the class as String from console. Upon reading the name of the class, it will automatically and dynamically generate that class and call its writeout method.If that class is not read from input, it will not be initialized.

但我不能再继续了;我需要为 3.class 做更多的事情,我该怎么办?

but I can't continue any more ; I need to more something for 3.class, what can I do?

推荐答案

到目前为止,您已经有了一个 Class 对象.您需要实例化类以拥有一个对象来调用方法.获得所需的实例后,您可以通过调用 Class 对象上的 getMethod(String name, Class... parameterTypes) 找到所需的方法.如果所有类的writeout()"方法都具有相同的方法名称,这会很有帮助,否则您必须弄清楚该方法在类中的名称.这是调用writeout()"时缺少的代码:

Up to this point you have a Class object. You need to instantiate the class to have an object to call a method on. Once you have the instance you need, you can find the method you want with a call to getMethod(String name, Class... parameterTypes) on the Class object. It's helpful if all of the classes have the same method name for the "writeout()" method, otherwise you have to figure out what the method is named inside of the class. Here is the code you're missing to make the call to "writeout()":

  Method m = cls.getMethod("writeout", null);
  Object writerInstance = cls.newInstance();

  m.invoke(writerInstance, null);

使用示例中的类和方法命名方案,您可以通过解析类名称并提取数字来找出方法名称.不过,如果所有类都共享方法名称,那就容易多了.

With the class and method naming scheme you have in your example, you could figure out the method name by parsing the class name and extracting the number. It's much easier if all the classes share the method name, though.

这篇关于生成java类并动态调用其方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆