摘要:// public/Computer.php内容<?phpnamespace com;class Computer{ public $name; public $school; public function __construct($name,$school){ &
// public/Computer.php内容
<?php
namespace com;
class Computer
{
public $name;
public $school;
public function __construct($name,$school){
$this->name = $name;
$this->school = $school;
echo $name.'在'.$school.'上学';
}
public function key(){
return __NAMESPACE__;
}
}
// test.php内容
<?php
namespace test;
//引入外部命名空间
use com\Computer as Comer;
//导入public下的Computer.php文件
require 'public/Computer.php';
//声明一个同名的Computer类
class Computer
{
//定义私有属性
private $key;
private $mouse;
private $monitor;
public function __construct($key,$mouse,$monitor){
$this->key = $key;
$this->mouse = $mouse;
$this->monitor = $monitor;
}
//定义获取私有属性的方法
public function getKey(){
return $this->key;
}
public function getMouse(){
return $this->mouse;
}
public function getMonitor(){
return $this->monitor;
}
}
$comer = new Comer('小明','河地大');
echo '<br>';
$computer = new Computer('键盘','鼠标','显示器');
echo $comer->key().'<br>';
echo '电脑的主要硬件包括:'.$computer->getKey().' '.$computer->getMouse().' '.$computer->getMonitor().'<br>';
批改老师:韦小宝批改时间:2018-11-24 11:35:28
老师总结:写的还可以哦!但是下次记得加上代码的高亮以及自己的总结!命名空间是很重要的知识点!课后还要记得多多练习哦!