Move the C&S thing into the attic

This commit is contained in:
Dan Buch
2015-07-25 14:41:14 -04:00
parent fc144fc403
commit af31512251
11 changed files with 0 additions and 0 deletions

View 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))
}

View 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));

View 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)
}

View 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);