Warning: Undefined array key 0 in /www/wwwroot/w/show/success.php on line 266
Warning: Trying to access array offset on value of type null in /www/wwwroot/w/show/success.php on line 266
Warning: Undefined array key 0 in /www/wwwroot/w/show/success.php on line 266
Warning: Trying to access array offset on value of type null in /www/wwwroot/w/show/success.php on line 266
10.4.3 接口、类和抽象类
一个类可以同时继承类和接口,或者抽象类和接口,抽象类和类不能在一个类的继承列表中同时出现。如果派生类同时继承类或抽象类和接口,一定要把类或者抽象类写在前面,接口写在后面,类名和接口名之间用“,”隔开,接口没有先后顺序。每一种继承都要根据各自的规则去实现。
接口与抽象类的区别如下。
- 抽象类是一个不完全的类,需要通过派生类来完善它。
- 接口只是对类的约束,它仅仅承诺了类能够调用的方法。
- 一个类一次可以实现若干个接口,但一个类只能继承一个基类。
- 抽象类只需派生类实现它的抽象方法,接口要求必须实现它的所有成员。
在实际编程中,接口的使用要比抽象类广泛得多。
Warning: Undefined array key 0 in /www/wwwroot/w/show/success.php on line 276
Warning: Trying to access array offset on value of type null in /www/wwwroot/w/show/success.php on line 276
Warning: Undefined array key 0 in /www/wwwroot/w/show/success.php on line 276
Warning: Trying to access array offset on value of type null in /www/wwwroot/w/show/success.php on line 276