This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

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

View file

@ -0,0 +1,6 @@
---
category:
- Object oriented
- Type System
- Encyclopedia
note: Basic language learning

View file

@ -0,0 +1,3 @@
public class Animal {
// ...
}

View file

@ -0,0 +1,3 @@
public class Cat extends Animal {
// ...
}

View file

@ -0,0 +1,3 @@
public class Dog extends Animal {
// ...
}

View file

@ -0,0 +1,3 @@
public class Lab extends Dog {
// ...
}

View file

@ -0,0 +1,3 @@
public class Collie extends Dog {
// ...
}

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

View file

@ -0,0 +1,3 @@
class Animal{
//functions go here...
}

View file

@ -0,0 +1,3 @@
class Dog extends Animal {
//functions go here...
}

View file

@ -0,0 +1,3 @@
class Cat extends Animal {
//functions go here...
}

View file

@ -0,0 +1,3 @@
class Lab extends Dog {
//functions go here...
}

View file

@ -0,0 +1,3 @@
class Collie extends Dog {
//functions go here...
}

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

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

View file

@ -0,0 +1,24 @@
class Animal
{
// ...
};
class Dog: public Animal
{
// ...
};
class Lab: public Dog
{
// ...
};
class Collie: public Dog
{
// ...
};
class Cat: public Animal
{
// ...
};

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

View file

@ -0,0 +1,4 @@
(derive ::dog ::animal)
(derive ::cat ::animal)
(derive ::lab ::dog)
(derive ::collie ::dog)

View file

@ -0,0 +1,6 @@
user> (isa? ::dog ::animal)
true
user> (isa? ::dog ::cat)
false
user> (isa? ::collie ::animal)
true

View file

@ -0,0 +1,5 @@
(defclass animal () ())
(defclass dog (animal) ())
(defclass lab (dog) ())
(defclass collie (dog) ())
(defclass cat (animal) ())

View file

@ -0,0 +1,5 @@
(defstruct animal)
(defstruct (dog (:include animal)))
(defstruct (lab (:include dog)))
(defstruct (collie (:include dog)))
(defstruct (cat (:include animal)))

View 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))
#| ... #|
)

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

View file

@ -0,0 +1,21 @@
class Animal {
// ...
}
class Dog: Animal {
// ...
}
class Lab: Dog {
// ...
}
class Collie: Dog {
// ...
}
class Cat: Animal {
// ...
}
void main() {}

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

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

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

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

View file

@ -0,0 +1,3 @@
def fido implements LabStamp {}
def tom implements CatStamp {}
def brick {} # not an animal

View 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

View file

@ -0,0 +1,3 @@
class
ANIMAL
end

View file

@ -0,0 +1,5 @@
class
DOG
inherit
ANIMAL
end

View file

@ -0,0 +1,5 @@
class
CAT
inherit
ANIMAL
end

View file

@ -0,0 +1,5 @@
class
LAB
inherit
DOG
end

View file

@ -0,0 +1,5 @@
class
COLLIE
inherit
DOG
end

View file

@ -0,0 +1,5 @@
TUPLE: animal ;
TUPLE: dog < animal ;
TUPLE: cat < animal ;
TUPLE: lab < dog ;
TUPLE: collie < dog ;

View file

@ -0,0 +1,19 @@
class Animal {
# ...
}
class Dog : Animal {
# ...
}
class Cat : Animal {
# ...
}
class Lab : Dog {
# ...
}
class Collie : Dog {
# ...
}

View file

@ -0,0 +1,19 @@
class Animal
{
}
class Dog : Animal
{
}
class Cat : Animal
{
}
class Lab : Dog
{
}
class Collie : Dog
{
}

View 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 {} ;

View 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

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

View file

@ -0,0 +1,3 @@
class Animal{
//contents go here...
}

View file

@ -0,0 +1,3 @@
class Dog extends Animal{
//contents go here...
}

View file

@ -0,0 +1,3 @@
class Cat extends Animal{
//contents go here...
}

View file

@ -0,0 +1,3 @@
class Lab extends Dog{
//contents go here...
}

View file

@ -0,0 +1,3 @@
class Collie extends Dog{
//contents go here...
}

View 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

View file

@ -0,0 +1,3 @@
class Animal {
// ...
}

View file

@ -0,0 +1,3 @@
class Cat extends Animal {
// ...
}

View file

@ -0,0 +1,3 @@
class Dog extends Animal {
// ...
}

View file

@ -0,0 +1,3 @@
class Lab extends Dog {
// ...
}

View file

@ -0,0 +1,3 @@
class Collie extends Dog {
// ...
}

View file

@ -0,0 +1,14 @@
class Animal ()
end
class Dog : Animal ()
end
class Cat : Animal ()
end
class Lab : Dog ()
end
class Collie : Dog ()
end

View 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.

View file

@ -0,0 +1,5 @@
Animal := Object clone
Cat := Animal clone
Dog := Animal clone
Collie := Dog clone
Lab := Dog clone

View file

@ -0,0 +1 @@
coclass 'Animal'

View file

@ -0,0 +1,2 @@
coclass 'Dog'
coinsert 'Animal'

View file

@ -0,0 +1,2 @@
coclass 'Cat'
coinsert 'Animal'

View file

@ -0,0 +1,2 @@
coclass 'Lab'
coinsert 'Dog'

View file

@ -0,0 +1,2 @@
coclass 'Collie'
coinsert 'Dog'

View file

@ -0,0 +1,4 @@
coinsert_Dog_ 'Animal'
coinsert_Cat_ 'Animal'
coinsert_Lab_ 'Dog'
coinsert_Collie_ 'Dog'

View file

@ -0,0 +1,3 @@
public class Animal{
//functions go here...
}

View file

@ -0,0 +1,3 @@
public class Dog extends Animal{
//functions go here...
}

View file

@ -0,0 +1,3 @@
public class Cat extends Animal{
//functions go here...
}

View file

@ -0,0 +1,3 @@
public class Lab extends Dog{
//functions go here...
}

View file

@ -0,0 +1,3 @@
public class Collie extends Dog{
//functions go here...
}

View file

@ -0,0 +1,3 @@
function Animal() {
// ...
}

View file

@ -0,0 +1,4 @@
function Dog() {
// ...
}
Dog.prototype = new Animal();

View file

@ -0,0 +1,4 @@
function Cat() {
// ...
}
Cat.prototype = new Animal();

View file

@ -0,0 +1,4 @@
function Collie() {
// ...
}
Collie.prototype = new Dog();

View file

@ -0,0 +1,4 @@
function Lab() {
// ...
}
Lab.prototype = new Dog();

View file

@ -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"

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

View file

@ -0,0 +1,3 @@
Section Header
+ name := ANIMAL;
// ...

View file

@ -0,0 +1,5 @@
Section Header
+ name := CAT;
Section Inherit
- parent : ANIMAL := ANIMAL;
// ...

View file

@ -0,0 +1,5 @@
Section Header
+ name := DOG;
Section Inherit
- parent : ANIMAL := ANIMAL;
// ...

View file

@ -0,0 +1,5 @@
Section Header
+ name := LAB;
Section Inherit
- parent : DOG := DOG;
// ...

View file

@ -0,0 +1,5 @@
Section Header
+ name := COLLIE;
Section Inherit
- parent : DOG := DOG;
// ...

View 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.

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

View file

@ -0,0 +1,3 @@
package Animal;
#functions go here...
1;

View file

@ -0,0 +1,5 @@
package Dog;
use Animal;
@ISA = qw( Animal );
#functions go here...
1;

View file

@ -0,0 +1,5 @@
package Cat;
use Animal;
@ISA = qw( Animal );
#functions go here...
1;

View file

@ -0,0 +1,5 @@
package Lab;
use Dog;
@ISA = qw( Dog );
#functions go here...
1;

View file

@ -0,0 +1,5 @@
package Collie;
use Dog;
@ISA = qw( Dog );
#functions go here...
1;

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

View file

@ -0,0 +1,9 @@
(class +Animal)
(class +Dog +Animal)
(class +Cat +Animal)
(class +Lab +Dog)
(class +Collie +Dog)

View file

@ -0,0 +1,6 @@
: (dep '+Animal)
+Animal
+Cat
+Dog
+Collie
+Lab

View 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...

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

View file

@ -0,0 +1,2 @@
aCollie <- "woof"
class(aCollie) <- c("Collie", "Dog", "Animal")

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

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

View 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

View file

@ -0,0 +1,5 @@
class Animal
class Dog extends Animal
class Cat extends Animal
class Lab extends Dog
class Collie extends Dog

View 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. *" !

View 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
# ...
}