<?php Class Father { public function fmeth1() { echo "fmeth1()...<br>"; } //public function fmeth1($str1) { // echo "fmeth1() with $str1...<br>"; //} } Class Son extends Father { public function fmeth1() { echo "fmeth1() in son...<br>"; } } $s=new Son(); $s->fmeth1(); ?>
class A { protected int method1(int a, int b) { return 0; } }
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; }
310-035中的题目,标准答案是A,C
A是override,access从protected--->public变宽了,因此是正确的。 B,D也是override,B从protected--->private变窄了,D的返回类型变了,所以都错误。 C是overload,access的宽窄和返回类型都无所谓,所以是正确的。 E是override,但是增加了static,因为static method cannot hide the instance method from super class.因此是错误的。 所以选AC。 子类继承父类并且覆盖父类方法的叫override --重写,覆写,覆盖 子类有多个相同方法名称,但是参数不同,叫overload - 重(zhong)载,过载