[英]Go to a Type with a dynamic name (Java)
I am creating a program which should be able to open different perspectives . 我正在创建一个程序,该程序应该能够打开不同的视角 。 Perspectives , in my program, mean different layouts of the controls. 在我的程序中, 透视图表示控件的不同布局。 The user is able to create and pick which perspective they want to use. 用户能够创建并选择他们想要使用的透视图。 There is also a Default Perspective . 还有一个默认透视图 。 The Default Perspective can be chosen. 可以选择默认透视图 。
Because of this I do not know how many perspectives there are, or what the Default Perspective is. 因此,我不知道有多少个透视图或默认透视图是什么。 My program uses engine.perspective
to Read and Write to the text file ( Perspective.TXT
) in which the all the perspectives are stored. 我的程序使用engine.perspective
读取和写入存储所有透视图的文本文件( Perspective.TXT
)。 When the user creates a perspective my program automatically makes a *.JAVA
file with all the perspective data. 当用户创建透视图时,我的程序会自动创建一个包含所有透视图数据的*.JAVA
文件。 This is my code for reading Perspective.TXT
and calling the correct type: 这是我的代码,用于阅读Perspective.TXT
并调用正确的类型:
ArrayList<String> ALLPRSPCTVE = Read.getAllPerspectives();
String PRSPCTVE = Read.getDefaultPerspective();
{
String[] ARR = (String[]) ALLPRSPCTVE.toArray();
for (int i = 0; i < ALLPRSPCTVE.size(); i++) {// Set All Perspectives
if (PRSPCTVE == ARR[i]) {
}
}
}
How do I change the String
returned from the method Read.getAllPerspectives
to something JAVA will recognize as a type and go to it? 如何将从Read.getAllPerspectives
方法返回的String
更改为JAVA会识别为类型的某种东西并转到它?
You have a software architecture problem. 您有软件体系结构问题。
But, if you want to do exactly what you say, you can still make it possible, even though it's not the right way. 但是,即使您做的只是想说的话,即使这不是正确的方法,也仍然可以实现。
Class.forName
) 然后您可以使用反射来动态加载类(使用Class.forName
) But, please don't do that and go on reading. 但是,请不要那样做并继续阅读。
Alan Turing introduced modern computer at the middle of the 20's century. 艾伦·图灵(Alan Turing)在20世纪中叶引入了现代计算机。 One of its main features is that it dissociates data from code. 它的主要功能之一是将数据与代码分离。
Data is the a part of memory containing variables and code is a set of instructions that manipulates those data (and other creating during programs's lifes. In java, program/code is written inside classes). 数据是包含变量的内存的一部分,而代码则是处理这些数据的指令集(以及程序生命周期内的其他创建。在Java中,程序/代码编写在类内部)。
Data can then be divided into two different support : dynamic, fast and volatile memory (modern RAM) and long term storage (hard disk/database). 然后可以将数据分为两种不同的支持:动态,快速和易失性内存(现代RAM)和长期存储(硬盘/数据库)。
Note that code is data. 请注意,代码是数据。 It has to be saved on disk, loaded in memory, but it is executed. 它必须保存在磁盘上,并加载到内存中,但是必须执行。 Simple data is not executed, it's mere data, read and written but not executed. 简单数据不会被执行,仅仅是数据,可以读写,但无法执行。
This has very practical impliactions for all applications, and you application is violating his separation of data and code has you use code (java classes) to memorize data. 这对所有应用程序都有非常实际的暗示,如果您使用代码(java类)来存储数据,则您的应用程序违反了数据和代码的分隔。
Here is what you should do : 这是您应该做的:
使用Class.forName(name).newInstance()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.