如何实现相当于特征的Rust的Java?

发布于 2025-01-19 09:18:22 字数 457 浏览 3 评论 0原文

Rust 有一个方便的方法来促进不同类型之间的转换:特征 来自(和Into)定义了一个“接口”来将一个元素转换为另一个元素。

pub trait From<T> {
    fn from(T) -> Self;
}

是否可以使用 Java 函数式接口实现与 From 特征等效的功能?

Rust has a convenient method to facilitate conversion between different types: the trait From (and Into) which defines an "interface" to convert an element to an other.

pub trait From<T> {
    fn from(T) -> Self;
}

Is it possible to implement an equivalent of the From trait with Java functional interfaces?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑忘罢 2025-01-26 09:18:23

Java 没有 Rust 扩展特征的等效项。但是,可以使用 Java 函数接口实现 From 特征的等效项。您可以使用带有单个抽象方法(SAM)的函数式接口来替代 Rust 的 From Trait2

我希望这会有所帮助!

Java does not have an equivalent for Rust’s extension traits. However, it is possible to implement an equivalent of the From trait with Java functional interfaces. You can use a functional interface with a single abstract method (SAM) as a replacement for Rust’s From trait2

I hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文