Inheritance/Single: Difference between revisions
m
→{{header|Phix}}: oops
(Added Odin variant) |
m (→{{header|Phix}}: oops) |
||
(23 intermediate revisions by 5 users not shown) | |||
Line 84:
=={{header|ActionScript}}==
<syntaxhighlight lang="actionscript">
public class Animal {
// ...
}
// ...
}
// ...
}
// ...
}
// ...
}</syntaxhighlight>
Line 116 ⟶ 117:
=={{header|Aikido}}==
<syntaxhighlight lang="aikido ">
class Animal{
//functions go here...
}
//functions go here...
}
//functions go here...
}
//functions go here...
}
//functions go here...
}
</syntaxhighlight>
=={{header|AmigaE}}==
Line 274 ⟶ 277:
=={{header|ChucK}}==
<syntaxhighlight lang="chuck">
public class Drums{
//functions go here...
}
//functions go here...
}
//functions go here...
}
//functions go here...
}
//functions go here...
}
</syntaxhighlight>
=={{header|Clojure}}==
Line 317 ⟶ 322:
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
CLASS-ID. Animal.
*> ...
END CLASS Animal.
CLASS-ID. Dog
INHERITS FROM Animal.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
Line 330 ⟶ 338:
END CLASS Dog.
CLASS-ID. Cat
INHERITS FROM Animal.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
Line 339 ⟶ 349:
END CLASS Cat.
CLASS-ID. Lab
INHERITS FROM Dog.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
Line 348 ⟶ 360:
END CLASS Lab.
CLASS-ID. Collie
INHERITS FROM Dog.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
Line 596 ⟶ 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 646 ⟶ 662:
// ...
}</syntaxhighlight>
=={{header|EMal}}==
<syntaxhighlight lang="emal">
in Org:RosettaCode
type Animal
model do end
type Dog extends Animal
model do end
type Cat extends Animal
model do end
type Lab extends Dog
model do end
type Collie extends Dog
model do end
type Main
var fuffy = Collie()
for each generic kind in generic[Animal, Dog, Cat, Lab, Collie]
writeLine("Fuffy " + when(Generic.check(kind, fuffy), "is", "is not") + " a " + Generic.name(kind))
end
</syntaxhighlight>
{{out}}
<pre>
Fuffy is a Org:RosettaCode:Animal
Fuffy is a Org:RosettaCode:Dog
Fuffy is not a Org:RosettaCode:Cat
Fuffy is not a Org:RosettaCode:Lab
Fuffy is a Org:RosettaCode:Collie
</pre>
=={{header|F_Sharp|F#}}==
Line 820 ⟶ 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 846 ⟶ 892:
=={{header|Haxe}}==
<syntaxhighlight lang="haxe">
class Animal {
// ...
}
// ...
}
// ...
}
// ...
}
// ...
}
</syntaxhighlight>
== Icon and {{header|Unicon}} ==
Line 904 ⟶ 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 928 ⟶ 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,102 ⟶ 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,111 ⟶ 1,166:
me.ancestor = script("Animal").new()
return me
end
property ancestor
Line 1,119 ⟶ 1,174:
me.ancestor = script("Animal").new()
return me
end
property ancestor
Line 1,127 ⟶ 1,182:
me.ancestor = script("Dog").new()
return me
end
property ancestor
Line 1,135 ⟶ 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,467 ⟶ 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,617 ⟶ 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,663 ⟶ 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 1,849 ⟶ 1,912:
buddy = Labrador()
buddy.kill()
print
</syntaxhighlight>
{{out}}
<pre>
Line 2,027 ⟶ 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}}==
Line 2,229 ⟶ 2,295:
=={{header|Wren}}==
<syntaxhighlight lang="
// methods
}
|