tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
71
Task/Polymorphism/PHP/polymorphism-1.php
Normal file
71
Task/Polymorphism/PHP/polymorphism-1.php
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
class Point
|
||||
{
|
||||
protected $_x;
|
||||
|
||||
protected $_y;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
switch( func_num_args() )
|
||||
{
|
||||
case 1:
|
||||
$point = func_get_arg( 0 );
|
||||
$this->setFromPoint( $point );
|
||||
break;
|
||||
case 2:
|
||||
$x = func_get_arg( 0 );
|
||||
$y = func_get_arg( 1 );
|
||||
$this->setX( $x );
|
||||
$this->setY( $y );
|
||||
break;
|
||||
default:
|
||||
throw new InvalidArgumentException( 'expecting one (Point) argument or two (numeric x and y) arguments' );
|
||||
}
|
||||
}
|
||||
|
||||
public function setFromPoint( Point $point )
|
||||
{
|
||||
$this->setX( $point->getX() );
|
||||
$this->setY( $point->getY() );
|
||||
}
|
||||
|
||||
public function getX()
|
||||
{
|
||||
return $this->_x;
|
||||
}
|
||||
|
||||
public function setX( $x )
|
||||
{
|
||||
if( !is_numeric( $x ) )
|
||||
{
|
||||
throw new InvalidArgumentException( 'expecting numeric value' );
|
||||
}
|
||||
|
||||
$this->_x = (float) $x;
|
||||
}
|
||||
|
||||
public function getY()
|
||||
{
|
||||
return $this->_y;
|
||||
}
|
||||
|
||||
public function setY( $y )
|
||||
{
|
||||
if( !is_numeric( $y ) )
|
||||
{
|
||||
throw new InvalidArgumentException( 'expecting numeric value' );
|
||||
}
|
||||
|
||||
$this->_y = (float) $y;
|
||||
}
|
||||
|
||||
public function output()
|
||||
{
|
||||
echo $this->__toString();
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return 'Point [x:' . $this->_x . ',y:' . $this->_y . ']';
|
||||
}
|
||||
}
|
||||
63
Task/Polymorphism/PHP/polymorphism-2.php
Normal file
63
Task/Polymorphism/PHP/polymorphism-2.php
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
class Circle extends Point
|
||||
{
|
||||
private $_radius;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
switch( func_num_args() )
|
||||
{
|
||||
case 1:
|
||||
$circle = func_get_arg( 0 );
|
||||
$this->setFromCircle( $circle );
|
||||
break;
|
||||
case 2:
|
||||
$point = func_get_arg( 0 );
|
||||
$radius = func_get_arg( 1 );
|
||||
$this->setFromPoint( $point );
|
||||
$this->setRadius( $radius );
|
||||
break;
|
||||
case 3:
|
||||
$x = func_get_arg( 0 );
|
||||
$y = func_get_arg( 1 );
|
||||
$radius = func_get_arg( 2 );
|
||||
$this->setX( $x );
|
||||
$this->setY( $y );
|
||||
$this->setRadius( $radius );
|
||||
break;
|
||||
default:
|
||||
throw new InvalidArgumentException( 'expecting one (Circle) argument or two (Point and numeric radius) or three (numeric x, y and radius) arguments' );
|
||||
}
|
||||
}
|
||||
|
||||
public function setFromCircle( Circle $circle )
|
||||
{
|
||||
$this->setX( $circle->getX() );
|
||||
$this->setY( $circle->getY() );
|
||||
$this->setRadius( $circle->getRadius() );
|
||||
}
|
||||
|
||||
public function getPoint()
|
||||
{
|
||||
return new Point( $this->getX(), $this->getY() );
|
||||
}
|
||||
|
||||
public function getRadius()
|
||||
{
|
||||
return $this->_radius;
|
||||
}
|
||||
|
||||
public function setRadius( $radius )
|
||||
{
|
||||
if( !is_numeric( $radius ) )
|
||||
{
|
||||
throw new InvalidArgumentException( 'expecting numeric value' );
|
||||
}
|
||||
|
||||
$this->_radius = (float) $radius;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return 'Circle [' . $this->getPoint() . ',radius:' . $this->_radius . ']';
|
||||
}
|
||||
}
|
||||
12
Task/Polymorphism/PHP/polymorphism-3.php
Normal file
12
Task/Polymorphism/PHP/polymorphism-3.php
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
$point = new Point( 1, 5 );
|
||||
$circle = new Circle( 1, 5, 6 );
|
||||
|
||||
$point->output();
|
||||
// or
|
||||
echo $point;
|
||||
|
||||
echo "\n";
|
||||
|
||||
$circle->output();
|
||||
// or
|
||||
echo $circle;
|
||||
Loading…
Add table
Add a link
Reference in a new issue