本帖最后由 kebi 于 2015-11-10 21:14 编辑
- 1. class A {
- 2. protected int method1(int a, int b) { return 0; }
- 3. }
复制代码
Which two are valid in a class that extends class A? (Choose two)
A. public int method1(int a, int b) { return 0; }
B. private int method1(int a, int b) { return 0; }
C. private int method1(int a, long b) { return 0; }
D. public short method1(int a, int b) { return 0; }
E. static protected int method1(int a, int b) { return 0; }
- publicclass B extends A{
- /**
- //can not reduce the visibility of the inherited method from A
- //即不能够使从类A中继续来的方法的可见性降低
- //private int method1(int a, int b) { return 0; }
-
- //This static method cannot hide the instance method from A
- //静态方法不能够隐藏继承于A的实例
- //static protected int method1(int a, int b) { return 0; }
- //返回类型与A中的该方法不一致
- //public short method1(int a, int b) { return 0; }
- /**
- *总结:类的继承中,如果要想重载父类的方法,必须要和父类中的返回类型、可见性等等都要操作一致
- *否则,程序就会报错。一定遵守子类要遵从于父类的原则
- */
- //这里是写了一个重载方法,因为参数类型不一致,不会报错,正确
- private int method1(int a, long b) { return 0; }
- //可见性可以增大,但是不能够缩小,正确
- public int method1(int a, int b) { return 0; }
- publicstaticvoid main(String[] args) {
- .......
- }
- }
复制代码
|
|