Inheritance/Single: Difference between revisions
m
→{{header|Phix}}: oops
m (→{{header|ChucK}}: 5*s->1) |
m (→{{header|Phix}}: oops) |
||
(16 intermediate revisions by the same user not shown) | |||
Line 610:
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel ">
class
ANIMAL
end
class
DOG
inherit
ANIMAL
end
class
CAT
inherit
ANIMAL
end
class
LAB
inherit
DOG
end
class
COLLIE
inherit
DOG
end
</syntaxhighlight>
=={{header|Elena}}==
Line 862 ⟶ 864:
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">
class Animal{
//contents go here...
}
//contents go here...
}
//contents go here...
}
//contents go here...
}
//contents go here...
}
</syntaxhighlight>
=={{header|Haskell}}==
Line 888 ⟶ 892:
=={{header|Haxe}}==
<syntaxhighlight lang="haxe">
class Animal {
// ...
}
// ...
}
// ...
}
// ...
}
// ...
}
</syntaxhighlight>
== Icon and {{header|Unicon}} ==
Line 946 ⟶ 952:
Here is how this would normally be done:
<syntaxhighlight lang="j"
coclass 'Dog'
coinsert 'Animal'
coclass 'Cat'
coinsert 'Animal'
coclass 'Lab'
coinsert 'Dog'
coclass 'Collie'
coinsert 'Dog'
</syntaxhighlight>
<code>coclass</code> specifies that following definitions will be within the named class, and <code>coinsert</code> specifies that the current class will inherit from the named classes (or object -- in J the only difference between a class and an object is its name and how you can create them -- this motivates the "co" prefix on operations which manipulate '''c'''lasses and '''o'''bjects).
Line 970 ⟶ 978:
=={{header|Java}}==
<syntaxhighlight lang="java">
public class Animal{
//functions go here...
}
//functions go here...
}
//functions go here...
}
//functions go here...
}
//functions go here...
}
</syntaxhighlight>
=={{header|JavaScript}}==
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance.
<syntaxhighlight lang="javascript">
function Animal() {
// ...
}
// ...
}
Dog.prototype = new Animal();
// ...
}
Cat.prototype = new Animal();
// ...
}
Collie.prototype = new Dog();
// ...
}
Lab.prototype = new Dog();
var lab = new Lab();
lab.speak(); // shows "an animal makes a sound"
</syntaxhighlight>
=={{header|Julia}}==
Line 1,144 ⟶ 1,156:
=={{header|Lingo}}==
In Lingo Classes are represented by "parent scripts". Instead of using new() as in the code below, child classes can also use rawNew() when creating an instance of their parent classes. rawNew() creates an instance of a class without calling its initialization function 'new' (constructor).
<syntaxhighlight lang="lingo">
-- parent script "Animal"
-- ...
property ancestor
Line 1,153 ⟶ 1,166:
me.ancestor = script("Animal").new()
return me
end
property ancestor
Line 1,161 ⟶ 1,174:
me.ancestor = script("Animal").new()
return me
end
property ancestor
Line 1,169 ⟶ 1,182:
me.ancestor = script("Dog").new()
return me
end
property ancestor
Line 1,177 ⟶ 1,190:
me.ancestor = script("Dog").new()
return me
end
</syntaxhighlight>
=={{header|Lisaac}}==
<syntaxhighlight lang="lisaac">
Section Header
+ name := ANIMAL;
// ...
+ name := CAT;
Section Inherit
- parent : ANIMAL := ANIMAL;
// ...
+ name := DOG;
Section Inherit
- parent : ANIMAL := ANIMAL;
// ...
+ name := LAB;
Section Inherit
- parent : DOG := DOG;
// ...
+ name := COLLIE;
Section Inherit
- parent : DOG := DOG;
// ...
</syntaxhighlight>
=={{header|Logtalk}}==
Line 1,509 ⟶ 1,525:
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">
class animal =
object (self)
(*functions go here...*)
end
object (self)
inherit animal
(*functions go here...*)
end
object (self)
inherit animal
(*functions go here...*)
end
object (self)
inherit dog
(*functions go here...*)
end
object (self)
inherit dog
(*functions go here...*)
end
</syntaxhighlight>
=={{header|Odin}}==
Line 1,659 ⟶ 1,677:
=={{header|Perl}}==
<syntaxhighlight lang="perl">
package Animal;
#functions go here...
1;
use Animal;
@ISA = qw( Animal );
#functions go here...
1;
use Animal;
@ISA = qw( Animal );
#functions go here...
1;
use Dog;
@ISA = qw( Dog );
#functions go here...
1;
use Dog;
@ISA = qw( Dog );
#functions go here...
1;
# The same using the [http://search.cpan.org/perldoc?MooseX::Declare MooseX::Declare] module:
class Animal {
Line 1,705 ⟶ 1,724:
class Collie extends Dog {
# methods go here...
}
</syntaxhighlight>
=={{header|Phix}}==
{{libheader|Phix/Class}}
Add (private|public) fields and methods as needed. Make Animal and Dog abstract (ie use "abstract class") to prevent instantiation.
<!--
<syntaxhighlight lang="phix">
without js -- (class)
class Animal
private string species
end class
class Dog extends Animal
public procedure bark()
puts(1,"woof\n")
end procedure
end class
class Lab extends Dog end class
class Collie extends Dog end class
class Cat extends Animal end class
</syntaxhighlight>
=={{header|PHP}}==
Line 2,070 ⟶ 2,091:
=={{header|Self}}==
Self is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. This is an example of the relevant excerpts from a Self transporter fileout. Normally the object tree would be built and navigated within the graphical Self environment.
<syntaxhighlight lang="self"
animal = ()
collie = (| parent* = dog |)
</syntaxhighlight>
=={{header|Sidef}}==
|