Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何制作扫描仪。下一步();返回BlueJ中的任何数据类型?_Java_Object_Bluej - Fatal编程技术网

Java 如何制作扫描仪。下一步();返回BlueJ中的任何数据类型?

Java 如何制作扫描仪。下一步();返回BlueJ中的任何数据类型?,java,object,bluej,Java,Object,Bluej,我有一个包含方法的类(“testClass”)。这个类有三个对象。现在我想通过在BlueJ终端中输入命令来调用这些对象的方法。我使用Scanner.next()读取命令。如果输入Object1,则应调用Object1的方法。问题是,Scanner.next()返回String,所以 testClass Object = Scanner.next(); Object.testMethod(); 不起作用。我得到了错误 “不兼容的类型:java.lang.String无法转换为testMethod

我有一个包含方法的类(
“testClass”
)。这个类有三个对象。现在我想通过在BlueJ终端中输入命令来调用这些对象的方法。我使用Scanner.next()读取命令。如果输入
Object1
,则应调用
Object1
的方法。问题是,
Scanner.next()
返回
String
,所以

testClass Object = Scanner.next();
Object.testMethod();
不起作用。我得到了错误

“不兼容的类型:java.lang.String无法转换为testMethod

我也不想做这样的东西:

String command = Scanner.next();
switch(command){
    case "object1": object1.testMethod();

    case "object2": object2.testMethod();

    ...


}
我怎样才能让它工作

如何使Scanner.next()返回BlueJ中的任何数据类型

你不能。你能做的就是写一些东西,把
字符串
转换成命令调用

我也不想做这样的[切换代码]:

听起来您想在这里实现类似于命令模式的东西。
下面是一个(非常)简单的Java示例:

// This is a simple way to have a list of commands to run.
// It also gives you a very easy way to map a String to a command behavior,
// to read console commands or menu headings or something like that.
// You could also have your command invoker catch the exception and display
// a "this action is unsupported" message.

public class CommandPatternExample {
    public static void main(String[] args) throws Exception {
        CommandEnum.valueOf("A").execute(); // run command "A"
        CommandEnum.valueOf("B").execute(); // run command "B"
        CommandEnum.valueOf("C").execute(); // IllegalArgumentException
    }

    interface Command {
        void execute();
    }

    enum CommandEnum implements Command {
        A {
            @Override
            public void execute() {
                System.out.println("Running command A");
            }
        },
        B {
            @Override
            public void execute() {
                System.out.println("Running command B");
            }
        };
    }
}

我不确定你想做什么。多态性可以帮你

abstract class A {
    abstract void testMethod();
}

class ChildA extends A {
    @Override
    void testMethod() {
        // Do something
    }
}

public class Test {
    public static void main(String[] args)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        A obj = (A) Class.forName("ChildA").newInstance();
        obj.testMethod();
    }
}

你也可以在这里
Scanner
在运行时从用户那里获取类名。

至于你的标题问题,除了重新编写Java平台,你真的不能。你不能。你读的一行Java说是字符串(这很有意义)。此外,您还应该使用一些类似Java的类的标准命名约定,这些类以大写开头,而变量则不以大写开头