Java 如何通过用户输入创建新的对象名称

Java 如何通过用户输入创建新的对象名称,java,object,Java,Object,我想创建一个新对象,并给它一个用户输入名 示例用户输入“robert”将匹配到: Action robert = new Action(); robert.eat(); 我需要在程序中更改什么,以便创建具有动态名称的新对象? 非常感谢。 我编写下一个代码: import java.util.Scanner; public class Human { /** * @param args */ public static void main(String

我想创建一个新对象,并给它一个用户输入名

示例用户输入“robert”将匹配到:

 Action robert = new Action();
 robert.eat();
我需要在程序中更改什么,以便创建具有动态名称的新对象? 非常感谢。 我编写下一个代码:

import java.util.Scanner;

public class Human {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner user_input = new Scanner( System.in );

        String first_name;
        System.out.print("Enter your first name: ");
        first_name = user_input.next( );//robert

        System.out.println("You are " + first_name);//robert

        Action first_name = new Action();
        Food orange = new Food();
        robert.eat(orange);

    }

}

无法在运行时(动态)指定(创建)变量名。

无法在运行时(动态)指定(创建)变量名。

这是不可能的。如何将类对象的名称声明为变量。

这是不可能的。如何将类对象的名称声明为变量。

与其他语言不同,Java无法动态创建变量名称。变量名在代码中声明。为了实现你想要做的事情,请查看该系列的功能。这将允许您将用户名“映射”到某个值

快速示例:

//1) Setup the mapping, 
//The first parameter to put() is the key (perhaps a user given name?) 
//and the second parameter is actual value you want to map for that key.
//note that although I'm using a String as the key and a String as the value
//You can use pretty much any object as the value. Keys are recommended to be
//immutable objects (a String is a good one)
Map<String,String> mMap = new HashMap<String,String>();
mMap.put("John", "Orange");
mMap.put("Steve","Apple");

//2) Once the map is setup, you can then retrieve values given a key:
System.out.println("John's fruit is: "+mMap.get("John"));
//1)设置映射,
//put()的第一个参数是键(可能是用户给定的名称?)
//第二个参数是要为该键映射的实际值。
//请注意,虽然我使用字符串作为键,使用字符串作为值
//几乎可以使用任何对象作为值。建议使用钥匙
//不可变对象(字符串是好的)
Map mMap=newhashmap();
mMap.put(“约翰”、“橙色”);
mMap.put(“史蒂夫”、“苹果”);
//2) 设置映射后,您可以检索给定键的值:
System.out.println(“约翰的水果是:”+mMap.get(“约翰”));

与其他语言不同,Java没有一种动态创建变量名的方法。变量名在代码中声明。为了实现你想要做的事情,请查看该系列的功能。这将允许您将用户名“映射”到某个值

快速示例:

//1) Setup the mapping, 
//The first parameter to put() is the key (perhaps a user given name?) 
//and the second parameter is actual value you want to map for that key.
//note that although I'm using a String as the key and a String as the value
//You can use pretty much any object as the value. Keys are recommended to be
//immutable objects (a String is a good one)
Map<String,String> mMap = new HashMap<String,String>();
mMap.put("John", "Orange");
mMap.put("Steve","Apple");

//2) Once the map is setup, you can then retrieve values given a key:
System.out.println("John's fruit is: "+mMap.get("John"));
//1)设置映射,
//put()的第一个参数是键(可能是用户给定的名称?)
//第二个参数是要为该键映射的实际值。
//请注意,虽然我使用字符串作为键,使用字符串作为值
//几乎可以使用任何对象作为值。建议使用钥匙
//不可变对象(字符串是好的)
Map mMap=newhashmap();
mMap.put(“约翰”、“橙色”);
mMap.put(“史蒂夫”、“苹果”);
//2) 设置映射后,您可以检索给定键的值:
System.out.println(“约翰的水果是:”+mMap.get(“约翰”));

对象通常没有名称。你说的是一个变量的名称,这是完全不同的。如果你想让你的对象有一个名字,它应该有一个字段来记住它的名字,你可能应该把名字传给构造函数。你错了,你把变量名和用户名混在一起了!如果在运行时没有人会看到,为什么要给变量指定一个动态名称?我有一种感觉,您正在寻找,它允许您使用键(可以是字符串)访问对象。对象通常没有名称。你说的是一个变量的名称,这是完全不同的。如果你想让你的对象有一个名字,它应该有一个字段来记住它的名字,你可能应该把名字传给构造函数。你错了,你把变量名和用户名混在一起了!如果在运行时没有人会看到,为什么要给变量指定一个动态名称?我有一种感觉,你在寻找,它允许你使用一个键(可以是字符串)访问一个对象。这非常有帮助,非常感谢。非常有帮助,非常感谢。