2 回答
TA贡献1895条经验 获得超3个赞
我希望如果我能提供帮助,您可以在验证时检查类类型。
class Reader{
protected $requests;
protected $name;
public function get_name(){
return $this->name;
}
}
class Teacher extends Reader{
}
class Student extends Reader{
}
class GeneralPublic extends Reader{
}
class Book{
protected $name;
public function __construct($name){
$this->name = $name;
}
}
class Requests
{
protected $bookRequested;
protected $reader;
protected $requestDate;
protected $returnDate;
public function __construct(Book $bookRequested, Reader $reader)
{
$this->bookRequested = $bookRequested;
$this->reader = $reader;
$this->requestDate = date('Y-m-d H:i:s');
$this->returnDate = date('Y-m-d H:i:s');
}
public function validate(){
echo get_class($this->reader);
}
}
$teacher = new Teacher();
$student = new Student();
$gen_pu = new GeneralPublic();
$book = new Book("Travel");
$req = new Requests($book,$teacher);
$req->validate();
TA贡献1820条经验 获得超2个赞
我不确定为什么在同一个请求中需要所有三个读者,因为只有其中一个会涉及给定的事务。如果你有这个:
abstract class Reader {}
class Teacher extends Reader {}
class Student extends Reader {}
class GeneralPublic extends Reader {}
...那么你的构造函数只需要一个参数,基类:
public function __construct(Book $bookRequested, Reader $reader) {}
要配置东西,您只需创建一个属性或方法。如果您选择一种方法,您也可以将其抽象化:
abstract class Reader
{
abstract public function getMaxBooks();
}
class Teacher extends Reader
{
public function getMaxBooks() {
return 10;
}
}
- 2 回答
- 0 关注
- 140 浏览
添加回答
举报