Java包导入别名
发布时间:2020-12-15 02:47:31 所属栏目:Java 来源:网络整理
导读:参见英文答案 Change Name of Import in Java,or import two classes with the same name5个 在Java中是否可以导入包并为此包导入特定名称? 我目前有一个类,它使用来自后端和服务包的一些DTO.在这两个包中,DTO都有相同的名称.我认为这不太可读: com.backen
参见英文答案 >
Change Name of Import in Java,or import two classes with the same name5个
在Java中是否可以导入包并为此包导入特定名称? 我目前有一个类,它使用来自后端和服务包的一些DTO.在这两个包中,DTO都有相同的名称.我认为这不太可读: com.backend.mypackage.a.b.c.d.UserDto userBackend = new com.backend.mypackage.a.b.c.d.UserDto(); com.service.mypackage.a.b.c.d.UserDto userService = new com.service.mypackage.a.b.c.d.UserDto(); mapper(userBackend,userService); 这是一个小例子.该类实际上非常复杂,并且其中包含更多代码. Java是否有类似导入com.backend.mypackage.a.b.c.d.UserDto作为userDtoBackend的东西,所以我可以缩短我的源代码? 解决方法
不,你不能做“导入x为y;”在Java中.
你可以做的是扩展类,或为它编写一个包装类,然后导入它. 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) { // ... } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |