{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Jan072010 - UCSantaCruz Switchstatement switch(expression...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science – Game Design UC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Switch statement AlternaFve to if Typically use break Can use goto to conFnue to another case switch (expression) { case constant-expression: statement(s); jump-statement [ default : statement(s);] Example: const int raining = 1; const int snowing = 0; int weather = snowing; switch (weather) { case snowing: System.Console.Writeln(“It is snowing!”); goto case raining; case raining; System.Console.Writeln(“I am wet!”); break; default: System.Console.Writeln(“Weath er OK”); break; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Computer Science – Game Design UC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC InformaFon Hiding in OO Languages Classes support informaFon hiding Data members of a class can be declared as private Hides implementaFon details of the class Create accessor methods to access data Typically get__() and set__(), which are public “GeUers and seUers” Other classes access data via get() and set() method So long as the interface to get and set stay the same, the class can change how it represents its data InformaFon hiding permits implementaFons to change without affecFng using classes But, is tedious to always access data via accessors x = foo.getX() is more tedious than x = foo.x; Wouldn’t it be great to preserve benefits of accessors, while also retaining the syntax of direct access?
Background image of page 2
Computer Science – Game Design UC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC C# ProperFes Provide procedural access to data Like accessors But have syntax similar to direct variable access foo.X instead of foo.getX(); Minor feature, but provides substanFal improvement in readability and fluidity of programming Travis thumbs his nose at private property www.flickr.com/photos/sillygwailo/492070136/
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Computer Science – Game Design UC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC C# Property Syntax Get accessor returns value of same type as “return type” Set accessors have implicit parameter named “value” Use to set internal data representaFon ProperFes can be public, private, protected Public: any class can access, private: only that class, protected: that class and children By convenFon, property names have iniFal capital (“X” to access “x”) [access-modifiers] return-type property-name { get { … sequence of statements ending in a return (or throw) } set { … sequence of statements } } get accessor set accessor
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}