在 clojure 中别名 java 包名称

编程入门 行业动态 更新时间:2024-10-18 12:35:00
本文介绍了在 clojure 中别名 java 包名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个 java 包 x.y.z,我可以将 x.y.z 别名为更短的名称,以便我可以将包内的 java 类称为 my-alias.MyJavaClass.

Given a java package x.y.z, can I alias x.y.z to a shorter name, so that i can then refer to java classes inside the package as my-alias.MyJavaClass.

如果这不可能,我可以将所有类import 导入我的命名空间,但我不想手动指定每个类的名称,并且 clojure API 文档没有似乎很清楚是否可以自动import一个包中的所有类.

If that isn't possible, I could just import all classes into my namespace, but I don't want to specify the names of each class manually, and the clojure API docs doesn't seem clear on whether it's possible to import all classes in a package automatically.

推荐答案

没有任何功能(目前).Java 包位于与 Clojure 不同的命名空间中,因此通常的别名技巧将不起作用.

There isn't any functionality for this (yet). Java packages are in a different namespace than Clojure's, so the usual alias tricks won't work.

你可以做的是导入每个类,这样你就可以避免使用完整的包+类名:

What you can do is import each class, which lets you avoid the full package+class name:

(import [java.io File Writer Reader])
(new File "/")
; #<File />

Rich 提供了他不支持 (import [java.io.*]) 的理由 这里.

Rich provides his reasons for not supporting (import [java.io.*]) here.

这篇关于在 clojure 中别名 java 包名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-27 08:08:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/686659.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:别名   名称   clojure   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!