Move the C&S thing into the attic
This commit is contained in:
31
oldstuff/c&s20150725/demo.go
Normal file
31
oldstuff/c&s20150725/demo.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Person struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func NewPerson(name string) *Person {
|
||||
return &Person{
|
||||
Name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Person) Wave() string {
|
||||
return p.Name + " is waving"
|
||||
}
|
||||
|
||||
func (p *Person) WaveAt(other *Person) string {
|
||||
return p.Name + " waves at " + other.Name
|
||||
}
|
||||
|
||||
func main() {
|
||||
boo := NewPerson("Boo")
|
||||
fmt.Printf("%#v\n", boo.Wave())
|
||||
|
||||
hay := NewPerson("Hay")
|
||||
fmt.Printf("%#v\n", hay.Wave())
|
||||
|
||||
fmt.Printf("%#v\n", boo.WaveAt(hay))
|
||||
}
|
25
oldstuff/c&s20150725/demo.php
Normal file
25
oldstuff/c&s20150725/demo.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
class Person {
|
||||
public $name;
|
||||
|
||||
public function wave()
|
||||
{
|
||||
return $this->name. ' is waving';
|
||||
}
|
||||
|
||||
public function waveAt(Person $waveTarget)
|
||||
{
|
||||
return $this->name . ' waves at ' . $waveTarget->name;
|
||||
}
|
||||
}
|
||||
|
||||
$boo = new Person();
|
||||
$boo->name = 'Boo';
|
||||
var_dump($boo->wave());
|
||||
|
||||
$hay = new Person();
|
||||
$hay->name = 'Hay';
|
||||
var_dump($hay->wave());
|
||||
|
||||
var_dump($boo->waveAt($hay));
|
59
oldstuff/c&s20150725/inheritance.go
Normal file
59
oldstuff/c&s20150725/inheritance.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Pet struct {
|
||||
Name string
|
||||
IsRunning bool
|
||||
}
|
||||
|
||||
func NewPet(name string) *Pet {
|
||||
return &Pet{Name: name}
|
||||
}
|
||||
|
||||
func (p *Pet) Run() string {
|
||||
p.IsRunning = true
|
||||
return p.Name + " is running"
|
||||
}
|
||||
|
||||
type Dog struct {
|
||||
*Pet
|
||||
|
||||
currentChaseTarget *Pet
|
||||
}
|
||||
|
||||
func NewDog(name string) *Dog {
|
||||
return &Dog{Pet: NewPet(name)}
|
||||
}
|
||||
|
||||
func (d *Dog) Chase(target *Pet) string {
|
||||
d.IsRunning = true
|
||||
d.currentChaseTarget = target
|
||||
d.currentChaseTarget.Run()
|
||||
|
||||
return d.Name + " is chasing " + target.Name
|
||||
}
|
||||
|
||||
func main() {
|
||||
cat := NewPet("Felix")
|
||||
|
||||
fmt.Printf("%#v\n", cat)
|
||||
|
||||
cat.Run()
|
||||
|
||||
fmt.Printf("%#v\n", cat)
|
||||
|
||||
dog := NewDog("Fido")
|
||||
|
||||
fmt.Printf("%#v\n", dog)
|
||||
|
||||
dog.Run()
|
||||
|
||||
fmt.Printf("%#v\n", dog)
|
||||
|
||||
fmt.Printf("%#v\n", dog)
|
||||
fmt.Printf("%#v\n", dog.Chase(cat))
|
||||
|
||||
fmt.Printf("%#v\n", dog)
|
||||
fmt.Printf("%#v\n", cat)
|
||||
}
|
52
oldstuff/c&s20150725/inheritance.php
Normal file
52
oldstuff/c&s20150725/inheritance.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
class Pet {
|
||||
public $name;
|
||||
public $isRunning = false;
|
||||
|
||||
private function __construct()
|
||||
{}
|
||||
|
||||
public static function create($name)
|
||||
{
|
||||
$newPet = new static;
|
||||
$newPet->name = $name;
|
||||
return $newPet;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->isRunning = true;
|
||||
return $this->name . ' is running';
|
||||
}
|
||||
}
|
||||
|
||||
class Dog extends Pet {
|
||||
protected $currentChaseTarget;
|
||||
|
||||
public function chase(Pet $chaseTarget)
|
||||
{
|
||||
$this->isRunning = true;
|
||||
$this->currentChaseTarget = $chaseTarget;
|
||||
$this->currentChaseTarget->run();
|
||||
|
||||
return $this->name . ' is chasing ' . $chaseTarget->name;
|
||||
}
|
||||
}
|
||||
|
||||
$cat = Pet::create('Felix');
|
||||
|
||||
var_dump($cat);
|
||||
|
||||
$cat->run();
|
||||
|
||||
var_dump($cat);
|
||||
|
||||
$dog = Dog::create('Fido');
|
||||
|
||||
var_dump($dog);
|
||||
|
||||
var_dump($dog->chase($cat));
|
||||
|
||||
var_dump($dog);
|
||||
var_dump($cat);
|
Reference in New Issue
Block a user