“using” directive in Java

前端 未结 4 933
醉话见心
醉话见心 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:37

    You can't create an alias, but you can import packages (JLS 7.5 Import Declarations) so that you don't have to fully qualify class names in that package.

    import java.util.*;
    import java.lang.reflect.Field;
    
    ....
    
    Set s = ... // Set is in java.util
    

    You can also do a static import (guide), though this practice should be limited.

    import static java.util.Arrays.asList;
    
    ...
    
    System.out.println(asList(1, 2, 3));
    

提交回复
热议问题