Inheritance/Single: Difference between revisions
m
→{{header|Phix}}: oops
m (→{{header|J}}: 5*s->1) |
m (→{{header|Phix}}: oops) |
||
(12 intermediate revisions by the same user not shown) | |||
Line 978:
=={{header|Java}}==
<syntaxhighlight lang="java">
public class Animal{
//functions go here...
}
}</syntaxhighlight>▼
//functions go here...
}
}</syntaxhighlight>▼
//functions go here...
}
}</syntaxhighlight>▼
//functions go here...
}
}</syntaxhighlight>▼
//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() {
// ...
}
}</syntaxhighlight>▼
// ...
}
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"
=={{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"
-- ...</syntaxhighlight>▼
-- ...
property ancestor
Line 1,161 ⟶ 1,166:
me.ancestor = script("Animal").new()
return me
end
end</syntaxhighlight>▼
property ancestor
Line 1,169 ⟶ 1,174:
me.ancestor = script("Animal").new()
return me
end
end</syntaxhighlight>▼
property ancestor
Line 1,177 ⟶ 1,182:
me.ancestor = script("Dog").new()
return me
end
property ancestor
Line 1,185 ⟶ 1,190:
me.ancestor = script("Dog").new()
return me
end
=={{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;
// ...
=={{header|Logtalk}}==
Line 1,517 ⟶ 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
=={{header|Odin}}==
Line 1,667 ⟶ 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,713 ⟶ 1,724:
class Collie extends Dog {
# methods go here...
}
=={{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.
<!--
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
=={{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 = ()
collie = (| parent* = dog |)
=={{header|Sidef}}==
|