Differences
This shows you the differences between two versions of the page.
| jf_oop_1 [2017-09-01 12:54] | jf_oop_1 [2022-07-18 11:20] (current) | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Koden från genomgång nr1 på OOP ====== | ||
| + | <code php> | ||
| + | <?php | ||
| + | /** | ||
| + | * Exempel på en enkel klass | ||
| + | * Första lektion på Prog C | ||
| + | */ | ||
| + | class Animal { | ||
| + | protected | ||
| + | private $legs; | ||
| + |  | ||
| + | function __construct($weight=0){ | ||
| + | $this-> | ||
| + | } | ||
| + |  | ||
| + | public function getWeight() { // public är default | ||
| + | return ' | ||
| + | } | ||
| + |  | ||
| + | function setWeight($weight) { | ||
| + | $this-> | ||
| + | } | ||
| + | |||
| + | public function getLegs() { // public är default | ||
| + | return $this-> | ||
| + | } | ||
| + |  | ||
| + | function setLegs($legs) { | ||
| + | $this-> | ||
| + | } | ||
| + |  | ||
| + | function info() { | ||
| + | echo 'Vikt: ' . $this-> | ||
| + | } | ||
| + | } | ||
| + | |||
| + | class Dog extends Animal { | ||
| + |  | ||
| + | function test() { | ||
| + | return $this-> | ||
| + | } | ||
| + |  | ||
| + | function getWeight(){ | ||
| + | return ' | ||
| + | } | ||
| + |  | ||
| + | function getLegs() { | ||
| + | return 'Dog: ' . parent:: | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | include(' | ||
| + | |||
| + | $a2 = new Animal(23); | ||
| + | $a2-> | ||
| + | echo $a2-> | ||
| + | echo ' | ||
| + | |||
| + | $d1 = new Dog(18); | ||
| + | $d1-> | ||
| + | echo 'd1: ' . $d1-> | ||
| + | echo $d1-> | ||
| + | echo $d1-> | ||
| + | |||
| + | include(' | ||
| + | |||
| + | </ | ||