Java的接口和C#一样,是interface关键字,但可以包含的成员不同
C# 的接口只能包含方法签名,Java的接口确可以包含属性(必须是常量),还可以在接口中定义内部接口
1.package test;
2.public interface A {
3. public void getA()throws AException;
4. public static interface B{
5. public void getB();
6. }
7. public class AException extends java.lang.Exception{
8. public AException(String msg){
9. super(msg);
10. }
11. }
12.}
Java中类继承或者叫实现一个接口关键字为implements 而C# 的继承始终都是:
1.package test;
2.public class ImplA implements A {
3. @Override
4. public void getA() throws A.AException {
5. System.out.println("Implements A interface");
6. A.AException e = new A.AException("This is AException");
7. throw e;
8. }
9. public static class ImplB implements A.B {
10. @Override
11. public void getB() {
12. System.out.println("Implements B interface");
13. }
14. }
15. public static void main(String[] arg) {
16. ImplA testA = new ImplA();
17. try {
18. testA.getA();
19. } catch (A.AException e) {
20. e.printStackTrace();
21. }
22. A.B testB = new ImplA.ImplB();
23. testB.getB();
24. }
25.}
注意下面这个写法
public static class ImplB implements A.B
内部类实现接口的内部接口,感觉是不是挺怪的?反正C#没有这样的东西,注意到以上内部接口,内部类,都是静态的