interface DeclareStuff{
public static final int EASY =3;
void doStuff(int t);
}
public class test100 implements DeclareStuff{
public static void main(String[] args) {
int x =5;
new test100().doStuff(++x);

}
void doStuff(int s){
s += EASY+ ++s;
}

}编译不成功 为什么 在test100类里的doStuff方法要加public

解决方案 »

  1.   

    ava中interface的方法默认修饰符是public abstract。
    官方文档如示:
    Java Language Specification, 3rd ed. §9.4 Abstract Method Declarations
    http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.4Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.Every method declaration in the body of an interface is implicitly public.所以接口中的doStuff 的修饰符就是public,它的实现类的方法也就只能是public,否则就破坏了接口的实现规则了。
      

  2.   

    这个以前还真不知道,学习了。 不过我也没有试过非public 的 interface 
      

  3.   

    Every method declaration in the body of an interface is implicitly public.每一个都是public!!要吗不加修饰符,要加就必须有public!
      

  4.   

    interface 是提供给其他类实现的。test100 类的修饰符是public,DeclareStuff的修饰符范围必须要比它的要大。