Is the C# “explicit implementation” of the interface present in Java?

前端 未结 4 1703
忘掉有多难
忘掉有多难 2020-12-17 14:23

In C#, if you have two base interfaces with the same method (say, F()) you can use explicit implementation to perform different impl. for F(). This alloes you to differently

4条回答
  •  误落风尘
    2020-12-17 15:02

    You can achieve similar effect using the mechanism of anonymous interface implementation in Java.

    See example:

    interface Foo {
    
        void f();
    }
    
    interface Bar {
    
        void f();
    }
    
    public class Test {
    
        private String foo = "foo", bar = "bar";
    
        Foo getFoo() {
            return new Foo() {
    
                @Override
                public void f() {
                    System.out.println(foo);
                }
            };
        }
    
        Bar getBar() {
            return new Bar() {
    
                @Override
                public void f() {
                    System.out.println(bar);
                }
            };
        }
    
        public static void main(String... args) {
            Test test = new Test();
            test.getFoo().f();
            test.getBar().f();
        }
    }
    

提交回复
热议问题