简体   繁体   English

转到具有动态名称的类型(Java)

[英]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. 但是,即使您做的只是想说的话,即使这不是正确的方法,也仍然可以实现。

  • you have to call the java compiler to transform your java file into class file 您必须调用Java编译器将Java文件转换为类文件
  • and then you can use reflection to load dynamically a class (using Class.forName ) 然后您可以使用反射来动态加载类(使用Class.forName
  • and finally instanciate this class to get a new object whose methods can be used, which would be much easier if you had a common interface for all those classes. 最后实例化该类以获取一个可以使用其方法的新对象,如果您为所有这些类具有一个公共接口,则将容易得多。

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 : 这是您应该做的:

  • save your data in a format that has nothing to do with java. 将数据保存为与Java无关的格式。 For a simple format in which fields have values, you could use a property file ( tutorial here ). 对于字段具有值的简单格式,可以使用属性文件( 此处是教程 )。 This will be your long term storage solution. 这将是您的长期存储解决方案。
  • Then try to wrap together all functions related to reading, manipulating and writing those properties in a separate class, with public methods that your other classes will be able to call to have access to this data. 然后,尝试将与读取,操作和写入这些属性有关的所有函数包装在一个单独的类中,并使用其他类将能够调用以访问此数据的公共方法。

使用Class.forName(name).newInstance()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM