File tree Expand file tree Collapse file tree 1 file changed +17
-13
lines changed
Expand file tree Collapse file tree 1 file changed +17
-13
lines changed Original file line number Diff line number Diff line change @@ -674,22 +674,26 @@ SuperExtendExample.func()
674674- 子类方法的返回类型必须是父类方法返回类型或为其子类型。
675675- 子类方法抛出的异常类型必须是父类抛出异常类型或为其子类型。
676676
677- 使用 @Override 注解,可以让编译器帮忙检查是否满足上面的三个限制条件。例如:
677+ 使用 @Override 注解,可以让编译器帮忙检查是否满足上面的三个限制条件。
678+
679+ 下面的示例中,SubClass 为 SuperClass 的子类,SubClass 重写了 SuperClass 的 func() 方法。其中:
680+
681+ - 子类方法访问权限为 public,大于父类的 protected。
682+ - 子类的返回类型为 ArrayList<Integer >,是父类返回类型 List<Integer > 的子类。
683+ - 子类抛出的异常类型为 Exception,是父类抛出异常 Throwable 的子类。
684+ - 子类重写方法使用 @Override 注解,从而让编译器自动检查是否满足限制条件。
685+
678686``` java
679- public class test {
680- class father {
681- protected List<Integer > say () throws Throwable {
682- System . out. println(" father" );
683- return new ArrayList<> ();
684- }
687+ class SuperClass {
688+ protected List<Integer > func () throws Throwable {
689+ return new ArrayList<> ();
685690 }
691+ }
686692
687- class son extends father{
688- @Override
689- public ArrayList<Integer > say () throws Exception {
690- System . out. println(" son" );
691- return new ArrayList<> ();
692- }
693+ class SubClass extends SuperClass {
694+ @Override
695+ public ArrayList<Integer > func () throws Exception {
696+ return new ArrayList<> ();
693697 }
694698}
695699```
You can’t perform that action at this time.
0 commit comments