A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
26
Task/Inheritance-Single/0DESCRIPTION
Normal file
26
Task/Inheritance-Single/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
:''This task is about derived types; for implementation inheritance, see [[Polymorphism]].
|
||||
|
||||
Inheritance is an operation of [[type algebra]] that creates a new type from one or several parent types. The obtained type is called '''derived type'''. It inherits some of the properties of its parent types. Usually inherited properties are:
|
||||
* methods
|
||||
* components
|
||||
* parts of the representation
|
||||
The [[classes | class]] of the new type is a '''subclass''' of the classes rooted in the parent types. When all (in certain sense) properties of the parents are preserved by the derived type, it is said to be a [[wp:Liskov_substitution_principle|Liskov subtype]]. When properties are preserved then the derived type is ''substitutable'' for its parents in all contexts. Usually full substitutability is achievable only in some contexts.
|
||||
|
||||
Inheritance is
|
||||
* '''single''', when only one parent is allowed
|
||||
* '''[[multiple inheritance | multiple]]''', otherwise
|
||||
|
||||
Some single inheritance languages usually allow multiple inheritance for certain [[abstract type]]s, interfaces in particular.
|
||||
|
||||
Inheritance can be considered as a relation parent-child. Parent types are sometimes called '''supertype''', the derived ones are '''subtype'''. This relation is [[wp:Transitive_relation|transitive]] and [[wp:Reflexive_relation|reflexive]]. Types bound by the relation form a [[wp:Directed_acyclic_graph directed acyclic graph]] (ignoring reflexivity). With single inheritance it becomes a [[wp:Tree_(graph_theory)|tree]].
|
||||
|
||||
'''Task''': Show a tree of types which inherit from each other. The top of the tree should be a class called Animal. The second level should have Dog and Cat. Under Dog should be Lab and Collie. None of the classes need to have any functions, the only thing they need to do is inherit from the specified superclasses (overriding functions should be shown in [[Polymorphism]]). The tree should look like this:
|
||||
<pre> Animal
|
||||
/\
|
||||
/ \
|
||||
/ \
|
||||
Dog Cat
|
||||
/\
|
||||
/ \
|
||||
/ \
|
||||
Lab Collie</pre>
|
||||
6
Task/Inheritance-Single/1META.yaml
Normal file
6
Task/Inheritance-Single/1META.yaml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
category:
|
||||
- Object oriented
|
||||
- Type System
|
||||
- Encyclopedia
|
||||
note: Basic language learning
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
public class Animal {
|
||||
// ...
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
public class Cat extends Animal {
|
||||
// ...
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
public class Dog extends Animal {
|
||||
// ...
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
public class Lab extends Dog {
|
||||
// ...
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
public class Collie extends Dog {
|
||||
// ...
|
||||
}
|
||||
13
Task/Inheritance-Single/Ada/inheritance-single.ada
Normal file
13
Task/Inheritance-Single/Ada/inheritance-single.ada
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package Inheritance is
|
||||
type Animal is tagged private;
|
||||
type Dog is new Animal with private;
|
||||
type Cat is new Animal with private;
|
||||
type Lab is new Dog with private;
|
||||
type Collie is new Dog with private;
|
||||
private
|
||||
type Animal is tagged null record;
|
||||
type Dog is new Animal with null record;
|
||||
type Cat is new Animal with null record;
|
||||
type Lab is new Dog with null record;
|
||||
type Collie is new Dog with null record;
|
||||
end Inheritance;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class Animal{
|
||||
//functions go here...
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class Dog extends Animal {
|
||||
//functions go here...
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class Cat extends Animal {
|
||||
//functions go here...
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class Lab extends Dog {
|
||||
//functions go here...
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class Collie extends Dog {
|
||||
//functions go here...
|
||||
}
|
||||
14
Task/Inheritance-Single/AutoHotkey/inheritance-single.ahk
Normal file
14
Task/Inheritance-Single/AutoHotkey/inheritance-single.ahk
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
dog := new Collie
|
||||
MsgBox, % "A " dog.__Class " is a " dog.base.base.__Class " and is part of the " dog.kingdom " kingdom."
|
||||
|
||||
class Animal {
|
||||
static kingdom := "Animalia" ; Class variable
|
||||
}
|
||||
class Dog extends Animal {
|
||||
}
|
||||
class Cat extends Animal {
|
||||
}
|
||||
class Lab extends Dog {
|
||||
}
|
||||
class Collie extends Dog {
|
||||
}
|
||||
20
Task/Inheritance-Single/BBC-BASIC/inheritance-single.bbc
Normal file
20
Task/Inheritance-Single/BBC-BASIC/inheritance-single.bbc
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
INSTALL @lib$+"CLASSLIB"
|
||||
|
||||
DIM Animal{method}
|
||||
PROC_class(Animal{})
|
||||
|
||||
DIM Cat{method}
|
||||
PROC_inherit(Cat{}, Animal{})
|
||||
PROC_class(Cat{})
|
||||
|
||||
DIM Dog{method}
|
||||
PROC_inherit(Dog{}, Animal{})
|
||||
PROC_class(Dog{})
|
||||
|
||||
DIM Labrador{method}
|
||||
PROC_inherit(Labrador{}, Dog{})
|
||||
PROC_class(Labrador{})
|
||||
|
||||
DIM Collie{method}
|
||||
PROC_inherit(Collie{}, Dog{})
|
||||
PROC_class(Collie{})
|
||||
24
Task/Inheritance-Single/C++/inheritance-single.cpp
Normal file
24
Task/Inheritance-Single/C++/inheritance-single.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
class Animal
|
||||
{
|
||||
// ...
|
||||
};
|
||||
|
||||
class Dog: public Animal
|
||||
{
|
||||
// ...
|
||||
};
|
||||
|
||||
class Lab: public Dog
|
||||
{
|
||||
// ...
|
||||
};
|
||||
|
||||
class Collie: public Dog
|
||||
{
|
||||
// ...
|
||||
};
|
||||
|
||||
class Cat: public Animal
|
||||
{
|
||||
// ...
|
||||
};
|
||||
5
Task/Inheritance-Single/Clojure/inheritance-single-1.clj
Normal file
5
Task/Inheritance-Single/Clojure/inheritance-single-1.clj
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(gen-class :name Animal)
|
||||
(gen-class :name Dog :extends Animal)
|
||||
(gen-class :name Cat :extends Animal)
|
||||
(gen-class :name Lab :extends Dog)
|
||||
(gen-class :name Collie :extends Dog)
|
||||
4
Task/Inheritance-Single/Clojure/inheritance-single-2.clj
Normal file
4
Task/Inheritance-Single/Clojure/inheritance-single-2.clj
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(derive ::dog ::animal)
|
||||
(derive ::cat ::animal)
|
||||
(derive ::lab ::dog)
|
||||
(derive ::collie ::dog)
|
||||
6
Task/Inheritance-Single/Clojure/inheritance-single-3.clj
Normal file
6
Task/Inheritance-Single/Clojure/inheritance-single-3.clj
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
user> (isa? ::dog ::animal)
|
||||
true
|
||||
user> (isa? ::dog ::cat)
|
||||
false
|
||||
user> (isa? ::collie ::animal)
|
||||
true
|
||||
5
Task/Inheritance-Single/Clojure/inheritance-single-4.clj
Normal file
5
Task/Inheritance-Single/Clojure/inheritance-single-4.clj
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(defclass animal () ())
|
||||
(defclass dog (animal) ())
|
||||
(defclass lab (dog) ())
|
||||
(defclass collie (dog) ())
|
||||
(defclass cat (animal) ())
|
||||
5
Task/Inheritance-Single/Clojure/inheritance-single-5.clj
Normal file
5
Task/Inheritance-Single/Clojure/inheritance-single-5.clj
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(defstruct animal)
|
||||
(defstruct (dog (:include animal)))
|
||||
(defstruct (lab (:include dog)))
|
||||
(defstruct (collie (:include dog)))
|
||||
(defstruct (cat (:include animal)))
|
||||
9
Task/Inheritance-Single/Clojure/inheritance-single-6.clj
Normal file
9
Task/Inheritance-Single/Clojure/inheritance-single-6.clj
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
;;; ASN.1 serialization logic specialized for animal class
|
||||
(defmethod serialize-to-asn-1 ((a animal))
|
||||
#| ... |#
|
||||
)
|
||||
|
||||
;;; casually introduce the method over strings too; no relation to animal
|
||||
(defmethod serialize-to-asn-1 ((s string))
|
||||
#| ... #|
|
||||
)
|
||||
6
Task/Inheritance-Single/Clojure/inheritance-single-7.clj
Normal file
6
Task/Inheritance-Single/Clojure/inheritance-single-7.clj
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
TYPE
|
||||
Animal = ABSTRACT RECORD (* *) END;
|
||||
Cat = RECORD (Animal) (* *) END; (* final record (cannot be extended) - by default *)
|
||||
Dog = EXTENSIBLE RECORD (Animal) (* *) END; (* extensible record *)
|
||||
Lab = RECORD (Dog) (* *) END;
|
||||
Collie = RECORD (Dog) (* *) END;
|
||||
21
Task/Inheritance-Single/D/inheritance-single.d
Normal file
21
Task/Inheritance-Single/D/inheritance-single.d
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
class Animal {
|
||||
// ...
|
||||
}
|
||||
|
||||
class Dog: Animal {
|
||||
// ...
|
||||
}
|
||||
|
||||
class Lab: Dog {
|
||||
// ...
|
||||
}
|
||||
|
||||
class Collie: Dog {
|
||||
// ...
|
||||
}
|
||||
|
||||
class Cat: Animal {
|
||||
// ...
|
||||
}
|
||||
|
||||
void main() {}
|
||||
12
Task/Inheritance-Single/DWScript/inheritance-single.dwscript
Normal file
12
Task/Inheritance-Single/DWScript/inheritance-single.dwscript
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
type
|
||||
Animal = class(TObject)
|
||||
private
|
||||
// private functions/variables
|
||||
public
|
||||
// public functions/variables
|
||||
end;
|
||||
|
||||
type Dog = class(Animal) end;
|
||||
type Cat = class(Animal) end;
|
||||
type Collie = class(Dog) end;
|
||||
type Lab = class(Dog) end;
|
||||
12
Task/Inheritance-Single/Delphi/inheritance-single.delphi
Normal file
12
Task/Inheritance-Single/Delphi/inheritance-single.delphi
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
type
|
||||
Animal = class(TObject)
|
||||
private
|
||||
// private functions/variables
|
||||
public
|
||||
// public functions/variables
|
||||
end;
|
||||
|
||||
Dog = class(Animal);
|
||||
Cat = class(Animal);
|
||||
Collie = class(Dog);
|
||||
Lab = class(Dog);
|
||||
18
Task/Inheritance-Single/E/inheritance-single-1.e
Normal file
18
Task/Inheritance-Single/E/inheritance-single-1.e
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
def makeType(label, superstamps) {
|
||||
def stamp {
|
||||
to audit(audition) {
|
||||
for s in superstamps { audition.ask(s) }
|
||||
return true
|
||||
}
|
||||
}
|
||||
def guard {
|
||||
to coerce(specimen, ejector) {
|
||||
if (__auditedBy(stamp, specimen)) {
|
||||
return specimen
|
||||
} else {
|
||||
throw.eject(ejector, `$specimen is not a $label`)
|
||||
}
|
||||
}
|
||||
}
|
||||
return [guard, stamp]
|
||||
}
|
||||
7
Task/Inheritance-Single/E/inheritance-single-2.e
Normal file
7
Task/Inheritance-Single/E/inheritance-single-2.e
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def [Animal, AnimalStamp] := makeType("Animal", [])
|
||||
|
||||
def [Cat, CatStamp] := makeType("Cat", [AnimalStamp])
|
||||
def [Dog, DogStamp] := makeType("Dog", [AnimalStamp])
|
||||
|
||||
def [Lab, LabStamp] := makeType("Lab", [DogStamp])
|
||||
def [Collie, CollieStamp] := makeType("Collie", [DogStamp])
|
||||
3
Task/Inheritance-Single/E/inheritance-single-3.e
Normal file
3
Task/Inheritance-Single/E/inheritance-single-3.e
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def fido implements LabStamp {}
|
||||
def tom implements CatStamp {}
|
||||
def brick {} # not an animal
|
||||
17
Task/Inheritance-Single/E/inheritance-single-4.e
Normal file
17
Task/Inheritance-Single/E/inheritance-single-4.e
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
? fido :Animal
|
||||
# value: <fido>
|
||||
|
||||
? fido :Cat
|
||||
# problem: <fido> is not a Cat
|
||||
|
||||
? fido :Lab
|
||||
# value: <fido>
|
||||
|
||||
? tom :Animal
|
||||
# value: <tom>
|
||||
|
||||
? tom :Cat
|
||||
# value: <tom>
|
||||
|
||||
? brick :Animal
|
||||
# problem: <brick> is not a Animal
|
||||
3
Task/Inheritance-Single/Eiffel/inheritance-single-1.e
Normal file
3
Task/Inheritance-Single/Eiffel/inheritance-single-1.e
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class
|
||||
ANIMAL
|
||||
end
|
||||
5
Task/Inheritance-Single/Eiffel/inheritance-single-2.e
Normal file
5
Task/Inheritance-Single/Eiffel/inheritance-single-2.e
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class
|
||||
DOG
|
||||
inherit
|
||||
ANIMAL
|
||||
end
|
||||
5
Task/Inheritance-Single/Eiffel/inheritance-single-3.e
Normal file
5
Task/Inheritance-Single/Eiffel/inheritance-single-3.e
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class
|
||||
CAT
|
||||
inherit
|
||||
ANIMAL
|
||||
end
|
||||
5
Task/Inheritance-Single/Eiffel/inheritance-single-4.e
Normal file
5
Task/Inheritance-Single/Eiffel/inheritance-single-4.e
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class
|
||||
LAB
|
||||
inherit
|
||||
DOG
|
||||
end
|
||||
5
Task/Inheritance-Single/Eiffel/inheritance-single-5.e
Normal file
5
Task/Inheritance-Single/Eiffel/inheritance-single-5.e
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class
|
||||
COLLIE
|
||||
inherit
|
||||
DOG
|
||||
end
|
||||
5
Task/Inheritance-Single/Factor/inheritance-single.factor
Normal file
5
Task/Inheritance-Single/Factor/inheritance-single.factor
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
TUPLE: animal ;
|
||||
TUPLE: dog < animal ;
|
||||
TUPLE: cat < animal ;
|
||||
TUPLE: lab < dog ;
|
||||
TUPLE: collie < dog ;
|
||||
19
Task/Inheritance-Single/Fancy/inheritance-single.fancy
Normal file
19
Task/Inheritance-Single/Fancy/inheritance-single.fancy
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
class Animal {
|
||||
# ...
|
||||
}
|
||||
|
||||
class Dog : Animal {
|
||||
# ...
|
||||
}
|
||||
|
||||
class Cat : Animal {
|
||||
# ...
|
||||
}
|
||||
|
||||
class Lab : Dog {
|
||||
# ...
|
||||
}
|
||||
|
||||
class Collie : Dog {
|
||||
# ...
|
||||
}
|
||||
19
Task/Inheritance-Single/Fantom/inheritance-single.fantom
Normal file
19
Task/Inheritance-Single/Fantom/inheritance-single.fantom
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
class Animal
|
||||
{
|
||||
}
|
||||
|
||||
class Dog : Animal
|
||||
{
|
||||
}
|
||||
|
||||
class Cat : Animal
|
||||
{
|
||||
}
|
||||
|
||||
class Lab : Dog
|
||||
{
|
||||
}
|
||||
|
||||
class Collie : Dog
|
||||
{
|
||||
}
|
||||
7
Task/Inheritance-Single/Forth/inheritance-single.fth
Normal file
7
Task/Inheritance-Single/Forth/inheritance-single.fth
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
include 4pp/lib/foos.4pp
|
||||
|
||||
:: Animal class end-class {} ;
|
||||
:: Dog extends Animal end-extends {} ;
|
||||
:: Cat extends Animal end-extends {} ;
|
||||
:: Lab extends Dog end-extends {} ;
|
||||
:: Collie extends Dog end-extends {} ;
|
||||
18
Task/Inheritance-Single/Fortran/inheritance-single.f
Normal file
18
Task/Inheritance-Single/Fortran/inheritance-single.f
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
module anim
|
||||
|
||||
type animal
|
||||
end type animal
|
||||
|
||||
type, extends(animal) :: dog
|
||||
end type dog
|
||||
|
||||
type, extends(animal) :: cat
|
||||
end type cat
|
||||
|
||||
type, extends(dog) :: lab
|
||||
end type lab
|
||||
|
||||
type, extends(dog) :: collie
|
||||
end type collie
|
||||
|
||||
end module anim
|
||||
32
Task/Inheritance-Single/Go/inheritance-single.go
Normal file
32
Task/Inheritance-Single/Go/inheritance-single.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package main
|
||||
|
||||
type animal struct {
|
||||
alive bool
|
||||
}
|
||||
|
||||
type dog struct {
|
||||
animal
|
||||
obedienceTrained bool
|
||||
}
|
||||
|
||||
type cat struct {
|
||||
animal
|
||||
litterBoxTrained bool
|
||||
}
|
||||
|
||||
type lab struct {
|
||||
dog
|
||||
color string
|
||||
}
|
||||
|
||||
type collie struct {
|
||||
dog
|
||||
catchesFrisbee bool
|
||||
}
|
||||
|
||||
func main() {
|
||||
var pet lab
|
||||
pet.alive = true
|
||||
pet.obedienceTrained = false
|
||||
pet.color = "yellow"
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class Animal{
|
||||
//contents go here...
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class Dog extends Animal{
|
||||
//contents go here...
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class Cat extends Animal{
|
||||
//contents go here...
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class Lab extends Dog{
|
||||
//contents go here...
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class Collie extends Dog{
|
||||
//contents go here...
|
||||
}
|
||||
5
Task/Inheritance-Single/Haskell/inheritance-single.hs
Normal file
5
Task/Inheritance-Single/Haskell/inheritance-single.hs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class Animal a
|
||||
class Animal a => Cat a
|
||||
class Animal a => Dog a
|
||||
class Dog a => Lab a
|
||||
class Dog a => Collie a
|
||||
3
Task/Inheritance-Single/Haxe/inheritance-single-1.haxe
Normal file
3
Task/Inheritance-Single/Haxe/inheritance-single-1.haxe
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class Animal {
|
||||
// ...
|
||||
}
|
||||
3
Task/Inheritance-Single/Haxe/inheritance-single-2.haxe
Normal file
3
Task/Inheritance-Single/Haxe/inheritance-single-2.haxe
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class Cat extends Animal {
|
||||
// ...
|
||||
}
|
||||
3
Task/Inheritance-Single/Haxe/inheritance-single-3.haxe
Normal file
3
Task/Inheritance-Single/Haxe/inheritance-single-3.haxe
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class Dog extends Animal {
|
||||
// ...
|
||||
}
|
||||
3
Task/Inheritance-Single/Haxe/inheritance-single-4.haxe
Normal file
3
Task/Inheritance-Single/Haxe/inheritance-single-4.haxe
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class Lab extends Dog {
|
||||
// ...
|
||||
}
|
||||
3
Task/Inheritance-Single/Haxe/inheritance-single-5.haxe
Normal file
3
Task/Inheritance-Single/Haxe/inheritance-single-5.haxe
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class Collie extends Dog {
|
||||
// ...
|
||||
}
|
||||
14
Task/Inheritance-Single/Haxe/inheritance-single-6.haxe
Normal file
14
Task/Inheritance-Single/Haxe/inheritance-single-6.haxe
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
class Animal ()
|
||||
end
|
||||
|
||||
class Dog : Animal ()
|
||||
end
|
||||
|
||||
class Cat : Animal ()
|
||||
end
|
||||
|
||||
class Lab : Dog ()
|
||||
end
|
||||
|
||||
class Collie : Dog ()
|
||||
end
|
||||
5
Task/Inheritance-Single/Inform-7/inheritance-single.inf
Normal file
5
Task/Inheritance-Single/Inform-7/inheritance-single.inf
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
An animal is a kind of thing.
|
||||
A cat is a kind of animal.
|
||||
A dog is a kind of animal.
|
||||
A collie is a kind of dog.
|
||||
A lab is a kind of dog.
|
||||
5
Task/Inheritance-Single/Io/inheritance-single.io
Normal file
5
Task/Inheritance-Single/Io/inheritance-single.io
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Animal := Object clone
|
||||
Cat := Animal clone
|
||||
Dog := Animal clone
|
||||
Collie := Dog clone
|
||||
Lab := Dog clone
|
||||
1
Task/Inheritance-Single/J/inheritance-single-1.j
Normal file
1
Task/Inheritance-Single/J/inheritance-single-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
coclass 'Animal'
|
||||
2
Task/Inheritance-Single/J/inheritance-single-2.j
Normal file
2
Task/Inheritance-Single/J/inheritance-single-2.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
coclass 'Dog'
|
||||
coinsert 'Animal'
|
||||
2
Task/Inheritance-Single/J/inheritance-single-3.j
Normal file
2
Task/Inheritance-Single/J/inheritance-single-3.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
coclass 'Cat'
|
||||
coinsert 'Animal'
|
||||
2
Task/Inheritance-Single/J/inheritance-single-4.j
Normal file
2
Task/Inheritance-Single/J/inheritance-single-4.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
coclass 'Lab'
|
||||
coinsert 'Dog'
|
||||
2
Task/Inheritance-Single/J/inheritance-single-5.j
Normal file
2
Task/Inheritance-Single/J/inheritance-single-5.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
coclass 'Collie'
|
||||
coinsert 'Dog'
|
||||
4
Task/Inheritance-Single/J/inheritance-single-6.j
Normal file
4
Task/Inheritance-Single/J/inheritance-single-6.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
coinsert_Dog_ 'Animal'
|
||||
coinsert_Cat_ 'Animal'
|
||||
coinsert_Lab_ 'Dog'
|
||||
coinsert_Collie_ 'Dog'
|
||||
3
Task/Inheritance-Single/Java/inheritance-single-1.java
Normal file
3
Task/Inheritance-Single/Java/inheritance-single-1.java
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
public class Animal{
|
||||
//functions go here...
|
||||
}
|
||||
3
Task/Inheritance-Single/Java/inheritance-single-2.java
Normal file
3
Task/Inheritance-Single/Java/inheritance-single-2.java
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
public class Dog extends Animal{
|
||||
//functions go here...
|
||||
}
|
||||
3
Task/Inheritance-Single/Java/inheritance-single-3.java
Normal file
3
Task/Inheritance-Single/Java/inheritance-single-3.java
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
public class Cat extends Animal{
|
||||
//functions go here...
|
||||
}
|
||||
3
Task/Inheritance-Single/Java/inheritance-single-4.java
Normal file
3
Task/Inheritance-Single/Java/inheritance-single-4.java
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
public class Lab extends Dog{
|
||||
//functions go here...
|
||||
}
|
||||
3
Task/Inheritance-Single/Java/inheritance-single-5.java
Normal file
3
Task/Inheritance-Single/Java/inheritance-single-5.java
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
public class Collie extends Dog{
|
||||
//functions go here...
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
function Animal() {
|
||||
// ...
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
function Dog() {
|
||||
// ...
|
||||
}
|
||||
Dog.prototype = new Animal();
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
function Cat() {
|
||||
// ...
|
||||
}
|
||||
Cat.prototype = new Animal();
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
function Collie() {
|
||||
// ...
|
||||
}
|
||||
Collie.prototype = new Dog();
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
function Lab() {
|
||||
// ...
|
||||
}
|
||||
Lab.prototype = new Dog();
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
Animal.prototype.speak = function() {print("an animal makes a sound")};
|
||||
|
||||
var lab = new Lab();
|
||||
lab.speak(); // shows "an animal makes a sound"
|
||||
15
Task/Inheritance-Single/Kite/inheritance-single.kite
Normal file
15
Task/Inheritance-Single/Kite/inheritance-single.kite
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
class Animal [
|
||||
#Method goes here
|
||||
];
|
||||
|
||||
class Dog from Animal [
|
||||
#Method goes here
|
||||
];
|
||||
|
||||
class Lab from Dog [
|
||||
#Method goes here
|
||||
];
|
||||
|
||||
class collie from Dog [
|
||||
#Method goes here
|
||||
];
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Section Header
|
||||
+ name := ANIMAL;
|
||||
// ...
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
Section Header
|
||||
+ name := CAT;
|
||||
Section Inherit
|
||||
- parent : ANIMAL := ANIMAL;
|
||||
// ...
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
Section Header
|
||||
+ name := DOG;
|
||||
Section Inherit
|
||||
- parent : ANIMAL := ANIMAL;
|
||||
// ...
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
Section Header
|
||||
+ name := LAB;
|
||||
Section Inherit
|
||||
- parent : DOG := DOG;
|
||||
// ...
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
Section Header
|
||||
+ name := COLLIE;
|
||||
Section Inherit
|
||||
- parent : DOG := DOG;
|
||||
// ...
|
||||
28
Task/Inheritance-Single/Logtalk/inheritance-single.logtalk
Normal file
28
Task/Inheritance-Single/Logtalk/inheritance-single.logtalk
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
:- object(thing,
|
||||
instantiates(thing)).
|
||||
:- end_object.
|
||||
|
||||
:- object(animal,
|
||||
specializes(thing)).
|
||||
...
|
||||
:- end_object.
|
||||
|
||||
:- object(dog,
|
||||
specializes(animal)).
|
||||
...
|
||||
:- end_object.
|
||||
|
||||
:- object(cat,
|
||||
specializes(animal)).
|
||||
...
|
||||
:- end_object.
|
||||
|
||||
:- object(lab,
|
||||
specializes(dog)).
|
||||
...
|
||||
:- end_object.
|
||||
|
||||
:- object(collie,
|
||||
specializes(dog)).
|
||||
...
|
||||
:- end_object.
|
||||
19
Task/Inheritance-Single/PHP/inheritance-single.php
Normal file
19
Task/Inheritance-Single/PHP/inheritance-single.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
class Animal {
|
||||
// functions go here...
|
||||
}
|
||||
|
||||
class Dog extends Animal {
|
||||
// functions go here...
|
||||
}
|
||||
|
||||
class Cat extends Animal {
|
||||
// functions go here...
|
||||
}
|
||||
|
||||
class Lab extends Dog {
|
||||
// functions go here...
|
||||
}
|
||||
|
||||
class Collie extends Dog {
|
||||
// functions go here...
|
||||
}
|
||||
3
Task/Inheritance-Single/Perl/inheritance-single-1.pl
Normal file
3
Task/Inheritance-Single/Perl/inheritance-single-1.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
package Animal;
|
||||
#functions go here...
|
||||
1;
|
||||
5
Task/Inheritance-Single/Perl/inheritance-single-2.pl
Normal file
5
Task/Inheritance-Single/Perl/inheritance-single-2.pl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package Dog;
|
||||
use Animal;
|
||||
@ISA = qw( Animal );
|
||||
#functions go here...
|
||||
1;
|
||||
5
Task/Inheritance-Single/Perl/inheritance-single-3.pl
Normal file
5
Task/Inheritance-Single/Perl/inheritance-single-3.pl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package Cat;
|
||||
use Animal;
|
||||
@ISA = qw( Animal );
|
||||
#functions go here...
|
||||
1;
|
||||
5
Task/Inheritance-Single/Perl/inheritance-single-4.pl
Normal file
5
Task/Inheritance-Single/Perl/inheritance-single-4.pl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package Lab;
|
||||
use Dog;
|
||||
@ISA = qw( Dog );
|
||||
#functions go here...
|
||||
1;
|
||||
5
Task/Inheritance-Single/Perl/inheritance-single-5.pl
Normal file
5
Task/Inheritance-Single/Perl/inheritance-single-5.pl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package Collie;
|
||||
use Dog;
|
||||
@ISA = qw( Dog );
|
||||
#functions go here...
|
||||
1;
|
||||
17
Task/Inheritance-Single/Perl/inheritance-single-6.pl
Normal file
17
Task/Inheritance-Single/Perl/inheritance-single-6.pl
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
use MooseX::Declare;
|
||||
|
||||
class Animal {
|
||||
# methods go here...
|
||||
}
|
||||
class Dog extends Animal {
|
||||
# methods go here...
|
||||
}
|
||||
class Cat extends Animal {
|
||||
# methods go here...
|
||||
}
|
||||
class Lab extends Dog {
|
||||
# methods go here...
|
||||
}
|
||||
class Collie extends Dog {
|
||||
# methods go here...
|
||||
}
|
||||
9
Task/Inheritance-Single/PicoLisp/inheritance-single-1.l
Normal file
9
Task/Inheritance-Single/PicoLisp/inheritance-single-1.l
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(class +Animal)
|
||||
|
||||
(class +Dog +Animal)
|
||||
|
||||
(class +Cat +Animal)
|
||||
|
||||
(class +Lab +Dog)
|
||||
|
||||
(class +Collie +Dog)
|
||||
6
Task/Inheritance-Single/PicoLisp/inheritance-single-2.l
Normal file
6
Task/Inheritance-Single/PicoLisp/inheritance-single-2.l
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
: (dep '+Animal)
|
||||
+Animal
|
||||
+Cat
|
||||
+Dog
|
||||
+Collie
|
||||
+Lab
|
||||
14
Task/Inheritance-Single/Python/inheritance-single-1.py
Normal file
14
Task/Inheritance-Single/Python/inheritance-single-1.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
class Animal:
|
||||
pass #functions go here...
|
||||
|
||||
class Dog(Animal):
|
||||
pass #functions go here...
|
||||
|
||||
class Cat(Animal):
|
||||
pass #functions go here...
|
||||
|
||||
class Lab(Dog):
|
||||
pass #functions go here...
|
||||
|
||||
class Collie(Dog):
|
||||
pass #functions go here...
|
||||
47
Task/Inheritance-Single/Python/inheritance-single-2.py
Normal file
47
Task/Inheritance-Single/Python/inheritance-single-2.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import time
|
||||
|
||||
class Animal(object):
|
||||
def __init__(self, birth=None, alive=True):
|
||||
self.birth = birth if birth else time.time()
|
||||
self.alive = alive
|
||||
def age(self):
|
||||
return time.time() - self.birth
|
||||
def kill(self):
|
||||
self.alive = False
|
||||
|
||||
class Dog(Animal):
|
||||
def __init__(self, bones_collected=0, **kwargs):
|
||||
self.bone_collected = bones_collected
|
||||
super(Dog, self).__init__(**kwargs)
|
||||
|
||||
class Cat(Animal):
|
||||
max_lives = 9
|
||||
def __init__(self, lives=max_lives, **kwargs):
|
||||
self.lives = lives
|
||||
super(Cat, self).__init__(**kwargs)
|
||||
def kill(self):
|
||||
if self.lives>0:
|
||||
self.lives -= 1
|
||||
if self.lives == 0:
|
||||
super(Cat, self).kill()
|
||||
else:
|
||||
raise ValueError
|
||||
return self
|
||||
|
||||
class Labrador(Dog):
|
||||
def __init__(self, guide_dog=False, **kwargs):
|
||||
self.guide_dog=False
|
||||
super(Labrador, self).__init__(**kwargs)
|
||||
|
||||
class Collie(Dog):
|
||||
def __init__(self, sheep_dog=False, **kwargs):
|
||||
self.sheep_dog=False
|
||||
super(Collie, self).__init__(**kwargs)
|
||||
|
||||
lassie = Collie()
|
||||
felix = Cat()
|
||||
felix.kill().kill().kill()
|
||||
mr_winkle = Dog()
|
||||
buddy = Labrador()
|
||||
buddy.kill()
|
||||
print "Felix has",felix.lives, "lives, ","Buddy is %salive!"%("" if buddy.alive else "not ")
|
||||
2
Task/Inheritance-Single/R/inheritance-single-1.r
Normal file
2
Task/Inheritance-Single/R/inheritance-single-1.r
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
aCollie <- "woof"
|
||||
class(aCollie) <- c("Collie", "Dog", "Animal")
|
||||
5
Task/Inheritance-Single/R/inheritance-single-2.r
Normal file
5
Task/Inheritance-Single/R/inheritance-single-2.r
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
setClass("Animal", representation(), prototype())
|
||||
setClass("Dog", representation(), prototype(), contains="Animal")
|
||||
setClass("Cat", representation(), prototype(), contains="Animal")
|
||||
setClass("Collie", representation(), prototype(), contains="Dog")
|
||||
setClass("Lab", representation(), prototype(), contains="Dog")
|
||||
13
Task/Inheritance-Single/Racket/inheritance-single.rkt
Normal file
13
Task/Inheritance-Single/Racket/inheritance-single.rkt
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#lang racket
|
||||
|
||||
(define animal% (class object% (super-new)))
|
||||
(define dog% (class animal% (super-new)))
|
||||
(define cat% (class animal% (super-new)))
|
||||
(define lab% (class dog% (super-new)))
|
||||
(define collie% (class dog% (super-new)))
|
||||
|
||||
;; unit tests
|
||||
(require rackunit)
|
||||
|
||||
(check-true (is-a? (new dog%) animal%))
|
||||
(check-false (is-a? (new collie%) cat%))
|
||||
22
Task/Inheritance-Single/Ruby/inheritance-single.rb
Normal file
22
Task/Inheritance-Single/Ruby/inheritance-single.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
class Animal
|
||||
#functions go here...
|
||||
def self.inherited(subclass)
|
||||
puts "new subclass of #{self}: #{subclass}"
|
||||
end
|
||||
end
|
||||
|
||||
class Dog < Animal
|
||||
#functions go here...
|
||||
end
|
||||
|
||||
class Cat < Animal
|
||||
#functions go here...
|
||||
end
|
||||
|
||||
class Lab < Dog
|
||||
#functions go here...
|
||||
end
|
||||
|
||||
class Collie < Dog
|
||||
#functions go here...
|
||||
end
|
||||
5
Task/Inheritance-Single/Scala/inheritance-single.scala
Normal file
5
Task/Inheritance-Single/Scala/inheritance-single.scala
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class Animal
|
||||
class Dog extends Animal
|
||||
class Cat extends Animal
|
||||
class Lab extends Dog
|
||||
class Collie extends Dog
|
||||
22
Task/Inheritance-Single/Smalltalk/inheritance-single.st
Normal file
22
Task/Inheritance-Single/Smalltalk/inheritance-single.st
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
Object subclass: #Animal
|
||||
instanceVariableNames: ' ' "* space separated list of names *"
|
||||
classVariableNames: ' '
|
||||
poolDictionaries: ' '
|
||||
category: ' ' !
|
||||
|
||||
"* declare methods here, separated with '!' *"
|
||||
"* !Animal methodsFor: 'a category'! *"
|
||||
"* methodName *"
|
||||
"* method body! !
|
||||
|
||||
!Animal subclass: #Dog
|
||||
"* etc. *" !
|
||||
|
||||
!Animal subclass: #Cat
|
||||
"* etc. *" !
|
||||
|
||||
!Dog subclass: #Lab
|
||||
"* etc. *" !
|
||||
|
||||
!Dog subclass: #Collie
|
||||
"* etc. *" !
|
||||
20
Task/Inheritance-Single/Tcl/inheritance-single.tcl
Normal file
20
Task/Inheritance-Single/Tcl/inheritance-single.tcl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package require TclOO
|
||||
oo::class create Animal {
|
||||
# ...
|
||||
}
|
||||
oo::class create Dog {
|
||||
superclass Animal
|
||||
# ...
|
||||
}
|
||||
oo::class create Cat {
|
||||
superclass Animal
|
||||
# ...
|
||||
}
|
||||
oo::class create Collie {
|
||||
superclass Dog
|
||||
# ...
|
||||
}
|
||||
oo::class create Lab {
|
||||
superclass Dog
|
||||
# ...
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue