黑马程序员技术交流社区

标题: 华为笔试题 [打印本页]

作者: kebi    时间: 2015-11-10 21:02
标题: 华为笔试题
本帖最后由 kebi 于 2015-11-10 21:14 编辑
  1. 1. class A {

  2. 2.            protected int method1(int a, int b) { return 0; }

  3. 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; }

  1. publicclass B extends A{
  2.     /**
  3.     //can not reduce the visibility of the inherited method from A
  4.     //即不能够使从类A中继续来的方法的可见性降低  
  5.     //private int method1(int a, int b) { return 0; }
  6.    
  7. //This static method cannot hide the instance method from A
  8.     //静态方法不能够隐藏继承于A的实例
  9.     //static protected int method1(int a, int b) { return 0; }

  10.     //返回类型与A中的该方法不一致
  11.     //public short method1(int a, int b) { return 0; }

  12.     /**
  13.      *总结:类的继承中,如果要想重载父类的方法,必须要和父类中的返回类型、可见性等等都要操作一致
  14.      *否则,程序就会报错。一定遵守子类要遵从于父类的原则
  15.      */

  16.     //这里是写了一个重载方法,因为参数类型不一致,不会报错,正确
  17.     private int method1(int a, long b) { return 0; }

  18.     //可见性可以增大,但是不能够缩小,正确
  19.     public int method1(int a, int b) { return 0; }

  20.     publicstaticvoid main(String[] args) {
  21.     .......
  22.     }
  23. }
复制代码









欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2