如何实现相当于特征的Rust的Java?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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!