English 中文(简体)
动态地点名某类案件?
原标题:Dynamically naming an instance of a class?

只是一个小小问题。 以下是我的方案2类方案拟定任务的一部分。 它侧重于继承和多个阶层。 我已回答了这个问题,但希望将课堂合并为主要职能,实际上建造一些东西,而不是仅仅提交最低课堂。

我的问题是下面的法典部分。 用户管理节目,向用户提供方言,询问他喜欢添加的内容(光盘或电影,两门课)。

What I would like though, is for the user to actually name the specific instance of the class while the program is running. So, the user will click Movie for example, be prompt to enter a name and then next line to use the constructor in the movie class to create a new instance class with the name the user entered? Is this possible? I tried using a simple string x = JOptionpane and then naming the class x also, but it didn t work. I m thinking there might be some method that will refer to the string? like contents.x maybe?

页: 1

import javax.swing.JOptionPane;
public class Main 
{

    public static void main (String args [])
    {
    Object[] options = {"Movie",
                "CD",
                "Cancel"};
            int n = JOptionPane.showOptionDialog(null, "Would you like to add an item?","Product",
                        JOptionPane.YES_NO_CANCEL_OPTION,
                            JOptionPane.QUESTION_MESSAGE,
                                null,
                                    options,
                                        options[2]);

    switch(n)
    {
    case 0: 
    String moviet = JOptionPane.showInputDialog("Title:");
    int movieID = Integer.parseInt(JOptionPane.showInputDialog("ID:"));
    Movie moviett = new Movie(moviet, movieID);


    }


    }

}
最佳回答

如果超出你的班级内容,你可以使用Map<String, Media>>1,以储存经临时划定的扼杀物。

Map<String, Media> instances = new HashMap<>();
// snip...
String moviet = JOptionPane.showInputDialog("Title:");
int movieID = Integer.parseInt(JOptionPane.showInputDialog("ID:"));
Movie moviett = new Movie(moviet, movieID);
instances.put("some user-provided string", moviett);

问题回答

Java没有这样做,事实上,变数名称比你想象的要少得多,编纂成文法几乎不存在。 相反,如何利用Stings建立一个电影场,或许是一个叫做“名称”的强势领域。 如果你想找一个基于“Sting”的班级,则考虑使用“Hasmaap”这样的地图。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签