Java中overriding的规则(二)

2014-11-24 02:31:34 · 作者: · 浏览: 1
tB test3(int a, int b) { System.out.println("sub"); return null; } @Override protected TestB test4(int a, int b) { System.out.println("sub"); return null; } @Override public TestB test5(int a, int b) { System.out.println("sub"); return null; } // fail // @Override // protected TestA test6(int a, TestB b) { // System.out.println("sub"); // return null; // } @Override protected TestA test6(int a, TestA b) { System.out.println("sub"); return null; } @Override protected TestA test6(int a, int b) { System.out.println("sub"); return null; } // fail: This instance method cannot override the static method // protected TestA test7(int a, int b) { // System.out.println("sub"); // return null; // } // fail: can not throw checked exception // @Override // protected TestA test8(int a, int b) throws Exception { // System.out.println("sub"); // return null; // } @Override protected TestA test8(int a, int b) throws NullPointerException { System.out.println("sub"); return null; } // fail:Exception Throwable is not compatible with throws // clause in TestA.test8(int, int) // @Override // protected TestA test8(int a, int b) throws Throwable { // System.out.println("sub"); // return null; // } // OK // @Override // protected TestA test9(int a, int b) throws Exception{ // System.out.println("sub"); // return null; // } // OK // @Override // protected TestA test9(int a, int b) throws Error{ // System.out.println("sub"); // return null; // } @Override protected TestA test9(int a, int b) throws NullPointerException{ System.out.println("sub"); return null; } // fail // @Override // protected TestA test9(int a, int b) throws Throwable{ // System.out.println("sub"); // return null; // } @Override protected TestA test10(int a, int b) throws NullPointerException{ System.out.println("sub"); return null; } // fail // @Override // protected TestA test10(int a, int b) throws Exception{ // System.out.println("sub"); // return null; // } protected static TestA test11(int a, int b) { System.out.println("supper"); return null; } }

http://www.tutorialspoint.com/java/java_overriding.htm

http://docs.oracle.com/javase/tutorial/java/IandI/override.html