当前位置: 技术问答>java相关
提问,谢谢大家的回答!
来源: 互联网 发布时间:2015-09-03
本文导语: 各位大侠们,想请教一个问题: 为什么一个CLASS使用一个接口的时候,定义接口的方法时一定要声明PUBLIC呢? 如 interface A{ void m1(); void m2(); } public class myclass implements A{ public void m1(){ System...
各位大侠们,想请教一个问题:
为什么一个CLASS使用一个接口的时候,定义接口的方法时一定要声明PUBLIC呢?
如
interface A{
void m1();
void m2();
}
public class myclass implements A{
public void m1(){ System.out.println("asdsadasd");};
public void m2(){System.out.println("asdsadasd");};
}
如果我不声明M1,M2前的PUBLIC的话,就会有
"myclass.java": Error #: 459 : method m2() in class leileixia.myclass cannot implement method m2() in interface leileixia.A with weaker access privileges, was public at line 6, column 10
"myclass.java": Error #: 459 : method m1() in class leileixia.myclass cannot implement method m1() in interface leileixia.A with weaker access privileges, was public at line 6, column 10
"myclass.java": Error #: 459 : method m1() in class leileixia.myclass cannot implement method m1() in interface leileixia.A with weaker access privileges, was public at line 7, column 11
"myclass.java": Error #: 459 : method m2() in class leileixia.myclass cannot implement method m2() in interface leileixia.A with weaker access privileges, was public at line 8, column 10
,不解的很!特别是不知道weaker access privileges的意思!!!
请各位指教!
为什么一个CLASS使用一个接口的时候,定义接口的方法时一定要声明PUBLIC呢?
如
interface A{
void m1();
void m2();
}
public class myclass implements A{
public void m1(){ System.out.println("asdsadasd");};
public void m2(){System.out.println("asdsadasd");};
}
如果我不声明M1,M2前的PUBLIC的话,就会有
"myclass.java": Error #: 459 : method m2() in class leileixia.myclass cannot implement method m2() in interface leileixia.A with weaker access privileges, was public at line 6, column 10
"myclass.java": Error #: 459 : method m1() in class leileixia.myclass cannot implement method m1() in interface leileixia.A with weaker access privileges, was public at line 6, column 10
"myclass.java": Error #: 459 : method m1() in class leileixia.myclass cannot implement method m1() in interface leileixia.A with weaker access privileges, was public at line 7, column 11
"myclass.java": Error #: 459 : method m2() in class leileixia.myclass cannot implement method m2() in interface leileixia.A with weaker access privileges, was public at line 8, column 10
,不解的很!特别是不知道weaker access privileges的意思!!!
请各位指教!
|
interface 中的method一定而且只能是 public abstract;尽管你不用写明;
interface Foo {
void aMethod();
}
相当于
interface Foo {
public abstract void aMethod();
}
interface Foo {
void aMethod();
}
相当于
interface Foo {
public abstract void aMethod();
}