-
在rust中,我们可以做这样的事情:
trait MyTrait { fn my_func(); } struct MyStruct {} impl MyTrait for MyStruct { fn my_func() { // this gets invoked } } fn my_func<B: MyTrait>() { // This here B::my_func(); } fn main() { my_func::<MyStruct>(); }
-
在java中,我们不能做这样的事情:
interface MyInterface { public static void myFunc() { // This gets invoked } } class MyClass implements MyInterface { public static void myFunc() { } } public class Main { static <T extends MyInterface> void myFunc() { // This here T.myFunc(); } public static void main(String[] args) { Main.<MyClass>myFunc(); } }
现在还有其他问题关于为什么 java 不允许在 java 中覆盖/强制执行静态方法,但是这个 特定示例 是否因为 单态化与类型擦除而不起作用 >?我试图了解更深层次的原因。 我的猜测是因为类型擦除后只有一个实现,并且需要编译静态调用,所以它不起作用。这是真正的原因吗?