“using” directive in Java

前端 未结 4 902
醉话见心
醉话见心 2021-01-18 06:55

When the type name is too long, in C# i can create alias like this:

using Dict = System.Collections.Generic.Dictionary;
4条回答
  •  执念已碎
    2021-01-18 07:54

    Short answer: nope.

    However, you can (and should) import classes so as to not use their fully qualified name:

    import java.lang.String
    // ....
    String s = "hello, world.";
    

    If you must define an alias since your class is using multi-level generics or whatnot, you can use this hack - by defining a private class which extends the class you're aliasing (generics included) just for the sake of having an easy-to-use handle:

    class MyMap extends HashMap {}
    
    MyMap a = new MyMap();
    a.put("key", "val");
    

    (adding class aliases was requested before as an enhancement to Java, and is still pending)

提交回复
热议问题