interface DeclareStuff { public static final int EASY = 3; void doStuff(int t);// public rabstract } public class TestDeclare implements DeclareStuff { public static void main(String[] args) { int x = 5; new TestDeclare().doStuff(++x); } void doStuff(int s) { s += EASY + ++s; System.out.println("s=" + s); } }
关于此段代码编译会出错的问题
首先,接口中所有方法默认都是public,至于为什么要是public,原因在于如果不是public,那么只能在同个包下被实现,可访问权限就降低很多了,那么在实现类中,实现的类相当于子类,子类的访问权限是不能比父类小的。
而在java中一个类如果没有权限的修饰符,默认是friendly/default (同一个包内的其它类才可访问),所以在实现类中一定要写public