Classes: Difference between revisions
m (→{{header|J}}: fix lang tags) |
m (Fixed lang tags.) |
||
Line 19: | Line 19: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
<lang actionscript> |
<lang actionscript>package { |
||
package { |
|||
public class MyClass { |
public class MyClass { |
||
Line 40: | Line 39: | ||
} |
} |
||
} |
} |
||
}</lang> |
|||
} |
|||
</lang> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Line 47: | Line 45: | ||
A package specification has the following form: |
A package specification has the following form: |
||
<lang ada> |
<lang ada>package My_Package is |
||
type My_Type is tagged private; |
|||
procedure Some_Procedure(Item : out My_Type); |
|||
function Set(Value : in Integer) return My_Type; |
|||
private |
|||
type My_Type is tagged record |
|||
Variable : Integer := -12; |
|||
end record; |
|||
end My_Package;</lang> |
|||
The type declaration at the top of the package gives public visibility to the private tagged type My_Type. Since My_Type is declared to be private, the public has no visibility of its structure. The type must be treated as a black box. The private section of the package specification includes the actual tagged record definition. Note that the data member Variable is initialized to -12. This corresponds to a default constructor for the type. |
The type declaration at the top of the package gives public visibility to the private tagged type My_Type. Since My_Type is declared to be private, the public has no visibility of its structure. The type must be treated as a black box. The private section of the package specification includes the actual tagged record definition. Note that the data member Variable is initialized to -12. This corresponds to a default constructor for the type. |
||
Line 75: | Line 73: | ||
An instance is typically created outside the package: |
An instance is typically created outside the package: |
||
<lang ada> |
<lang ada>with My_Package; use My_Package; |
||
procedure Main is |
|||
Foo : My_Type; -- Foo is created and initialized to -12 |
|||
begin |
|||
Some_Procedure(Foo); -- Foo is doubled |
|||
Foo := Set(2007); -- Foo.Variable is set to 2007 |
|||
end Main;</lang> |
|||
=={{header|AmigaE}}== |
=={{header|AmigaE}}== |
||
Line 348: | Line 346: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<lang lisp>(defclass circle () |
|||
((radius :initarg :radius |
|||
:initform 1.0 |
|||
:type number |
|||
:reader radius))) |
|||
(defmethod area ((shape circle)) |
|||
(* pi (expt (radius shape) 2))) |
|||
> (defvar *c* (make-instance 'circle :radius 2)) |
|||
> (area *c*) |
|||
12.566370614359172d0</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
Line 419: | Line 417: | ||
In E, classes, constructors, and instance variables are not built into the language. This is an example of the basic convention; different cases may call for objects built in different ways. |
In E, classes, constructors, and instance variables are not built into the language. This is an example of the basic convention; different cases may call for objects built in different ways. |
||
<lang e>def makeColor(name :String) { |
|||
def color { |
|||
to colorize(thing :String) { |
|||
return `$name $thing` |
|||
} |
|||
} |
|||
return color |
|||
}</lang> |
|||
} |
|||
Example interactive session creating and using it: |
Example interactive session creating and using it: |
||
<lang e>? def red := makeColor("red") |
|||
# value: <color> |
|||
? red.colorize("apple") |
|||
# value: "red apple"</lang> |
|||
=={{header|Factor}}== |
=={{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|Forth}}== |
=={{header|Forth}}== |
||
Line 452: | Line 450: | ||
Declare a class |
Declare a class |
||
<lang forth>:class MyClass <super Object |
|||
int memvar |
|||
:m ClassInit: ( -- ) |
|||
ClassInit: super |
|||
1 to memvar ;m |
|||
:m ~: ( -- ) ." Final " show: [ Self ] ;m |
|||
:m set: ( n -- ) to memvar ;m |
|||
:m show: ( -- ) ." Memvar = " memvar . ;m |
|||
;class</lang> |
|||
Allocate a static object |
Allocate a static object |
||
<lang forth>MyClass newInstance</lang> |
|||
Allocate a dynamic object, saving its pointer in a global variable. |
Allocate a dynamic object, saving its pointer in a global variable. |
||
<lang forth>New> MyClass value newInstance</lang> |
|||
Call member functions |
Call member functions |
||
<lang forth>10 set: newInstance |
|||
show: newInstance</lang> |
|||
Free a dynamically allocated object |
Free a dynamically allocated object |
||
<lang forth>newInstance dispose |
|||
0 to newInstance \ no dangling pointers!</lang> |
|||
Example of dynamic allocation and local variable use" |
Example of dynamic allocation and local variable use" |
||
<lang forth>: test { \ obj -- } |
|||
New> MyClass to obj |
|||
show: obj |
|||
1000 set: obj |
|||
obj dispose ;</lang> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Line 543: | Line 541: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Class definition:''' |
'''Class definition:''' |
||
<lang j> |
<lang j>coclass 'exampleClass' |
||
coclass 'exampleClass' |
|||
exampleMethod=: monad define |
exampleMethod=: monad define |
||
Line 554: | Line 551: | ||
) |
) |
||
exampleInstanceVariable=: 0 |
exampleInstanceVariable=: 0</lang> |
||
</lang> |
|||
'''Instantiation:''' |
'''Instantiation:''' |
||
Line 585: | Line 581: | ||
{{works with|Firefox|2.0}} |
{{works with|Firefox|2.0}} |
||
<lang javascript> |
<lang javascript>//constructor |
||
function MyClass(initVal) { |
|||
//instance variable |
|||
if(initVal == undefined) { |
|||
this.number = 1; |
|||
} |
|||
else { |
|||
this.number = initVal; |
|||
} |
|||
} |
|||
//method of MyClass |
|||
MyClass.prototype.getDouble = function() { |
|||
return this.number * 2; |
|||
}; |
|||
var instance1 = new MyClass; //or "new MyClass();" |
|||
instance1.number = 5; |
|||
alert( instance1.getDouble() ); //10 |
|||
var instance2 = new MyClass(3); |
|||
alert( instance2.getDouble() ); //6</lang> |
|||
Replace the <code>alert</code> calls with <code>WScript.Echo</code>, and this code {{works with|JScript|5.7}} |
Replace the <code>alert</code> calls with <code>WScript.Echo</code>, and this code {{works with|JScript|5.7}} |
||
=={{header|Lisaac}}== |
=={{header|Lisaac}}== |
||
<lang Lisaac> |
<lang Lisaac>Section Header |
||
Section Header |
|||
+ name := SAMPLE; |
+ name := SAMPLE; |
||
Line 634: | Line 629: | ||
sample := SAMPLE.clone; |
sample := SAMPLE.clone; |
||
sample.some_method; |
sample.some_method; |
||
); |
);</lang> |
||
</lang> |
|||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
{{works with|OO2C|2.1.11}} |
{{works with|OO2C|2.1.11}} |
||
<lang oberon2>MODULE M; |
|||
TYPE |
|||
T = POINTER TO TDesc; |
|||
TDesc = RECORD |
|||
x: INTEGER |
|||
END; |
|||
PROCEDURE New*(): T; |
|||
VAR t: T; |
|||
BEGIN |
|||
NEW(t); t.x := 0; |
|||
RETURN t |
|||
END New; |
|||
PROCEDURE (t: T) Increment*; |
|||
BEGIN |
|||
INC(t.x) |
|||
END Increment; |
|||
END M.</lang> |
|||
Exported procedures are marked with an asterisk (*). There is nothing special about the constructor New, it is just a function that returns a new object of type T. The name of the method receiver can also be chosen freely. INC is a predeclared procedure that increments its argument. |
Exported procedures are marked with an asterisk (*). There is nothing special about the constructor New, it is just a function that returns a new object of type T. The name of the method receiver can also be chosen freely. INC is a predeclared procedure that increments its argument. |
||
Line 668: | Line 662: | ||
:''Note: This is not part of standard Pascal, but Turbo Pascal specific'' |
:''Note: This is not part of standard Pascal, but Turbo Pascal specific'' |
||
<lang objectpascal>type |
|||
type |
|||
MyClass = object |
|||
variable: integer; |
|||
constructor init; |
|||
destructor done; |
|||
procedure someMethod; |
|||
end; |
|||
constructor MyClass.init; |
|||
begin |
|||
variable := 0; |
|||
end; |
|||
procedure MyClass.someMethod; |
|||
begin |
|||
variable := 1; |
|||
end; |
|||
var |
|||
instance: MyClass; { as variable } |
|||
pInstance: ^MyClass; { on free store } |
|||
begin |
begin |
||
variable := 0; |
|||
{ create instances } |
|||
instance.init; |
|||
new(pInstance, init); { alternatively: pInstance := new(MyClass, init); } |
|||
{ call method } |
|||
instance.someMethod; |
|||
pInstance^.someMethod; |
|||
{ get rid of the objects } |
|||
instance.done; |
|||
dispose(pInstance, done); |
|||
end; |
end; |
||
procedure MyClass.someMethod; |
|||
begin |
|||
variable := 1; |
|||
end; |
|||
var |
|||
instance: MyClass; { as variable } |
|||
pInstance: ^MyClass; { on free store } |
|||
begin |
|||
{ create instances } |
|||
instance.init; |
|||
new(pInstance, init); { alternatively: pInstance := new(MyClass, init); } |
|||
{ call method } |
|||
instance.someMethod; |
|||
pInstance^.someMethod; |
|||
{ get rid of the objects } |
|||
instance.done; |
|||
dispose(pInstance, done); |
|||
end;</lang> |
|||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
Line 713: | Line 707: | ||
Interface: |
Interface: |
||
<lang objc>// There are no class variables, so static variables are used. |
|||
<lang objc> |
|||
static int myClassVariable = 0; |
|||
// There are no class variables, so static variables are used. |
|||
static int myClassVariable = 0; |
|||
@interface MyClass : NSObject |
|||
{ |
|||
@interface MyClass : NSObject |
|||
int variable; // instance variable |
|||
{ |
|||
} |
|||
int variable; // instance variable |
|||
} |
|||
- (int)variable; // Typical accessor - you should use the same name as the variable |
|||
- (int)variable; // Typical accessor - you should use the same name as the variable |
|||
@end</lang> |
|||
@end |
|||
</lang> |
|||
Implementation: |
Implementation: |
||
<lang objc> |
<lang objc>@implementation MyClass |
||
// Was not declared because init is defined in NSObject |
|||
- init |
|||
{ |
|||
if (self = [super init]) |
|||
variable = 0; |
|||
return self; |
|||
} |
|||
- (int)variable |
|||
{ |
|||
return variable; |
|||
} |
|||
@end</lang> |
|||
Using the class: |
Using the class: |
||
<lang objc> |
<lang objc>// Creating an instance |
||
MyClass *mc = [[MyClass alloc] init]; |
|||
// Sending a message |
|||
[mc variable]; |
|||
// Releasing it. When its reference count goes to zero, it will be deallocated |
|||
[mc release];</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
Line 886: | Line 878: | ||
it. |
it. |
||
<lang pop11>uses objectclass; |
|||
define :class MyClass; |
|||
slot value = 1; |
|||
enddefine;</lang> |
|||
Defining class MyClass automatically defines two constructors, newMyClass and consMyClass and slot (instance variable) accessors, so we can immediately start using our new class: |
Defining class MyClass automatically defines two constructors, newMyClass and consMyClass and slot (instance variable) accessors, so we can immediately start using our new class: |
||
<lang pop11>;;; Construct instance with default slot values |
|||
lvars instance1 = newMyClass(); |
|||
;;; Construct instance with explicitely given slot values |
|||
lvars instance2 = consMyClass(15); |
|||
;;; Print slot value using dot notation |
|||
instance1.value => |
|||
instance2.value => |
|||
;;; Print slot value using funtional notation |
|||
value(instance1) => |
|||
;;; Change slot value |
|||
12 -> value(instance1); |
|||
;;; Print it |
|||
value(instance1) =></lang> |
|||
We can add methods at any time (even after creating an instance): |
We can add methods at any time (even after creating an instance): |
||
<lang pop11>define :method reset(x : MyClass); |
|||
0 -> value(x); |
|||
enddefine; |
|||
reset(instance1); |
|||
;;; Print it |
|||
instance1 =></lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 970: | Line 962: | ||
===S3=== |
===S3=== |
||
S3 provides a very simple class system designed to be easily used interactively. |
S3 provides a very simple class system designed to be easily used interactively. |
||
<lang R>#You define a class simply by setting the class attribute of an object |
|||
<lang R> |
|||
#You define a class simply by setting the class attribute of an object |
|||
circS3 <- list(radius=5.5, centre=c(3, 4.2)) |
circS3 <- list(radius=5.5, centre=c(3, 4.2)) |
||
class(circS3) <- "circle" |
class(circS3) <- "circle" |
||
Line 983: | Line 974: | ||
type="l", ...) |
type="l", ...) |
||
} |
} |
||
plot(circS3) |
plot(circS3)</lang> |
||
</lang> |
|||
===S4=== |
===S4=== |
||
S4 is a more formal class system that provides validity checking and a way to define different methods for different input signatures. |
S4 is a more formal class system that provides validity checking and a way to define different methods for different input signatures. |
||
<lang R> |
<lang R>setClass("circle", |
||
setClass("circle", |
|||
representation( |
representation( |
||
radius="numeric", |
radius="numeric", |
||
Line 1,011: | Line 1,000: | ||
type="l", ...) |
type="l", ...) |
||
}) |
}) |
||
plot(circS4) |
plot(circS4)</lang> |
||
</lang> |
|||
=={{header|RapidQ}}== |
=={{header|RapidQ}}== |
||
<lang rapidq>TYPE MyClass EXTENDS QObject |
|||
Variable AS INTEGER |
|||
CONSTRUCTOR |
|||
Variable = 0 |
|||
END CONSTRUCTOR |
|||
SUB someMethod |
|||
MyClass.Variable = 1 |
|||
END SUB |
|||
END TYPE |
|||
' create an instance |
|||
DIM instance AS MyClass |
|||
' invoke the method |
|||
instance.someMethod</lang> |
|||
=={{header|Raven}}== |
=={{header|Raven}}== |
||
Line 1,038: | Line 1,026: | ||
Build classes: |
Build classes: |
||
<lang raven>class Alpha |
|||
'I am Alpha.' as greeting |
|||
define say_hello |
|||
greeting print |
|||
class Beta extend Alpha |
|||
'I am Beta!' as greeting</lang> |
|||
Execute classes to create objects: |
Execute classes to create objects: |
||
<lang raven>Alpha as alpha |
|||
Beta as beta</lang> |
|||
Call methods: |
Call methods: |
||
<lang raven>alpha.say_hello |
|||
beta.say_hello</lang> |
|||
Result: |
Result: |
||
<lang raven>I am Alpha. |
|||
I am Beta!</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Line 1,085: | Line 1,073: | ||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
Slate objects operate as prototypes with multi-methods: |
Slate objects operate as prototypes with multi-methods: |
||
<lang slate>prototypes define: #MyPrototype &parents: {Cloneable} &slots: #(instanceVar). |
|||
<lang slate> |
|||
prototypes define: #MyPrototype &parents: {Cloneable} &slots: #(instanceVar). |
|||
MyPrototype traits addSlot: #classVar. |
MyPrototype traits addSlot: #classVar. |
||
Line 1,097: | Line 1,084: | ||
[ |
[ |
||
x instanceVar = 1 /\ (x classVar = 3) |
x instanceVar = 1 /\ (x classVar = 3) |
||
]. |
].</lang> |
||
=={{header|Smalltalk}}== |
|||
</lang> |
|||
<lang smalltalk>Object subclass: #MyClass |
|||
instanceVariableNames: 'instanceVar' |
|||
classVariableNames: 'classVar' |
|||
poolDictionaries: '' |
|||
category: 'Testing' ! |
|||
!MyClass class methodsFor: 'instance creation'! |
|||
=={{header|Smalltalk}}== |
|||
new |
|||
<lang smalltalk> Object subclass: #MyClass |
|||
^self basicNew instanceVar := 0 ! ! |
|||
classVariableNames: 'classVar' |
|||
!MyClass methodsFor: 'testing'! |
|||
poolDictionaries: '' |
|||
someMethod |
|||
category: 'Testing' ! |
|||
^self instanceVar = 1; classVar = 3 ! ! |
|||
!MyClass class methodsFor: 'instance creation'! |
|||
MyClass new someMethod!</lang> |
|||
new |
|||
^self basicNew instanceVar := 0 ! ! |
|||
!MyClass methodsFor: 'testing'! |
|||
someMethod |
|||
^self instanceVar = 1; classVar = 3 ! ! |
|||
MyClass new someMethod!</lang> |
|||
=={{header|SuperCollider}}== |
=={{header|SuperCollider}}== |
||
Line 1,171: | Line 1,156: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
===Defining a class=== |
===Defining a class=== |
||
<lang vbnet> |
<lang vbnet>Class Foo |
||
Private m_Bar As Integer |
|||
Public Sub New() |
|||
End Sub |
|||
Public Sub New(ByVal bar As Integer) |
|||
m_Bar = bar |
|||
End Sub |
|||
Public Property Bar() As Integer |
|||
Get |
|||
Return m_Bar |
|||
End Get |
|||
Set(ByVal value As Integer) |
|||
m_Bar = value |
|||
End Set |
|||
End Property |
|||
Public Sub DoubleBar() |
|||
m_Bar *= 2 |
|||
End Sub |
|||
Public Function MultiplyBar(ByVal x As Integer) As Integer |
|||
Return x * Bar |
|||
End Function |
|||
End Class</lang> |
|||
===Using an object=== |
===Using an object=== |
||
<lang vbnet> |
<lang vbnet>'Declare and create separately |
||
Dim foo1 As Foo |
|||
foo1 = New Foo |
|||
'Declare and create at the same time |
|||
Dim foo2 As New Foo |
|||
'... while passing constructor parameters |
|||
Dim foo3 As New Foo(5) |
|||
'... and them immediately set properties |
|||
Dim foo4 As New Foo With {.Bar = 10} |
|||
'Calling a method that returns a value |
|||
Console.WriteLine(foo4.MultiplyBar(20)) |
|||
'Calling a method that performs an action |
|||
foo4.DoubleBar() |
|||
'Reading/writing properties |
|||
Console.WriteLine(foo4.Bar) |
|||
foo4.Bar = 1000</lang> |
|||
{{omit from|AWK}} |
{{omit from|AWK}} |
Revision as of 16:41, 19 November 2009
You are encouraged to solve this task according to the task description, using any language you may know.
In object-oriented programming class is a set (a transitive closure) of types bound by the relation of inheritance. It is said that all types derived from some base type T and the type T itself form a class T. The first type T from the class T sometimes is called the root type of the class.
A class of types itself, as a type, has the values and operations of its own. The operations of are usually called methods of the root type. Both operations and values are called polymorphic.
A polymorphic operation (method) selects an implementation depending on the actual specific type of the polymorphic argument. The action of choice the type-specific implementation of a polymorphic operation is called dispatch. Correspondingly, polymorphic operations are often called dispatching or virtual. Operations with multiple arguments and/or the results of the class are called multi-methods. A further generalization of is the operation with arguments and/or results from different classes.
- single-dispatch languages are those that allow only one argument or result to control the dispatch. Usually it is the first parameter, often hidden, so that a prefix notation x.f() is used instead of mathematical f(x).
- multiple-dispatch languages allow many arguments and/or results to control the dispatch.
A polymorphic value has a type tag indicating its specific type from the class and the corresponding specific value of that type. This type is sometimes called the most specific type of a [polymorphic] value. The type tag of the value is used in order to resolve the dispatch. The set of polymorphic values of a class is a transitive closure of the sets of values of all types from that class.
In many OO languages the type of the class of T and T itself are considered equivalent. In some languages they are distinct (like in Ada). When class T and T are equivalent, there is no way to distinguish polymorphic and specific values.
The purpose of this task is to create a basic class with a method, a constructor, an instance variable and how to instantiate it.
ActionScript
<lang actionscript>package {
public class MyClass { private var myVariable:int; // Note: instance variables are usually "private" /** * The constructor */ public function MyClass() { // creates a new instance } /** * A method */ public function someMethod():void { this.myVariable = 1; // Note: "this." is optional // myVariable = 1; works also } }
}</lang>
Ada
Class is used in many languages to provide both encapsulation, or grouping of data and actions, and type definition. Ada packages provide encapsulation or grouping while type definitions are done using the type reserved word. Types participating in inheritance are named tagged record types.
A package specification has the following form: <lang ada>package My_Package is
type My_Type is tagged private; procedure Some_Procedure(Item : out My_Type); function Set(Value : in Integer) return My_Type;
private
type My_Type is tagged record Variable : Integer := -12; end record;
end My_Package;</lang> The type declaration at the top of the package gives public visibility to the private tagged type My_Type. Since My_Type is declared to be private, the public has no visibility of its structure. The type must be treated as a black box. The private section of the package specification includes the actual tagged record definition. Note that the data member Variable is initialized to -12. This corresponds to a default constructor for the type.
The package body must contain the implementation of the procedures and functions declared in the package specification. <lang ada> package body My_Package is
procedure Some_Procedure(Item : out My_Type) is begin Item := 2 * Item; end Some_Procedure; function Set(Value : Integer) return My_Type is Temp : My_Type; begin Temp.Variable := Value; return Temp; end Set;
end My_Package;</lang> The Set function acts as a conversion constructor for My_Type.
An instance is typically created outside the package: <lang ada>with My_Package; use My_Package;
procedure Main is
Foo : My_Type; -- Foo is created and initialized to -12
begin
Some_Procedure(Foo); -- Foo is doubled Foo := Set(2007); -- Foo.Variable is set to 2007
end Main;</lang>
AmigaE
<lang amigae>OBJECT a_class
varA, varP
ENDOBJECT
-> this could be used like a constructor PROC init() OF a_class
self.varP := 10 self.varA := 2
ENDPROC
-> the special proc end() is for destructor PROC end() OF a_class -> nothing to do here... ENDPROC
-> a not so useful getter PROC getP() OF a_class IS self.varP
PROC main()
DEF obj : PTR TO a_class NEW obj.init() WriteF('\d\n', obj.varA) -> this can be done, while -> varP can't be accessed directly WriteF('\d\n', obj.varP) -> or WriteF('\d\n', obj.getP()) END obj
ENDPROC</lang>
BASIC
DECLARE SUB MyClassDelete (pthis AS MyClass) DECLARE SUB MyClassSomeMethod (pthis AS MyClass) DECLARE SUB MyClassInit (pthis AS MyClass)
TYPE MyClass Variable AS INTEGER END TYPE
DIM obj AS MyClass MyClassInit obj MyClassSomeMethod obj
SUB MyClassInit (pthis AS MyClass) pthis.Variable = 0 END SUB
SUB MyClassSomeMethod (pthis AS MyClass) pthis.Variable = 1 END SUB
ALGOL 68
The following code is experimental. Basically ALGOL 68 is not object oriented, so the task to create (and use of) objects is tedious due to the lack of certain constructs, especially the lack of OO syntactic sugar. For further details:
Other examples of this experimental approach are located at pages: Life in two dimensions, Playing Cards and Stack.
<lang algol68>MODE MYDATA = STRUCT(
INT name1
); STRUCT(
INT name2, PROC (REF MYDATA)REF MYDATA new, PROC (REF MYDATA)VOID init, PROC (REF MYDATA)VOID some method
) class my data := (
# name2 := # 2, # Class attribute # # PROC new := # (REF MYDATA new)REF MYDATA:( (init OF class my data)(new); new ),
# PROC init := # (REF MYDATA self)VOID:( """ Constructor (Technically an initializer rather than a true 'constructor') """; name1 OF self := 0 # Instance attribute # ), # PROC some method := # (REF MYDATA self)VOID:( """ Method """; name1 OF self := 1; name2 OF class my data := 3 )
);
- class name, invoked as a function is the constructor syntax #
REF MYDATA my data = (new OF class my data)(LOC MYDATA);
MODE GENDEROPT = UNION(STRING, VOID); MODE AGEOPT = UNION(INT, VOID);
MODE MYOTHERDATA = STRUCT(
STRING name, GENDEROPT gender, AGEOPT age
); STRUCT (
INT count, PROC (REF MYOTHERDATA, STRING, GENDEROPT, AGEOPT)REF MYOTHERDATA new, PROC (REF MYOTHERDATA, STRING, GENDEROPT, AGEOPT)VOID init, PROC (REF MYOTHERDATA)VOID del
) class my other data := (
# count := # 0, # Population of "(init OF class my other data)" objects #
# PROC new := # (REF MYOTHERDATA new, STRING name, GENDEROPT gender, AGEOPT age)REF MYOTHERDATA:( (init OF class my other data)(new, name, gender, age); new ),
# PROC init := # (REF MYOTHERDATA self, STRING name, GENDEROPT gender, AGEOPT age)VOID:( """ One initializer required, others are optional (with different defaults) """; count OF class my other data +:= 1; name OF self := name; gender OF self := gender; CASE gender OF self IN (VOID):gender OF self := "Male" ESAC; age OF self := age ),
# PROC del := # (REF MYOTHERDATA self)VOID:( count OF class my other data -:= 1 )
);
PROC attribute error := STRING: error char; # mend the error with the "error char" #
- Allocate the instance from HEAP #
REF MYOTHERDATA person1 = (new OF class my other data)(HEAP MYOTHERDATA, "John", EMPTY, EMPTY); print (((name OF person1), space,
(gender OF person1|(STRING gender):gender|attribute error), new line)); # "John Male" #
print (((age OF person1|(INT age):age|attribute error), new line)); # Raises AttributeError exception! #
- Allocate the instance from LOC (stack) #
REF MYOTHERDATA person2 = (new OF class my other data)(LOC MYOTHERDATA, "Jane", "Female", 23); print (((name OF person2), space,
(gender OF person2|(STRING gender):gender|attribute error), space, (age OF person2|(INT age):age|attribute error), new line)) # "Jane Female 23" #</lang>
Output:
John Male * Jane Female +23
C
<lang c>typedef struct MyClass {
int variable;
} MyClass;
struct MyClass* MyClass_new() {
struct MyClass* pthis = (struct MyClass*)malloc( sizeof(struct MyClass) ); //memset(pthis, 0, sizeof(struct MyClass) ); pthis->variable = 0; return pthis;
}
void MyClass_delete(struct MyClass** pthis) {
if(pthis && *pthis) { free(*pthis); *pthis = NULL; }
}
struct void MyClass_someMethod(struct MyClass* pthis) {
pthis->variable = 1;
}
struct MyClass* obj = MyClass_new(); MyClass_someMethod(obj); MyClass_delete(&obj);</lang>
C++
<lang cpp>class MyClass { public:
void someMethod(); // member function = method MyClass(); // constructor
private:
int variable; // member variable = instance variable
};
// implementation of constructor MyClass::MyClass():
variable(0)
{
// here could be more code
}
// implementation of member function void MyClass::someMethod() {
variable = 1; // alternatively: this->variable = 1
}
// Create an instance as variable MyClass instance;
// Create an instance on free store MyClass* pInstance = new MyClass; // Instances allocated with new must be explicitly destroyed when not needed any more: delete pInstance;</lang>
Note: MyClass instance(); would not define an instance, but declare a function returning an instance. Accidentally declaring functions when object definitions are wanted is a rather common bug in C++.
Functions can also be defined inline:
<lang cpp>class MyClass { public:
MyClass(): variable(0) {} void someMethod() { variable = 1; }
private:
int variable;
};</lang>
Note that member functions in C++ by default are not polymorphic; if you want a polymorphic member function, you have to mark it as virtual. In that case, you should also add a virtual destructor, even if that is empty. Example:
<lang cpp>class MyClass { public:
virtual void someMethod(); // this is polymorphic virtual ~MyClass(); // destructor
};</lang>
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>
Common Lisp
<lang lisp>(defclass circle ()
((radius :initarg :radius :initform 1.0 :type number :reader radius)))
(defmethod area ((shape circle))
(* pi (expt (radius shape) 2)))
> (defvar *c* (make-instance 'circle :radius 2)) > (area *c*) 12.566370614359172d0</lang>
D
<lang d>module Class;
import std.stdio;
class MyClass {
//constructor this() { }
void someMethod() { variable = 1; }
private int _variable;
// getter method int variable() { return _variable; }
// setter method int variable(int new_variable) { return _variable = new_variable; }
}
void main() {
// scope instances are allocated on the heap // this is not really necessary because unneeded objects will be GC'ed scope instance = new MyClass();
// prints 'variable=0' because ints are initialized to 0 by default writefln("variable=", instance.variable); // invoke the method instance.someMethod;
// prints 'variable=1' writefln("variable=", instance.variable);
// set the variable using setter method instance.variable = 99;
// prints 'variable=99' writefln("variable=", instance.variable);
}</lang>
E
In E, classes, constructors, and instance variables are not built into the language. This is an example of the basic convention; different cases may call for objects built in different ways.
<lang e>def makeColor(name :String) {
def color { to colorize(thing :String) { return `$name $thing` } } return color
}</lang>
Example interactive session creating and using it:
<lang e>? def red := makeColor("red")
- value: <color>
? red.colorize("apple")
- value: "red apple"</lang>
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>
Forth
ANSI Forth has no object oriented features, but as Forth is a very easy language to extend, many object oriented programming systems have been implemented for it over the years. WinForth has one such system, which is described here.
Declare a class
<lang forth>:class MyClass <super Object
int memvar
:m ClassInit: ( -- ) ClassInit: super 1 to memvar ;m
:m ~: ( -- ) ." Final " show: [ Self ] ;m
:m set: ( n -- ) to memvar ;m :m show: ( -- ) ." Memvar = " memvar . ;m
- class</lang>
Allocate a static object
<lang forth>MyClass newInstance</lang>
Allocate a dynamic object, saving its pointer in a global variable.
<lang forth>New> MyClass value newInstance</lang>
Call member functions
<lang forth>10 set: newInstance show: newInstance</lang>
Free a dynamically allocated object
<lang forth>newInstance dispose 0 to newInstance \ no dangling pointers!</lang>
Example of dynamic allocation and local variable use"
<lang forth>: test { \ obj -- }
New> MyClass to obj show: obj 1000 set: obj obj dispose ;</lang>
Groovy
A class: <lang groovy>/** Ye olde classe declaration */ class Stuff {
/** Heare bee anne instance variable declared */ def guts /** This constuctor converts bits into Stuff */ Stuff(injectedGuts) { guts = injectedGuts } /** Brethren and sistren, let us flangulate with this fine flangulating method */ def flangulate() { println "This stuff is flangulating its guts: ${guts}" }
}</lang>
A demonstration: <lang groovy>def stuff = new Stuff( I have made mistakes in the past. I have made mistakes in the future.
-- Vice President Dan Quayle
)
stuff.flangulate()
stuff.guts = Our enemies are innovative and resourceful, and so are we. They never stop thinking about new ways to harm our country and our people, and neither do we.
-- President George W. Bush
stuff.flangulate()</lang>
Output:
This stuff is flangulating its guts: I have made mistakes in the past. I have made mistakes in the future. -- Vice President Dan Quayle This stuff is flangulating its guts: Our enemies are innovative and resourceful, and so are we. They never stop thinking about new ways to harm our country and our people, and neither do we. -- President George W. Bush
J
Class definition: <lang j>coclass 'exampleClass'
exampleMethod=: monad define
1+exampleInstanceVariable
)
create=: monad define
'this is the constructor'
)
exampleInstanceVariable=: 0</lang>
Instantiation: <lang j> exampleObject=: conew 'exampleClass'</lang>
Java
<lang java>public class MyClass {
// instance variable private int variable; // Note: instance variables are usually "private"
/** * The constructor */ public MyClass() { // creates a new instance }
/** * A method */ public void someMethod() { this.variable = 1; }
}</lang> Note: "this." in someMethod is optional. "variable = 1;" works also. If a parameter also named "variable" came into someMethod, using "this" specifies using the instance variable rather than the local method variable.
JavaScript
<lang javascript>//constructor function MyClass(initVal) {
//instance variable if(initVal == undefined) { this.number = 1; } else { this.number = initVal; }
}
//method of MyClass MyClass.prototype.getDouble = function() {
return this.number * 2;
};
var instance1 = new MyClass; //or "new MyClass();" instance1.number = 5; alert( instance1.getDouble() ); //10
var instance2 = new MyClass(3); alert( instance2.getDouble() ); //6</lang>
Replace the alert
calls with WScript.Echo
, and this code
Lisaac
<lang Lisaac>Section Header
+ name := SAMPLE;
Section Inherit
- parent : OBJECT := OBJECT;
Section Private
+ variable : INTEGER <- 0;
Section Public
- some_method <- (
variable := 1;
);
- main <- (
+ sample : SAMPLE;
sample := SAMPLE.clone; sample.some_method;
);</lang>
Oberon-2
<lang oberon2>MODULE M;
TYPE T = POINTER TO TDesc; TDesc = RECORD x: INTEGER END;
PROCEDURE New*(): T; VAR t: T; BEGIN NEW(t); t.x := 0; RETURN t END New;
PROCEDURE (t: T) Increment*; BEGIN INC(t.x) END Increment;
END M.</lang>
Exported procedures are marked with an asterisk (*). There is nothing special about the constructor New, it is just a function that returns a new object of type T. The name of the method receiver can also be chosen freely. INC is a predeclared procedure that increments its argument.
Object Pascal
- Note: This is not part of standard Pascal, but Turbo Pascal specific
<lang objectpascal>type
MyClass = object variable: integer; constructor init; destructor done; procedure someMethod; end;
constructor MyClass.init;
begin variable := 0; end;
procedure MyClass.someMethod;
begin variable := 1; end;
var
instance: MyClass; { as variable } pInstance: ^MyClass; { on free store }
begin
{ create instances } instance.init; new(pInstance, init); { alternatively: pInstance := new(MyClass, init); } { call method } instance.someMethod; pInstance^.someMethod; { get rid of the objects } instance.done; dispose(pInstance, done);
end;</lang>
Objective-C
Interface:
<lang objc>// There are no class variables, so static variables are used. static int myClassVariable = 0;
@interface MyClass : NSObject {
int variable; // instance variable
}
- (int)variable; // Typical accessor - you should use the same name as the variable
@end</lang>
Implementation:
<lang objc>@implementation MyClass
// Was not declared because init is defined in NSObject - init {
if (self = [super init]) variable = 0; return self;
}
- (int)variable {
return variable;
}
@end</lang>
Using the class:
<lang objc>// Creating an instance MyClass *mc = [[MyClass alloc] init];
// Sending a message [mc variable];
// Releasing it. When its reference count goes to zero, it will be deallocated [mc release];</lang>
OCaml
<lang ocaml>class my_class =
object (self) val mutable variable = 0 method some_method = variable <- 1 end</lang>
Using the class:
# let instance = new my_class;; val instance : my_class = <obj> # instance#some_method;; - : unit = ()
Perl
The implementation (there are no declarations): <lang perl>{
# a class is a package (i.e. a namespace) with methods in it package MyClass;
# a constructor is a function that returns a blessed reference sub new { my $class = shift; bless {variable => 0}, $class; # the instance object is a hashref in disguise. # (it can be a ref to anything.) };
# an instance method is a function that takes an object as first argument. # the -> invocation syntax takes care of that nicely, see Usage paragraph below. sub someMethod { my $self = shift; $self->{variable} = 1; };
};</lang> Using the class: <lang perl>my $instance = MyClass->new; # invoke constructor method
$instance->someMethod; # invoke method on object instance
# instance deallocates when the last reference falls out of scope</lang>
Perl 6
<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; # The ! twigil makes it private. has Str $.name; has Str $.color; has Bool $.wings;
multi method new (Str $s) { Butterfly.bless(*, name => $s, color => 'blue') } multi method new (Int $n) { Butterfly.bless(*, age => $n, name => "Butterfly aged $n") }
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 5; say "Name: {$a.name}, Color: {$a.color}"; $a.flap;
my Butterfly $b .= new 'Osgood', age => 4; say "Name: {$b.name}, Color: {$b.color}"; $b.flap;</lang>
PHP
<lang php>class MyClass {
public static $classVar; public $instanceVar; // can also initialize it here function __construct() { $this->instanceVar = 0; } function someMethod() { $this->instanceVar = 1; self::$classVar = 3; }
} $myObj = new MyClass();</lang>
Pop11
Object system is implemented as a library, so we must first load it.
<lang pop11>uses objectclass; define :class MyClass;
slot value = 1;
enddefine;</lang>
Defining class MyClass automatically defines two constructors, newMyClass and consMyClass and slot (instance variable) accessors, so we can immediately start using our new class:
<lang pop11>;;; Construct instance with default slot values lvars instance1 = newMyClass();
- Construct instance with explicitely given slot values
lvars instance2 = consMyClass(15);
- Print slot value using dot notation
instance1.value => instance2.value =>
- Print slot value using funtional notation
value(instance1) =>
- Change slot value
12 -> value(instance1);
- Print it
value(instance1) =></lang>
We can add methods at any time (even after creating an instance):
<lang pop11>define :method reset(x : MyClass);
0 -> value(x);
enddefine; reset(instance1);
- Print it
instance1 =></lang>
Python
<lang python>class MyClass:
name2 = 2 # Class attribute
def __init__(self): """ Constructor (Technically an initializer rather than a true "constructor") """ self.name1 = 0 # Instance attribute def someMethod(self): """ Method """ self.name1 = 1 MyClass.name2 = 3
myclass = MyClass() # class name, invoked as a function is the constructor syntax.
class MyOtherClass:
count = 0 # Population of "MyOtherClass" objects def __init__(self, name, gender="Male", age=None): """ One initializer required, others are optional (with different defaults) """ MyOtherClass.count += 1 self.name = name self.gender = gender if age is not None: self.age = age def __del__(self): MyOtherClass.count -= 1
person1 = MyOtherClass("John") print person1.name, person1.gender # "John Male" print person1.age # Raises AttributeError exception! person2 = MyOtherClass("Jane", "Female", 23) print person2.name, person2.gender, person2.age # "Jane Female 23"</lang>
Python allows for very flexible argument passing including normal named parameters, defaulted/optional named parameters, up to one "varargs" tuple, and any number of keywords arguments (which are all passed in the form of a single dictionary (associative array), and any non-ambiguous combination of these). All types of argument passing for functions can also be used for object instantiation/initialization (passed to the special __init__() method) as shown.
New-style classes inherit from "object" or any descendant of the "object" class:
<lang python>class MyClass(object):
...</lang>
These "new-style" classes support some features which were unavailable in "classic classes". New features include a __new__() with lower level control over object instantiation, metaclass support, static methods, class methods, "properties" (managed attributes) and "slots" (attribute restrictions).
R
R has (at least) 5 different object oriented systems. S3 and S4 correspond to different versions of the S language, from which R was derived. See, for example, this presentation by Freidrich Leisch for a more thorough introduction to S3 and S4 classes. Both these class systems are in use, and ship with the standard R distribution. The OOP, R.oo and proto packages provide other systems.
S3
S3 provides a very simple class system designed to be easily used interactively. <lang R>#You define a class simply by setting the class attribute of an object circS3 <- list(radius=5.5, centre=c(3, 4.2)) class(circS3) <- "circle"
- plot is a generic function, so we can define a class specific method by naming it plot.classname
plot.circle <- function(x, ...) {
t <- seq(0, 2*pi, length.out=200) plot(x$centre[1] + x$radius*cos(t), x$centre[2] + x$radius*sin(t), type="l", ...)
} plot(circS3)</lang>
S4
S4 is a more formal class system that provides validity checking and a way to define different methods for different input signatures. <lang R>setClass("circle",
representation( radius="numeric", centre="numeric"), prototype( radius=1, centre=c(0,0)))
- Instantiate class with some arguments
circS4 <- new("circle", radius=5.5)
- Set other data slots (properties)
circS4@centre <- c(3,4.2)
- Define a method
setMethod("plot", #signature("circle"),
signature(x="circle", y="missing"), function(x, ...) { t <- seq(0, 2*pi, length.out=200) #Note the use of @ instead of $ plot(x@centre[1] + x@radius*cos(t), x@centre[2] + x@radius*sin(t), type="l", ...) })
plot(circS4)</lang>
RapidQ
<lang rapidq>TYPE MyClass EXTENDS QObject
Variable AS INTEGER
CONSTRUCTOR Variable = 0 END CONSTRUCTOR
SUB someMethod MyClass.Variable = 1 END SUB
END TYPE
' create an instance DIM instance AS MyClass
' invoke the method instance.someMethod</lang>
Raven
Build classes:
<lang raven>class Alpha
'I am Alpha.' as greeting define say_hello greeting print
class Beta extend Alpha
'I am Beta!' as greeting</lang>
Execute classes to create objects:
<lang raven>Alpha as alpha Beta as beta</lang>
Call methods:
<lang raven>alpha.say_hello beta.say_hello</lang>
Result:
<lang raven>I am Alpha. I am Beta!</lang>
Ruby
<lang ruby>class MyClass
@@class_var = []
def initialize # 'initialize' is the constructor method invoked during 'MyClass.new' @instance_var = 0 end
def some_method @instance_var = 1 @@class_var << Time.now end
def self.class_method # ... end
end
myclass = MyClass.new</lang>
Slate
Slate objects operate as prototypes with multi-methods: <lang slate>prototypes define: #MyPrototype &parents: {Cloneable} &slots: #(instanceVar). MyPrototype traits addSlot: #classVar.
x@(MyPrototype traits) new [
x clone `>> [instanceVar: 0. ]
].
x@(MyPrototype traits) someMethod [
x instanceVar = 1 /\ (x classVar = 3)
].</lang>
Smalltalk
<lang smalltalk>Object subclass: #MyClass
instanceVariableNames: 'instanceVar' classVariableNames: 'classVar' poolDictionaries: category: 'Testing' !
!MyClass class methodsFor: 'instance creation'! new
^self basicNew instanceVar := 0 ! !
!MyClass methodsFor: 'testing'! someMethod
^self instanceVar = 1; classVar = 3 ! !
MyClass new someMethod!</lang>
SuperCollider
<lang SuperCollider>MyClass {
classvar someVar, <another, <>thirdVar; // Class variables. var <>something, <>somethingElse; // Instance variables. // Note: variables are private by default. In the above, "<" enables getting, ">" enables setting
*new { ^super.new.init // constructor is a class method. typically calls some instance method to set up, here "init" }
init { something = thirdVar.squared; somethingElse = this.class.name; }
*aClassMethod { ^ someVar + thirdVar // The "^" means to return the result. If not specified, then the object itself will be returned ("^this") }
anInstanceMethod { something = something + 1; }
}</lang>
Tcl
and the TclOO package
<lang Tcl>oo::class create summation {
variable v constructor {} { set v 0 } method add x { incr v $x } method value {} { return $v } destructor { puts "Ended with value $v" }
} set sum [summation new] puts "Start with [$sum value]" for {set i 1} {$i <= 10} {incr i} {
puts "Add $i to get [$sum add $i]"
} $sum destroy</lang>
Visual Basic .NET
Defining a class
<lang vbnet>Class Foo
Private m_Bar As Integer
Public Sub New()
End Sub
Public Sub New(ByVal bar As Integer) m_Bar = bar End Sub
Public Property Bar() As Integer Get Return m_Bar End Get Set(ByVal value As Integer) m_Bar = value End Set End Property
Public Sub DoubleBar() m_Bar *= 2 End Sub
Public Function MultiplyBar(ByVal x As Integer) As Integer Return x * Bar End Function
End Class</lang>
Using an object
<lang vbnet>'Declare and create separately Dim foo1 As Foo foo1 = New Foo
'Declare and create at the same time Dim foo2 As New Foo
'... while passing constructor parameters Dim foo3 As New Foo(5)
'... and them immediately set properties Dim foo4 As New Foo With {.Bar = 10}
'Calling a method that returns a value Console.WriteLine(foo4.MultiplyBar(20))
'Calling a method that performs an action foo4.DoubleBar()
'Reading/writing properties Console.WriteLine(foo4.Bar) foo4.Bar = 1000</lang>
- Programming Tasks
- Basic language learning
- Object oriented
- Type System
- Encyclopedia
- ActionScript
- Ada
- AmigaE
- BASIC
- ALGOL 68
- C
- C++
- C sharp
- Common Lisp
- D
- E
- Factor
- Forth
- Groovy
- J
- Java
- JavaScript
- Lisaac
- Oberon-2
- Object Pascal
- Objective-C
- OCaml
- Perl
- Perl 6
- PHP
- Pop11
- Python
- R
- RapidQ
- Raven
- Ruby
- Slate
- Smalltalk
- SuperCollider
- Tcl
- Visual Basic .NET
- AWK/Omit
- Fortran/Omit
- M4/Omit
- Metafont/Omit
- Octave/Omit
- TI-89 BASIC/Omit