是C#"明确实施"存在于Java的接口? [英] Is the C# "explicit implementation" of the interface present in Java?

查看:90
本文介绍了是C#"明确实施"存在于Java的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,如果你有同样的方法两个基接口(比如,F()),可以使用明确的执行来执行不同的implement执行。与F()。这alloes你区别对待的对象,对应目前来看:作为IMyInterface1或IMyInterface2。这是可能在Java中?

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 treat the object, corresponding to the current point of view: as IMyInterface1 or IMyInterface2. Is this possible in Java?

推荐答案

没有,没有什么像Java的C#显式接口实现。

No, there's nothing like C#'s explicit interface implementation in Java.

在加方,Java有协变返回类型,所以如果你想提供比接口指定一个更强类型的实现,那也没关系。例如,这是好的:

On the plus side, Java has covariant return types, so if you want to provide a more strongly typed implementation than the interface specifies, that's okay. For instance, this is fine:

interface Foo
{
    Object getBar();
}

public class Test implements Foo
{
    @Override
    public String getBar()
    {
        return "hi";
    }
}



C#不允许的 - 和一围绕它的方式通常是显式地实现接口,然后有一个更具体的公共方法(通常由接口实现调用)。

C# wouldn't allow that - and one of the ways around it is typically to implement the interface explicitly and then have a more specific public method (usually called by the interface implementation).

这篇关于是C#"明确实施"存在于Java的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆