PHP基础之多态和接口

1.多态原理

class A{
		function test1($p){
			echo "test1".$p;
		}
		
		function test2($p1,$p2){
			echo "test2".$p1.$p2;
		}
		function __call($method,$p){
			if($method=='test'){
				if(count($p)==1){
					$this->test1('hello');
				}elseif(count($p)==2){
					$this->test2('yaksun',123);
				}
			}
		}
		
	}
	
	$a = new A();
	
	$a->test(1);
	echo '<br />';
	$a->test(23,89);

2.接口实现

interface fishAble{
		function swimming();
	}
	interface birdAble{
		function fly();
	}
	class Monkey{
		
		function climing(){
			echo "猴子会爬树";
		}
	}
	
	class miniMonkey extends Monkey implements fishAble,birdAble{
		public $name;
		
		function __construct($name){
			$this->name=$name;
		}
		
		
		function swimming(){
			echo $this->name."有游泳的能力";
		}
		function fly(){
			echo "有飞行的能力";
		}
		
	}
	$mini = new miniMonkey("孙悟空");
	echo $mini->name;
	$mini->climing();
	$mini->swimming();
	

猜你喜欢

转载自blog.csdn.net/weixin_42819066/article/details/85684848
今日推荐