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