VSIP/formularium: verschil tussen versies

Uit Wina Examenwiki
Naar navigatie springen Naar zoeken springen
Geen bewerkingssamenvatting
Regel 94: Regel 94:
=C#=
=C#=


'''Getters en setters:'''
'''NOOT''' C# is, tenzij expliciet vermeld, identiek aan Java qua syntax
* Mogelijkheid 1
 
===== Enumerations =====
* enum Day : int { Monday, ..., Friday = 5, ... };
 
===== Operators =====
public static MyType operator+(MyType l, MyType r) { ... return resultMyType; }
 
===== Variable number of parameters =====
public void myMethod(string a, params int[] a) { ... }
 
===== Properties =====
* Getter/setter BEIDE publiek:
public string Foo { get; set; }
* Alternatief (cfr. Java)
  private string foo;
  private string foo;
  public string Foo {
  public string Foo {
   get { return foo; }
   get { return foo; }
   set { foo = value; }
   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]; } }
 
===== Foreach =====
MoneyAmount[] amounts = ...;
  foreach (MoneyAmount thisAmount in amounts)
  thisAmount. ...
 
===== 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)


* Mogelijkheid 2 (indien zowel getter en setter publiek) (C# 3.0+)
===== Constructor =====
  public string Foo { get; set; }
* 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() { ... }

Versie van 17 dec 2008 01:11

Schrijf hier korte voorbeeldjes neer voor elke taal

Smalltalk

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)!

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#

NOOT C# is, tenzij expliciet vermeld, identiek aan Java qua syntax

Enumerations
  • enum Day : int { Monday, ..., Friday = 5, ... };
Operators
public static MyType operator+(MyType l, MyType r) { ... return resultMyType; }
Variable number of parameters

public void myMethod(string a, params int[] a) { ... }

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]; } }
Foreach
MoneyAmount[] amounts = ...;
foreach (MoneyAmount thisAmount in amounts)
 thisAmount. ...
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)
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() { ... }