Classes: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 442:
MyClass_someMethod(obj);
MyClass_delete(&obj);</lang>
 
=={{header|C sharp|C#}}==
<lang csharp>public class MyClass
{
public MyClass()
{
}
public void SomeMethod()
{
}
private int _variable;
public int Variable
{
get { return _variable; }
set { _variable = value; }
}
public static void Main()
{
// instantiate it
MyClass instance = new MyClass();
// invoke the method
instance.SomeMethod();
// set the variable
instance.Variable = 99;
// get the variable
System.Console.WriteLine( "Variable=" + instance.Variable.ToString() );
}
}</lang>
 
=={{header|C++}}==
Line 496 ⟶ 524:
virtual ~MyClass(); // destructor
};</lang>
 
=={{header|C sharp|C#}}==
<lang csharp>public class MyClass
{
public MyClass()
{
}
public void SomeMethod()
{
}
private int _variable;
public int Variable
{
get { return _variable; }
set { _variable = value; }
}
public static void Main()
{
// instantiate it
MyClass instance = new MyClass();
// invoke the method
instance.SomeMethod();
// set the variable
instance.Variable = 99;
// get the variable
System.Console.WriteLine( "Variable=" + instance.Variable.ToString() );
}
}</lang>
 
=={{header|Clojure}}==
Line 747:
my_class = MyClass.new
</lang>
 
=={{header|D}}==
<lang d>import std.stdio;
Line 873 ⟶ 874:
</lang>
 
This is enough to declare a
 
=={{header|Dragon}}==
Line 930 ⟶ 931:
? red.colorize("apple")
# value: "red apple"</lang>
 
=={{header|EchoLisp}}==
<lang lisp>
(lib 'gloops) ; load oo library
 
(define-class Person null (name (age :initform 66)))
(define-method tostring (Person) (lambda (p) ( format "🚶 %a " p.name)))
(define-method mailto (Person Person) (lambda( p o) (printf "From %a to️ %a : ..." p o)))
 
;; define a sub-class of Person with same methods
(define-class Writer (Person) (books))
(define-method tostring (Writer) (lambda (w)( format "🎩 %a" w.name)))
(define-method mailto (Person Writer)
(lambda (p w) (printf " From %a (age %d). Dear writer of %a ..." p p.age w.books )))
 
</lang>
{{Output}}
<lang lisp>
;; instantiate
(define simone (make-instance Person :name 'simone :age 42)) ;; slots values by name
(define antoinette (make-instance Person :name 'antoinette :age 37))
(define albert (Person "albert" 33)) ;; quick way : slots values in order
(define simon (make-instance Writer :name "simon" :books '(my-life my-bike)))
 
 
(mailto simone simon) ;; method Person-Writer
(mailto simone antoinette) ;; method Person-Person
(mailto simon albert) ;; no method Writer-Person : call 'super' Person-Person
(mailto simon simon) ;; no mehod Writer-Writer : call 'super' Person-Writer
From 🚶 simone (age 42). Dear writer of (my-life my-bike) ...
From 🚶 simone to️ 🚶 antoinette : ...
From 🎩 simon to️ 🚶 albert : ...
From 🎩 simon (age 66). Dear writer of (my-life my-bike) ...
</lang>
 
=={{header|Eiffel}}==
Line 1,055 ⟶ 1,091:
 
end
</lang>
 
=={{header|EchoLisp}}==
<lang lisp>
(lib 'gloops) ; load oo library
 
(define-class Person null (name (age :initform 66)))
(define-method tostring (Person) (lambda (p) ( format "🚶 %a " p.name)))
(define-method mailto (Person Person) (lambda( p o) (printf "From %a to️ %a : ..." p o)))
 
;; define a sub-class of Person with same methods
(define-class Writer (Person) (books))
(define-method tostring (Writer) (lambda (w)( format "🎩 %a" w.name)))
(define-method mailto (Person Writer)
(lambda (p w) (printf " From %a (age %d). Dear writer of %a ..." p p.age w.books )))
 
</lang>
{{Output}}
<lang lisp>
;; instantiate
(define simone (make-instance Person :name 'simone :age 42)) ;; slots values by name
(define antoinette (make-instance Person :name 'antoinette :age 37))
(define albert (Person "albert" 33)) ;; quick way : slots values in order
(define simon (make-instance Writer :name "simon" :books '(my-life my-bike)))
 
 
(mailto simone simon) ;; method Person-Writer
(mailto simone antoinette) ;; method Person-Person
(mailto simon albert) ;; no method Writer-Person : call 'super' Person-Person
(mailto simon simon) ;; no mehod Writer-Writer : call 'super' Person-Writer
From 🚶 simone (age 42). Dear writer of (my-life my-bike) ...
From 🚶 simone to️ 🚶 antoinette : ...
From 🎩 simon to️ 🚶 albert : ...
From 🎩 simon (age 66). Dear writer of (my-life my-bike) ...
</lang>
 
Line 1,187 ⟶ 1,188:
member x.Perimeter() = 2.0 * width + 2.0 * height
member x.Area() = width * height</lang>
 
=={{header|Factor}}==
<lang factor>TUPLE: my-class foo bar baz ;
M: my-class quux foo>> 20 + ;
C: <my-class> my-class
10 20 30 <my-class> quux ! result: 30
TUPLE: my-child-class < my-class quxx ;
C: <my-child-class> my-child-class
M: my-child-class foobar 20 >>quux ;
20 20 30 <my-child-class> foobar quux ! result: 30</lang>
 
=={{header|Falcon}}==
Line 1,216 ⟶ 1,227:
<lang falcon>m = mailbox( 10 )
// Ouputs: Box now ready for 100 messages.</lang>
 
=={{header|Factor}}==
<lang factor>TUPLE: my-class foo bar baz ;
M: my-class quux foo>> 20 + ;
C: <my-class> my-class
10 20 30 <my-class> quux ! result: 30
TUPLE: my-child-class < my-class quxx ;
C: <my-child-class> my-child-class
M: my-child-class foobar 20 >>quux ;
20 20 30 <my-child-class> foobar quux ! result: 30</lang>
 
=={{header|Fancy}}==
Line 2,352 ⟶ 2,353:
A(3).bb
</lang>
 
 
=={{header|MATLAB}}==
Line 2,449:
 
3.141592653589793</lang>
 
 
=={{header|MiniScript}}==
Line 2,537 ⟶ 2,536:
mies = 3
say s mies</lang>
 
 
=={{header|Nim}}==
Line 2,940 ⟶ 2,938:
$instance->some_method; # invoke method on object instance
# instance deallocates when the last reference falls out of scope</lang>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2015.12}}
<lang perl6>class Camel { has Int $.humps = 1; }
 
my Camel $a .= new;
say $a.humps; # Automatically generated accessor method.
 
my Camel $b .= new: humps => 2;
say $b.humps;</lang>
 
A more complex example:
 
<lang perl6>class Butterfly {
has Int $!age; # With the ! twigil, no public accessor method is generated
has Str $.name;
has Str $.color;
has Bool $.wings;
 
submethod BUILD(:$!name = 'Camelia', :$!age = 2, :$!color = 'pink') {
# BUILD is called by bless. Its primary use is to to control
# object initialization.
$!wings = $!age > 1;
}
 
method flap() {
say ($.wings
?? 'Watch out for that hurricane!'
!! 'No wings to flap.');
}
}
 
my Butterfly $a .= new: age => 5;
say "Name: {$a.name}, Color: {$a.color}";
$a.flap;
 
my Butterfly $b .= new(name => 'Osgood', age => 4);
say "Name: {$b.name}, Color: {$b.color}";
$b.flap;</lang>
 
=={{header|Phix}}==
Line 3,435 ⟶ 3,394:
(new fish% [size 50])
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.12}}
<lang perl6>class Camel { has Int $.humps = 1; }
 
my Camel $a .= new;
say $a.humps; # Automatically generated accessor method.
 
my Camel $b .= new: humps => 2;
say $b.humps;</lang>
 
A more complex example:
 
<lang perl6>class Butterfly {
has Int $!age; # With the ! twigil, no public accessor method is generated
has Str $.name;
has Str $.color;
has Bool $.wings;
 
submethod BUILD(:$!name = 'Camelia', :$!age = 2, :$!color = 'pink') {
# BUILD is called by bless. Its primary use is to to control
# object initialization.
$!wings = $!age > 1;
}
 
method flap() {
say ($.wings
?? 'Watch out for that hurricane!'
!! 'No wings to flap.');
}
}
 
my Butterfly $a .= new: age => 5;
say "Name: {$a.name}, Color: {$a.color}";
$a.flap;
 
my Butterfly $b .= new(name => 'Osgood', age => 4);
say "Name: {$b.name}, Color: {$b.color}";
$b.flap;</lang>
 
=={{header|RapidQ}}==
Line 3,583 ⟶ 3,582:
z + nl # print the z attribute
</lang>
 
 
=={{header|Ruby}}==
Line 4,226 ⟶ 4,224:
<pre>(THE PROGRAMMING LANGUAGE LISP WAS CREATED IN 1958)</pre>
 
=={{header|zonnon}}==
<lang zonnon>
module Graphics;
type
{ref,public} (* class *)
Point = object(ord,abs: integer)
var
(* instance variables *)
{public,immutable} x,y: integer;
 
(* method *)
procedure {public} Ord():integer;
begin
return y
end Ord;
 
(* method *)
procedure {public} Abs():integer;
begin
return x
end Abs;
 
(* constructor *)
begin
self.x := ord;
self.y := abs;
end Point;
end Graphics.
 
module Main;
import Graphics;
var
p: Graphics.Point;
 
procedure Write(p: Graphics.Point);
begin
writeln('[':1,p.x:3,',':1,p.y:3,']':1)
end Write;
 
begin
p := new Graphics.Point(12,12);
Write(p);
writeln("Abs: ":4,p.Abs():3," Ord: ":5,p.Ord():3);
end Main.
</lang>
{{out}}
<pre>
[ 12, 12]
Abs: 12 Ord: 12
</pre>
=={{header|zkl}}==
<lang zkl>class C{ // define class named "C", no parents or attributes
Line 4,327 ⟶ 4,275:
{{omit from|Vim Script}}
{{omit from|ZX Spectrum Basic|not OO}}
 
=={{header|zonnon}}==
<lang zonnon>
module Graphics;
type
{ref,public} (* class *)
Point = object(ord,abs: integer)
var
(* instance variables *)
{public,immutable} x,y: integer;
 
(* method *)
procedure {public} Ord():integer;
begin
return y
end Ord;
 
(* method *)
procedure {public} Abs():integer;
begin
return x
end Abs;
 
(* constructor *)
begin
self.x := ord;
self.y := abs;
end Point;
end Graphics.
 
module Main;
import Graphics;
var
p: Graphics.Point;
 
procedure Write(p: Graphics.Point);
begin
writeln('[':1,p.x:3,',':1,p.y:3,']':1)
end Write;
 
begin
p := new Graphics.Point(12,12);
Write(p);
writeln("Abs: ":4,p.Abs():3," Ord: ":5,p.Ord():3);
end Main.
</lang>
{{out}}
<pre>
[ 12, 12]
Abs: 12 Ord: 12
</pre>
10,333

edits