VSIP/formularium
Schrijf hier korte voorbeeldjes neer voor elke taal
Smalltalk
toewijzing : := null: nil niet gelijk aan(pointer) : ~~ this: self locale variabele: |localvar| static: class clone object o: o copy and: &
if statement
(cond) ifTrue: ['Auditorium: ' display. currentAuditorium getName displayNl.] ifFalse: ['Not in an Auditorium' displayNl.].
while
[cond] whileTrue: [C1]
array
newarray := Array new: 3. newarray at: 1 put: 42. ^ newarray size
for
1 to: (newarray size) do: [:i| C1].
room.st
Object subclass: #Room instanceVariableNames:'name maxpopulation currentPopulation visitors' classVariableNames: poolDictionaries: ! !Room class methodsFor: 'construction'! withName: theName andMaxPopulation: themaxpopulation |result| result := Room new. result withName: theName andMaxPopulation: themaxpopulation. ^result !! !Room methodsFor: 'Initialization'! withName: theName andMaxPopulation: themaxpopulation name := theName. maxpopulation := themaxpopulation. currentPopulation := 0. visitors := Array new: maxpopulation. !!
auditorium.st
Room subclass: #Auditorium instanceVariableNames:'resources' classVariableNames: poolDictionaries: ! !Auditorium class methodsFor: 'construction'! withName: theName andMaxPopulation: themaxpopulation |result| result:= self new. result withName: theName andMaxPopulation: themaxpopulation. ^result !!
main.st
FileStream fileIn: 'room.st'! FileStream fileIn: 'auditorium.st'! Smalltalk at: #a1 put: (Auditorium withName: '200A' andMaxPopulation: 5)!
Ideas: Everything is an object. To achieve something, you send messages to those objects. There are only messages and objects. Each variable can contain any object. There can be exceptions when messages are not understood. = dynamic type system. Everything is reference semantics. (dus ook integers etc) Alle small-integers met gelijke waarde zijn pointer gelijk, zelfde voor String literals. String literals ook immutable. 3 types message: unary (1 paramater nl receiver bv getName), keyword (meerde parameters bv put: at: ) of binary (+) Geen ondersteuning voor specificaties. Variables (class of instance) niet publiek Messages altijd publiek Klassen zijn ook objecten: instantiatie van de metaclass. Metaclasses zijn instanties van Metaclass klasse.
Eiffel
toewijzing : := niet gelijk aan : \= null: Void string: STRING int: INTEGER this: Current return value: Result
if cond then C1 else C2 end
class NEWCLASS -- de naam van je klasse creation constructormethod -- constructoren aanduiden feature{} -- globale variabelen name:STRING feature{} -- private other: feature{ANY}, feature{NAMECLASS1,NAMECLASS2} constructormethod(newname: like name) is do -- start method name := newname -- do stuff end -- end constructor feature{ANY} get_name :STRING is -- get_name method (NAMING CONVENTIONS: get_name) do --start method Result := name --return name end --end method invariant valid_name : not Current.name.is_equal("") end -- end class NEWCLASS
overerving voorbeeld
class AUDITORIUM inherit ROOM rename build as build_room end -- redefine parent method creation build feature{} build(n: like name; p: INTEGER) is -- (arg :like var) or (arg : TYPE) do build_room(n, p) -- constructor parent classe aanroepe end end
C++
C#
The Basics
NOOT C# is, tenzij expliciet vermeld, identiek aan Java qua syntax
Main method
static void Main(string[] args) { ... }
Preprocessor
#if, #elif, #else. #endif #define, #undef
Access control
- Classes: public, internal (assembly; default)
- Class members/structured types: public, internal, private (default), protected, protected internal
Variable number of parameters
public void myMethod(string a, params int[] a) { ... }
Foreach
MoneyAmount[] amounts = ...; foreach (MoneyAmount thisAmount in amounts) thisAmount. ...
Enumerations
enum Day : int { Monday, ..., Friday = 5, ... };
Operators
public static MyType operator+(MyType l, MyType r) { ... return resultMyType; }
Casting
- Square sq = (Square)rect;
- if (box is int) a = 5 * ((int)box);
- Square sq = rect as Square;
Properties
- Getter/setter BEIDE publiek:
public string Foo { get; set; }
- Alternatief (cfr. Java)
private string foo; public string Foo { get { return foo; } set { foo = value; } }
Indexers
class A { int[,] myInts = int[8,8]; A this[int row, int col] get { return myInts[row, col]; } set { myInts[row, col] = value; } } class B { void Bla(A myA, int row, int col) { return myA[row, col]; } }
Constructor
- readonly var initialization only within constructor
public class/struct Location { int x, int y; readonly string name; public Location(int x, int y) { this.x = x; this.y = y; name = "Sven"; } public Location(int x) : this(x, 0) {} }
Destructor
public class Location { ~Location() { ... } }
Inheritance
Interfaces
interface IMyInterface { void MyMethod(int a); } class MyDefaultImpl : IComparable, IMyInterface { public void MyMethod(int a) { ... } ... }
Abstract
abstract public class A { abstract public int MethodA(); public int MetodB() { ... } } public class B : A { override public int MethodA() { ... } }
Sealed
public class A { public int MethodA() { ... } } public class B : A { public sealed override int MethodA() { ... } } public sealed class C : B { }
Extension Inheritance
public class Rectangle { public Rectangle(int width, int height) { ... } public Foo(int a) { ... } } public class PositionedRectangle : Rectangle { public PositionedRectangle(int x, int y, int width, int height) : base(width, height) { ... } new public Foo(int a) { ... } public Foo(string a) { ... } }
Specialization Inheritance
- virtual methods: dynamic methods
public class Rectangle { public double Surface { ... } virtual public double Perimeter { ... } } public class Square : Rectangle { new public double Surface { ... } override public double Perimeter { ... } }