Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JAVA显式实现接口成员?_Java_C# - Fatal编程技术网

如何使用JAVA显式实现接口成员?

如何使用JAVA显式实现接口成员?,java,c#,Java,C#,我有一个场景,我们使用C#中的显式实现来实现接口成员。目前我需要在JAVA中实现相同的行为,但无法显式调用接口成员,因此将实现的成员标记为public会显示错误。我是JAVA新手,请告诉我如何在JAVA中实现相同的行为 C#代码: public class WF_33470: IData { private string m_data = string.Empty; string IData.Element { get {

我有一个场景,我们使用C#中的显式实现来实现接口成员。目前我需要在JAVA中实现相同的行为,但无法显式调用接口成员,因此将实现的成员标记为public会显示错误。我是JAVA新手,请告诉我如何在JAVA中实现相同的行为

C#代码:

public class WF_33470: IData
{
    private string m_data = string.Empty;
    string IData.Element
    {
        get
        {
            return m_data;
        }
    }
    string IData.Data()
    {
        get
        {
            return "Hello World";
        }
    {
}

    internal interface IData
    {
        string Element
        {
            get;
        }
        string Data();
    }
public class WF_33470 implements  IData
{
    private  String m_data = "";
    String getElement()throws Exception{//throws error to change the modifier to be public, but i need to achieve explicit implementation here
        return m_data;
    }
    String  Data()throws Exception{//throws error to change the modifier to be public, but i need to achieve explicit implementation here
        return "Hello World";
    }
}

interface IData
{
    String getElement()throws Exception;
    String Data()throws Exception;
}
Java代码:

public class WF_33470: IData
{
    private string m_data = string.Empty;
    string IData.Element
    {
        get
        {
            return m_data;
        }
    }
    string IData.Data()
    {
        get
        {
            return "Hello World";
        }
    {
}

    internal interface IData
    {
        string Element
        {
            get;
        }
        string Data();
    }
public class WF_33470 implements  IData
{
    private  String m_data = "";
    String getElement()throws Exception{//throws error to change the modifier to be public, but i need to achieve explicit implementation here
        return m_data;
    }
    String  Data()throws Exception{//throws error to change the modifier to be public, but i need to achieve explicit implementation here
        return "Hello World";
    }
}

interface IData
{
    String getElement()throws Exception;
    String Data()throws Exception;
}

接口只是为了向其他类公开公共功能。您使用接口允许代码外部的人员与代码交互。为此,需要定义公共方法


如果要强制某人重写给定的一组私有方法,则可能需要使用一系列受抽象保护的方法声明一个抽象类。

您无法执行当前正在执行的操作,从而降低了方法的可见性:

给定

访问修饰符public(§6.6)适用于各种接口声明

根据中定义的规则

重写或隐藏方法的访问修饰符(§6.6)必须提供至少与重写或隐藏方法相同的访问,如下所示:

如果重写或隐藏的方法是public,那么重写或隐藏的方法必须是public;否则,将发生编译时错误。[……]

正如您所见,它定义了
接口
中定义的每个方法都是隐式
公共
,即使没有提供访问修饰符。根据后面JLS部分中的规则,不可能降低给定方法的可见性,因此在类
WF_33470
中定义方法的显式实现而不使用
public
修饰符的结果将是编译时错误,因为您试图降低方法的可见性

您需要将它们定义为

public String getElement()throws Exception{
    return m_data;
}
public String  Data()throws Exception{
    return "Hello World";
}
如果您希望具有较低的可见性,您可能希望使用
abstract
类,如下所示:

public class WF_33470 extends IData {
    private String m_data = "";

    String getElement() throws Exception {
        return m_data;
    }

    String Data() throws Exception {
        return "Hello World";
    }
}

abstract class IData {
    abstract String getElement() throws Exception;

    abstract String Data() throws Exception;
}

在java
中,接口
方法被自动定义为
公共
,由于继承时不能降低可见性,因此在实现接口时,您需要将它们定义为公共(
公共字符串数据()抛出异常{…}
),那么
显式实现
是什么意思@KevinEsche是对的,接口总是公共的()。是否有可能降低继承成员的可见性?@user2810266,使用
抽象类
怎么样?虽然它应该是一个公共问题,但它会妨碍您在实现类上执行
扩展。。设计决策。。