Docsity

Typesafe Enums and Interfaces in Java - Prof. Benjamin B. Bederson, Study notes of Computer Science

Partial preview of the text

Download Typesafe Enums and Interfaces in Java - Prof. Benjamin B. Bederson and more Study notes Computer Science in PDF only on Docsity!

C#

for Programmers

using

System;

using

System;

using

System;

using

System;

class

Hello

class

Hello

class

Hello

class

Hello

static

void

Main()

static

void

Main()

static

void

Main()

static

void

Main()

Console.WriteLine("HelloConsole.WriteLine("HelloConsole.WriteLine("HelloConsole.WriteLine("Hello

world");world");world");world");

Hello World

import

System; import

System; import

System; import

System;

System.out.printlnSystem.out.printlnSystem.out.printlnSystem.out.println

Command Line Compiler

!^ Create source file:

Hello.cs.

!^ Invoke compiler:

csc

Hello.cs

!^ If OK,

Hello.exe

is created.

!^ Run executable:

Hello

C# Program Structure

!^ Namespaces

"^ Types, namespaces

!^ Type declarations

"^ Classes, interfaces, structs, enums, delegates

!^ Members

"^ Fields, methods, constants, indexers,properties events, operators, constructors, destructors

package

Java Beans

Void

Finalize

Void

Finalize

Void

Finalize

Void

Finalize

inner classes

namespace

N

{

// N

namespace

N

{

// N

namespace

N

{

// N

namespace

N

{

// N

class

C

{

// N1.C

class

C

{

// N1.C

class

C

{

// N1.C

class

C

{

// N1.C

class

C

{}

// N1.C1.C

class

C

{}

// N1.C1.C

class

C

{}

// N1.C1.C

class

C

{}

// N1.C1.C

}}}} namespace

N

{

// N1.N

namespace

N

{^

// N1.N

namespace

N

{

// N1.N

namespace

N

{^

// N1.N

class

C

{}

// N1.N2.C

class

C

{}

// N1.N2.C

class

C

{}

// N1.N2.C

class

C

{}

// N1.N2.C

}}}} } }}}

Namespace

= Packets Made Easy

using

System; using

System; using

System; using

System; namespacenamespacenamespacenamespace

MyMethodsMyMethodsMyMethodsMyMethods

public

class public

class public

class public

class

AddClassAddClassAddClassAddClass

public

static

long

public

static

long

public

static

long

public

static

long

AddAddAddAdd(long

i,^

long

j)^

(long

i,^

long

j)^

(long

i,^

long

j)^

(long

i,^

long

j)^

return(i+j);return(i+j);return(i+j);return(i+j);}}}} }}}} }}}} using^

System; using

System; using

System; using

System; namespacenamespacenamespacenamespace

MyMethodsMyMethodsMyMethodsMyMethods

public

class public

class public

class public

class

MultiplyClassMultiplyClassMultiplyClassMultiplyClass

public

static

long

public

static

long

public

static

long

public

static

long

MultiplyMultiplyMultiplyMultiply(long

i,^

long

j)^

(long

i,^

long

j)^

(long

i,^

long

j)^

(long

i,^

long

j)^

return(ij);return(ij);return(ij);return(ij);}}}} }}}} }}}}

Creating DLLs

csc/target:librarycsc/target:librarycsc/target:librarycsc/target:library////out:MyLibrary.DLLout:MyLibrary.DLLout:MyLibrary.DLLout:MyLibrary.DLLAdd.csAdd.csAdd.csAdd.cs

Mult.csMult.csMult.csMult.cs

Value Types

!^ primitives

"^ int i;

!^ enums

"^ enum State { Off, On }

!^ structs

"^ struct Point { int x, y; }

Stored "on the stack" or "in-line."

Enums

enumenumenumenum

Suit {Suit {Suit {Suit {Clubs = 0;Clubs = 0;Clubs = 0;Clubs = 0;Diamonds = 1;Diamonds = 1;Diamonds = 1;Diamonds = 1;Hearts = 2;Hearts = 2;Hearts = 2;Hearts = 2;Spades = 3;Spades = 3;Spades = 3;Spades = 3;

}}}} ………… Suit s =Suit s =Suit s =Suit s =

Suit.ClubsSuit.ClubsSuit.ClubsSuit.Clubs;

Console.WriteLineConsole.WriteLineConsole.WriteLineConsole.WriteLine

(s);(s);(s);(s);

Typesafe Enums in Java

public

class

Suit

public

class

Suit

public

class

Suit

public

class

Suit

private

final

private

final

private

final

private

final

intintintint

s;s;s;s;

privateprivateprivateprivate

Suit(intSuit(intSuit(intSuit(int

s)^

{s) {s) {s) {

this.sthis.sthis.sthis.s

=^ s;=^ s;=^ s;=^ s;

}}}} publicpublicpublicpublic

intintintint

tointtointtointtoint()

return

s;

return

s;

return

s;

return

s;

}}}} public

static

final

Suit

CLUBS

=^ new

Suit(0);

public

static

final

Suit

CLUBS

=^ new

Suit(0);

public

static

final

Suit

CLUBS

=^ new

Suit(0);

public

static

final

Suit

CLUBS

=^ new

Suit(0);

public

static

final

Suit

DIAMONDS

=^ new

Suit(1);

public

static

final

Suit

DIAMONDS

=^ new

Suit(1);

public

static

final

Suit

DIAMONDS

=^ new

Suit(1);

public

static

final

Suit

DIAMONDS

=^ new

Suit(1);

public

static

final

Suit

HEARTS

=^ new

Suit(2);

public

static

final

Suit

HEARTS

=^ new

Suit(2);

public

static

final

Suit

HEARTS

=^ new

Suit(2);

public

static

final

Suit

HEARTS

=^ new

Suit(2);

public

static

final

Suit

SPADES

=^ new

Suit(3);

public

static

final

Suit

SPADES

=^ new

Suit(3);

public

static

final

Suit

SPADES

=^ new

Suit(3);

public

static

final

Suit

SPADES

=^ new

Suit(3);

try {try {try {try {throw new Exception(“Oops!”);throw new Exception(“Oops!”);throw new Exception(“Oops!”);throw new Exception(“Oops!”);} catch (Exception e) {} catch (Exception e) {} catch (Exception e) {} catch (Exception e) {

… Handle exception ……;… Handle exception ……;… Handle exception ……;… Handle exception ……;

} finally {} finally {} finally {} finally {

… clean up, even if no exception occurred…;… clean up, even if no exception occurred…;… clean up, even if no exception occurred…;… clean up, even if no exception occurred…;

Exceptions Does not show up

in type ofMethods

(no ìthrowsî declaration)

class B : A {class B : A {class B : A {class B : A {public B (public B (public B (public B (int

intintint

x) : base (…,…,…) {x) : base (…,…,…) {x) : base (…,…,…) {x) : base (…,…,…) {

…………………………………………}}}} public B (public B (public B (public B (bool

boolboolbool

b) : this (…,…,…) {b) : this (…,…,…) {b) : this (…,…,…) {b) : this (…,…,…) {

…………………………………………}}}} public B (char c) {public B (char c) {public B (char c) {public B (char c) {…………………………………………}}}}

Constructors

The optional constructor-initializer Is invoked beforeexecuting the constuctorBody (default is

basebasebasebase

(^ )(^ )(^ )(^ )

class

X^ :
IB,
IC^

class

X^ :
IB,
IC^

class

X^ :
IB,
IC^

class

X^ :
IB,
IC^

void

()^ {

void

()^ {

void

()^ {

void

()^ {

Console.WriteLineConsole.WriteLineConsole.WriteLineConsole.WriteLine

("("("("IA.gIA.gIA.gIA.g");

void

()^ {

void

()^ {

void

()^ {

void

()^ {

Console.WriteLineConsole.WriteLineConsole.WriteLineConsole.WriteLine

("("("("IC.fIC.fIC.fIC.f");

void

()^ {

void

()^ {

void

()^ {

void

()^ {

Console.WriteLineConsole.WriteLineConsole.WriteLineConsole.WriteLine

("("("("IB.fIB.fIB.fIB.f");

IA.gIA.gIA.gIA.gIC.fIC.fIC.fIC.fIB.fIB.fIB.fIB.f

interface

IA^

interface

IA^

interface

IA^

interface

IA^

void

g^ (); void

g^ (); void

g^ (); void

g^ (); }}}}

interface

IB^
:^ IA

interface

IB^
:^ IA

interface

IB^
:^ IA

interface

IB^
:^ IA

void

f^ (); void

f^ (); void

f^ (); void

f^ (); }}}}

interface

IC^
:^ IA

interface

IC^
:^ IA

interface

IC^
:^ IA

interface

IC^
:^ IA

void

f^ (); void

f^ (); void

f^ (); void

f^ (); }}}}

Interfaces

X^ x

=^

new

X^

X^ x

=^

new

X^

X^ x

=^

new

X^

X^ x

=^

new

X^

if^

(x^

is^

IB)

if^

(x^

is^

IB)

if^

(x^

is^

IB)

if^

(x^

is^

IB)

IB^

b^ =

IB^

b^ =

IB^

b^ =

IB^

b^ =

(IB)xIB)xIB)xIB)x;

……………………………………………………} else

}^ else

}^ else

}^ else

as/is

X^ x

=^

new

X^

X^ x

=^

new

X^

X^ x

=^

new

X^

X^ x

=^

new

X^

IB^

b^ =

x^

as^

IB;

IB^

b^ =

x^

as^

IB;

IB^

b^ =

x^

as^

IB;

IB^

b^ =

x^

as^

IB;

if^

(b^

!=^

null)

if^

(b^

!=^

null)

if^

(b^

!=^

null)

if^

(b^

!=^

null)

…………………………………………} else

}^ else

}^ else

}^ else

as/is