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

相关文章:

  • 2021-07-29
  • 2022-12-23
  • 2021-11-30
  • 2021-05-19
  • 2021-10-18
  • 2022-12-23
  • 2022-02-08
  • 2022-12-23
猜你喜欢
  • 2021-10-26
  • 2022-01-01
  • 2021-08-08
  • 2021-06-26
  • 2022-01-15
相关资源
相似解决方案