我有我们用来实现在C#中使用显式实现的接口成员的场景。目前我需要在JAVA中实现相同的行为,但不能显式调用接口成员,因此它显示错误标记实现的成员为public。我是JAVA新手,请让我知道如何在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();
}
Java代码:
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',并且当继承你需要将它们定义为public时,你不能降低可见性,同时实现该接口,并且('public String Data()throws Exception {...}') – SomeJavaGuy
你的意思是“明确实施”吗? @KevinEsche是对的,接口总是公开的([相关QA](http:// stackoverflow。COM /问题/ 9614708 /为什么,应该-WE-声明接口的方法,如公共))。 –
有没有可能降低继承的成员的可见性? – Karthikk