Java:执行子包中任意类的方法
考虑这个包结构
java.assignments
Main.java
java.assignments.lab1
Exe1.java
java.assignments.lab2
Exe1.java
Exe2.java
Exe3.java
java.assignments.lab3
Exe1.java
Exe2.java
Exe3.java
Exe4.java
and so on.
我想以这样的方式编写主类,如果我给出 命令如
中的方法$ java java.assignments.Main exe java.assignments.lab2.Exe2
程序应该执行main 存储在lab2.Exe2.java
如果我给出
$ java java.assignments.Main src java.assignments.lab2.Exe2
程序应该打印源代码 Exe2.java的我希望显示的源代码包含我认为的注释 写在程序中。
如果我给出
中包含的包和类$ java java.assignments.Main list
它应该列出所有java.assignments
如果我给出
中包含的所有类$ java java.assignments.Main list java.assignements.lab3
它应该列出java.assignments.lab3
这些事情可能吗?如果是,我该如何实现这一目标。
到目前为止我发现的东西,
我发现我可以从 本教程, 但我如何实现#2#显示源代码
和列出包#3#
Consider this package structure
java.assignments
Main.java
java.assignments.lab1
Exe1.java
java.assignments.lab2
Exe1.java
Exe2.java
Exe3.java
java.assignments.lab3
Exe1.java
Exe2.java
Exe3.java
Exe4.java
and so on.
I want to write the main class in such a way that if I give the
command like$ java java.assignments.Main exe
The program should execute the main
java.assignments.lab2.Exe2
method stored inlab2.Exe2.java
If I give
$ java java.assignments.Main src
The program should print the source code
java.assignments.lab2.Exe2
of Exe2.javaI would prefer if the source code displayed included comments that I
write in the program.If I give
$ java java.assignments.Main list
It should list all the
packages and the classes contained injava.assignments
If I give
$ java java.assignments.Main list java.assignements.lab3
It should list all the classes contained injava.assignments.lab3
Are these things possible ? and if yes how do I achieve this .
The stuff I have found out till now ,
I found out that I can dynamically load and execute classes #1#
from this tutorial,
but how do I achieve #2# displaying Source code
and listing packages #3#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不建议你开始学习反思;我认为这对于初学者来说太难了,甚至对于一些中级程序员来说也太难了,而且也太投入了。我的意思是,即使您确实理解概念等,反射也是一个大主题,它会让您远离编程中的具体事情。
因此,我建议这样做:
让所有“分配”类实现一个接口,指示它们定义一个您将要执行的方法。例如,
在名为 MainClass.java 的类中定义,然后让每个作业都实现该接口:
即,只需在类声明之后添加“implements MainClass”即可。
如果您还不知道,您将需要处理从命令行传递给 java main 方法的“arguments”字符串数组。这就是“exe”和用户希望您执行的类的名称将显示的位置。
因此,您的“main main”方法将测试第一个参数,看看它是否等于“exe”。如果是,则 1. 将包名称 (java.assignments) 添加到您的类名中,使用 Class.forName() 实例化该类,将其转换为 Main,然后执行 main 方法。该代码看起来像:
Class.forName() 实例化给定类名的对象。您所需要的只是一个完全限定名称的字符串。
上面 Class.forName() 之后的行将新实例化的对象“转换”到 Main 接口。如果您的对象没有实现(或扩展)Main,您的代码将在执行强制转换的行上失败。它告诉编译器允许您使用它执行实现 Main 的类允许的操作。
之后,该对象就像您对其进行了“new”操作的对象一样,您可以调用其上定义的任何方法。
我想说的是,首先让该部分为一些不同的课程运行。如果您仍然对显示源代码感兴趣并想知道将其放在哪里等,那么我们可以在另一节课中讨论这一点。我真的认为,如果你能运行这个,那么一次努力就足以完成任务。
RC
I do not recommend having you start learning reflection; I think it is too difficult for a beginner, and even for some intermediate programmers, and also too involved. I mean, even if you do understand the concepts and so forth, reflection is a big topic and takes you rather far away from doing concrete things in programming.
So I recommend this instead:
Have all your "assignment" classes implement an interface that dictates they define a method which you're then going to execute. For instance, define
in a class called MainClass.java, and then have each of your assignments implement that interface:
I.e., just put "implements MainClass" after the class declaration.
If you do not already know, you are going to need to handle the "arguments" string array passed to a java main method from the command line. That is where the "exe" and the name of the class the user wants you to execute are going to show up.
So your "main main" method is going to test the first argument to see if it is equal to "exe". If it is, then 1. prepend the package name (java.assignments) to your class name, instantiate the class using Class.forName(), cast it to Main, and execute the main method. That code will look something like:
Class.forName() instantiates an object of the given class name. All you need is a string that is the fully-qualified name.
The line after Class.forName() above does a "cast" of the newly instantiated object to the Main interface. If your object doesn't implement (or extend) Main, your code will fail on the line that does the cast. It tells the compiler to allow you to do things with it that are allowed on a class that implements Main.
After that, the object is just like one you did a "new " on, and you can invoke any method defined on it.
I would say get that part running first for some different classes. If you're still interested in displaying source and want to know where to put it, etc., then we can get into that in another lesson. I really think if you get this running, that will be enough to accomplish for one effort.
rc
您无法根据列出包中包含的所有类你使用反射找到包中的所有类吗?
如果你想列出一个类的源代码,我建议将源代码包含在你的 jar 中并以某种方式显示它。
You cannot list all classes contained in a package according to Can you find all classes in a package using reflection?
If you want to list the source code of a class, I suggest including the source in your jar and displaying it somehow that way.