抽象类,Number,作为我在Java中的输入

时间:2016-03-05 16:09:55

标签: java numbers instanceof

我试图在实现以这种形式输入实体的方法的上下文中弄清楚如何处理抽象类Number。

下面是我写的一个简短的java脚本,显示了我的困惑。

在main方法中,我无法弄清楚如何使我的输入更通用,以便在调用whynowork时,它可以根据其数据类型打印出一条消息(Double,int ,可比较)

public class PleaseWork{


    public static void main(String[] args) {

  //where i was desperately trying to figure out how to input a number       
        int x= Integer.parseInt(args[0]);
        float a = Float.parseFloat(args[0]);
        whynowork(3);

    }

// this tells you what data type your input is
    public static void whynowork(Number param) {

         if( param instanceof Double) {
            System.out.println("param is a Double");
        }
        else if( param instanceof Integer) {
            System.out.println("param is an Integer");
        }

        if( param instanceof Comparable) {
            System.out.println("param is comparable"); 
        }       

    }

}

1 个答案:

答案 0 :(得分:1)

您可以将其简化为:document.addEventListener('keydown', function(event) { var key_press = String.fromCharCode(event.keyCode); if (//I don't know what should go here) { if (key_press == "W" && player.y >= dist) { player.y -= dist; } else if (key_press == "S" && player.y <= (cH - dist)) { player.y += dist; } else if (key_press == "A" && player.x >= dist) { player.x -= dist; } else if (key_press == "D" && player.x < (cW - dist)) { player.x += dist; } } });