首页 > 技术文章 > Java package can not import alias

brookin 2019-02-22 15:53 原文

you can not do "import x as y;" in Java.

What you CAN do is to extend the class, or write a wrapper class for it, and import that one instead.

import com.backend.mypackage.a.b.c.UserDto;

public class ImportAlias {
    static class UserDtoAlias extends com.backend.mypackage.a.b.c.d.UserDto {
    }

    public static void main(String[] args) {
        UserDto userBackend = new UserDto();
        UserDtoAlias userService = new UserDtoAlias();

        mapper(userBackend, userService);
    }

    private static void mapper(UserDto userBackend, UserDtoAlias userService) {
        // ...
    }
}

 

推荐阅读