PHP 抽象类的简单测试与总结

本文探讨了PHP抽象类的概念,通过实例进行了测试,并对相关内容进行了总结。在编程的道路上,作者鼓励大家勇于发现错误,共同进步。
摘要由CSDN通过智能技术生成

<?php
abstract class AbstractClass{
	//	定义必须设定的方法
	abstract protected function getValue();
	abstract protected function prefixValue($prefix);
	
	//	普通方法的定义
	public function printOut(){
		print $this -> getValue().'\n';
	}
	
	public function starice(){
		echo "starice\n";
	}
}
//Strict standards: Non-static method AbstractClass::starice() should not be called statically in D:\wamp64\www\Ces\ClassCes.php on line 17
//	非静态的方法调用,能出现结果,但是会报错
// AbstractClass::starice();

class CLASS1 extends AbstractClass{
	//	继承抽象类的子方法
	public function getValue(){
		return "ClassCes true";
	}
	//	Fatal error: Class CLASS1 contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (AbstractClass::prefixValue) in D:\wamp64\www\Ces\ClassCes.php on line 19
	//	必须实现抽象类的所有抽象方法
	
	protected function prefixValue($prefix){
		$str = '接收成功'.$prefix;
		return $str;
	}
	// Fatal error: Access level to CLASS1::prefixValue() must be protected (as in class AbstractClass) or weaker in D:\wamp64\www\Ces\ClassCes.php on line 26
	//	实现抽象类的属性或方法,访问权限不能低于父类,不然将会发生语法错误。
}
//	Fatal error: Cannot instantiate abstract class Abs
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值