Inheritance/Single: Difference between revisions

m
m (→‎{{header|J}}: 5*s->1)
 
(12 intermediate revisions by the same user not shown)
Line 978:
 
=={{header|Java}}==
<syntaxhighlight lang="java">public class Animal{
public class Animal{
//functions go here...
}
}</syntaxhighlight>
<syntaxhighlight lang="java">public class Dog extends Animal{
//functions go here...
}
}</syntaxhighlight>
<syntaxhighlight lang="java">public class Cat extends Animal{
//functions go here...
}
}</syntaxhighlight>
<syntaxhighlight lang="java">public class Lab extends Dog{
//functions go here...
}
}</syntaxhighlight>
<syntaxhighlight lang="java">public class Collie extends Dog{
//functions go here...
}
}</syntaxhighlight>
}</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() {
function Animal() {
// ...
}
}</syntaxhighlight>
 
<syntaxhighlight lang="javascript">function Dog() {
// ...
}
Dog.prototype = new Animal();</syntaxhighlight>
 
<syntaxhighlight lang="javascript">function Cat() {
// ...
}
Cat.prototype = new Animal();</syntaxhighlight>
 
<syntaxhighlight lang="javascript">function Collie() {
// ...
}
Collie.prototype = new Dog();</syntaxhighlight>
 
<syntaxhighlight lang="javascript">function Lab() {
// ...
}
Lab.prototype = new Dog();</syntaxhighlight>
 
<syntaxhighlight lang="javascript">Animal.prototype.speak = function() {print("an animal makes a sound")};
 
var lab = new Lab();
lab.speak(); // shows "an animal makes a sound"</syntaxhighlight>
}</syntaxhighlight>
 
=={{header|Julia}}==
Line 1,152 ⟶ 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"
-- parent script "Animal"
-- ...</syntaxhighlight>
-- ...
 
<syntaxhighlight lang="lingo">-- parent script "Dog"
property ancestor
 
Line 1,161 ⟶ 1,166:
me.ancestor = script("Animal").new()
return me
end
end</syntaxhighlight>
<syntaxhighlight lang="lingo">-- parent script "Cat"
property ancestor
 
Line 1,169 ⟶ 1,174:
me.ancestor = script("Animal").new()
return me
end
end</syntaxhighlight>
 
<syntaxhighlight lang="lingo">-- parent script "Lab"
property ancestor
 
Line 1,177 ⟶ 1,182:
me.ancestor = script("Dog").new()
return me
end
end</syntaxhighlight>
 
<syntaxhighlight lang="lingo">-- parent script "Collie"
property ancestor
 
Line 1,185 ⟶ 1,190:
me.ancestor = script("Dog").new()
return me
end
end</syntaxhighlight>
}</syntaxhighlight>
 
=={{header|Lisaac}}==
<syntaxhighlight lang="lisaac">Section Header
Section Header
+ name := ANIMAL;
// ...</syntaxhighlight>
<syntaxhighlight lang="lisaac">Section Header
+ name := CAT;
Section Inherit
- parent : ANIMAL := ANIMAL;
// ...</syntaxhighlight>
<syntaxhighlight lang="lisaac">Section Header
+ name := DOG;
Section Inherit
- parent : ANIMAL := ANIMAL;
// ...</syntaxhighlight>
<syntaxhighlight lang="lisaac">Section Header
+ name := LAB;
Section Inherit
- parent : DOG := DOG;
// ...</syntaxhighlight>
<syntaxhighlight lang="lisaac">Section Header
+ name := COLLIE;
Section Inherit
- parent : DOG := DOG;
// ...</syntaxhighlight>
}</syntaxhighlight>
 
=={{header|Logtalk}}==
Line 1,517 ⟶ 1,525:
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">class animal =
class animal =
object (self)
(*functions go here...*)
end</syntaxhighlight>
<syntaxhighlight lang="ocaml">class dog =
object (self)
inherit animal
(*functions go here...*)
end</syntaxhighlight>
<syntaxhighlight lang="ocaml">class cat =
object (self)
inherit animal
(*functions go here...*)
end</syntaxhighlight>
<syntaxhighlight lang="ocaml">class lab =
object (self)
inherit dog
(*functions go here...*)
end</syntaxhighlight>
<syntaxhighlight lang="ocaml">class collie =
object (self)
inherit dog
(*functions go here...*)
end</syntaxhighlight>
}</syntaxhighlight>
 
=={{header|Odin}}==
Line 1,667 ⟶ 1,677:
=={{header|Perl}}==
 
<syntaxhighlight lang="perl">package Animal;
package Animal;
#functions go here...
1;
1;</syntaxhighlight>
 
<syntaxhighlight lang="perl">package Dog;
use Animal;
@ISA = qw( Animal );
#functions go here...
1;
1;</syntaxhighlight>
 
<syntaxhighlight lang="perl">package Cat;
use Animal;
@ISA = qw( Animal );
#functions go here...
1;
1;</syntaxhighlight>
 
<syntaxhighlight lang="perl">package Lab;
use Dog;
@ISA = qw( Dog );
#functions go here...
1;
1;</syntaxhighlight>
 
<syntaxhighlight lang="perl">package Collie;
use Dog;
@ISA = qw( Dog );
#functions go here...
1;
1;</syntaxhighlight>
 
# The same using the [http://search.cpan.org/perldoc?MooseX::Declare MooseX::Declare] module:
 
<syntaxhighlight lang="perl">use MooseX::Declare;
 
class Animal {
Line 1,713 ⟶ 1,724:
class Collie extends Dog {
# methods go here...
}
}</syntaxhighlight>
}</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">(notonline)-->
end</syntaxhighlight lang="phix">
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (class)</span>
without js -- (class)
<span style="color: #008080;">class</span> <span style="color: #000000;">Animal</span>
class Animal
<span style="color: #008080;">private</span> <span style="color: #004080;">string</span> <span style="color: #000000;">species</span>
private string species
<span style="color: #008080;">end</span> <span style="color: #008080;">class</span>
end class
<span style="color: #008080;">class</span> <span style="color: #000000;">Dog</span> <span style="color: #008080;">extends</span> <span style="color: #000000;">Animal</span>
class Dog extends Animal
<span style="color: #008080;">public</span> <span style="color: #008080;">procedure</span> <span style="color: #000000;">bark</span><span style="color: #0000FF;">()</span>
public procedure bark()
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"woof\n"</span><span style="color: #0000FF;">)</span>
puts(1,"woof\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">class</span>
end class
<span style="color: #008080;">class</span> <span style="color: #000000;">Lab</span> <span style="color: #008080;">extends</span> <span style="color: #000000;">Dog</span> <span style="color: #008080;">end</span> <span style="color: #008080;">class</span>
class Lab extends Dog end class
<span style="color: #008080;">class</span> <span style="color: #000000;">Collie</span> <span style="color: #008080;">extends</span> <span style="color: #000000;">Dog</span> <span style="color: #008080;">end</span> <span style="color: #008080;">class</span>
class Collie extends Dog end class
<span style="color: #008080;">class</span> <span style="color: #000000;">Cat</span> <span style="color: #008080;">extends</span> <span style="color: #000000;">Animal</span> <span style="color: #008080;">end</span> <span style="color: #008080;">class</span>
class Cat extends Animal end class
<!--</syntaxhighlight>-->
-- ...</syntaxhighlight>
 
=={{header|PHP}}==
Line 2,078 ⟶ 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 = ()</syntaxhighlight>
animal = ()
<syntaxhighlight lang="self">dog = (| parent* = animal |)</syntaxhighlight>
<syntaxhighlight lang="self">catdog = (| parent* = animal |)</syntaxhighlight>
<syntaxhighlight lang="self">labcat = (| parent* = doganimal |)</syntaxhighlight>
<syntaxhighlight lang="self">collielab = (| parent* = dog |)</syntaxhighlight>
collie = (| parent* = dog |)
end</syntaxhighlight>
 
=={{header|Sidef}}==
7,794

edits