https://www.phpxy.com/article/200.html

 

<?php

interface travelinterface 
{
	public function __construct($speed, $distance);
	public function run();
}

abstarct class travel implements travelinterface 
{
	protect $speed; 	//最高时速
	protect $distance; 	//最远路程

	public function __construct($speed, $distance)
	{
		$this->speed = $speed;
		$this->distance = $distance;
	}
}

class drive extends travel
{
	public function run()
	{
		echo '自驾游';
	}
}

class walk extends travel
{
	public function run()
	{
		echo '徒步旅行';
	}
}


class human 
{
	protect $travel;

	public function __construct(travel $travel)
	{
		$this->travel = $travel;
	}
	
	// public function __construct()
        // {
        //        $this->travel = new drive(60,1000);
        // }

	public function traveling()
	{
		$this->traveling->run();
	}
}

$travel = new drive(60, 1000);
$xiaoming = new human();
$xiaoming->traveling();


//配置
$config = [
"travel" => drive::class,
];
$travel = new $config["travel"](60,1000);
$xiaoming = new human($travel);
$xiaoming->traveling();

  

相关文章:

  • 2021-11-19
  • 2021-05-20
  • 2022-01-12
  • 2022-12-23
  • 2022-12-23
  • 2022-03-06
  • 1970-01-01
猜你喜欢
  • 2021-05-24
  • 2021-10-13
  • 2022-01-07
  • 2021-09-19
  • 2021-10-31
相关资源
相似解决方案