interface myItfc
{
double area(double a, double b);
}class test implements myItfc
{
double area(double a, double b)
{
return 0.1;
}
}--------------------Configuration: JDK version 1.5.0_02 <Default>--------------------
F:\temp\d\B.java:8: area(double,double) in test cannot implement area(double,double) in myItfc; attempting to assign weaker access privileges; was public
double area(double a, double b)
^
1 errorProcess completed.这是怎么回事啊?一定要把area声明为public吗?
还有我试过在interface里声明area为protected,或者private都说不能这么声明。
在接口里面只能声明为
public或者不声明吗?
{
double area(double a, double b);
}class test implements myItfc
{
double area(double a, double b)
{
return 0.1;
}
}--------------------Configuration: JDK version 1.5.0_02 <Default>--------------------
F:\temp\d\B.java:8: area(double,double) in test cannot implement area(double,double) in myItfc; attempting to assign weaker access privileges; was public
double area(double a, double b)
^
1 errorProcess completed.这是怎么回事啊?一定要把area声明为public吗?
还有我试过在interface里声明area为protected,或者private都说不能这么声明。
在接口里面只能声明为
public或者不声明吗?
见《java编程思想》(第三版)249页。
你不可以降低访问权限
在class test中默认为packge或叫default,它的存取权限介于public 和protect之间,比public低;
但java规定子类重写父类的方法时,存取权限只能扩大或不变,不能降低另外,interface中的方法只能声明为public或不声明,因为接口即让外界知道,对外开放的意思,所以不能声明为private