APC 03 04 ObjectOrientation

Embed Size (px)

Citation preview

  • 7/27/2019 APC 03 04 ObjectOrientation

    1/28

    1

    Chair of Business Informatics andSoftware Engineering

    Prof. Dr. Stefan Eicker

    Prof. Dr. Stefan Eicker, Peter M. Schuler

    Winter Semester 2005

    Version 2.0

    Advanced Programming Concepts

    Objectoriented C#

  • 7/27/2019 APC 03 04 ObjectOrientation

    2/28

    2

    Object Terminology

    Based on: [BiHo2004, p. 31]

    a program

    types

    namespaces

    simple

    structured

    data member function member

    fields

    variables

    constants

    operators

    constructors

    methods

    properties

    consists of

    have

    are grouped in

    are

    are

    and

    arehave

    are int

    class or struct

    parameters

    e.g.

    e.g.

    indexers

    finalizers

    contain

    events

  • 7/27/2019 APC 03 04 ObjectOrientation

    3/28

    3

    Properties

    Methods that are dressed to look like a field get accessor takes no parameter and returns the same type as

    the property has been declared as

    No specification of explicit parameters for the set accesor Implement Read- and Write-Only Property by omitting the

    compliment property

    Note: C# does not permit to setting different access modifiers Properties can be declared as vi r t ual orabst r act

    publ i c st r i ng SomePr oper t y{

    get{

    r et ur n Thi s i s t he Pr oper t y val ue,}set{

    / / code t o set t he pr oper t y}

    }

    Source: [Robi2002, p. 117-119]

  • 7/27/2019 APC 03 04 ObjectOrientation

    4/28

    4

    Constructors

    Compiler will supply a default constructor, if explicitly noother constructor is implemented

    If you supply any constructor that takes parameters, then thecompiler will not automatically supply a default one

    Constructors can be defined as pr i vat e orpr ot ect ed

    It is possibly in C# to write a static no-parameter constructorfor a class (no access modifiers)

    publ i c cl ass MyCl ass

    {

    publ i c MyCl ass ( ) {}

    }

    Source: [Robi2002, p. 130-140]

  • 7/27/2019 APC 03 04 ObjectOrientation

    5/28

    5

    Readonly Fields

    Keyword r eadonl y

    You can assign values to a r eadonl y field inside a constructor

    A r eadonl y field can be an instance rather than a static field.

    publ i c cl ass Document{

    publ i c r eadonl y Dat eTi me Cr eat i onDat e;

    publ i c Document ( )

    {

    / / r ead i n creat i on dat e f r om f i l e. Assume r esul t

    / / i s 1 J an 2002 but i n gener al t hi s can be/ / di f f er ent f or di f f er ent i nst ances of t he cl ass

    Cr eat i onDat e = new Dat eTi me( 2002, 1, 1) ;

    }

    } Source: [Robi2002, p. 146-148]

  • 7/27/2019 APC 03 04 ObjectOrientation

    6/28

  • 7/27/2019 APC 03 04 ObjectOrientation

    7/28

    7

    Structs (2)

    newoperator works different for structs

    Calls the appropriate constructor, according to the parameters passed to it,

    and initializes all fields Legal to write:

    Di mensi ons poi nt ;

    poi nt . Lengt h = 3;

    poi nt . Wi dt h = 6;

    Everything must be initialized before use either with- the newoperator

    - or when values have been assigned to all of its fields

    Structs derive from System.Object

    - but a struct can not derive from any other class- and you can not inherit from as struct

    You are not permitted to define a constructor that takes no parameters

    It is not permitted to define a destructor

    Source: [Robi2002, p. 148-151]

  • 7/27/2019 APC 03 04 ObjectOrientation

    8/28

    8

    The Object Class

    Returns details of the type of the object.publ i cType Get Type( )

    Makes a shallow copy of the object.pr ot ect edobj ectMemberwi seCl one( )

    This is the .NET version of a destructor.pr ot ect ed vi r t ualvoi d Fi nal i ze( )

    Used if implementing dictionaries (hash

    tables).

    publ i c vi r t uali nt Get HashTabl e( )

    Compares instances of the object for

    equality.

    publ i c vi r t ualbool Equal s ( obj ectobj )

    Compares instances of the object for

    equality.

    publ i c st at i cbool Equal s ( obj ect

    obj A, obj ect obj B)

    Compares whether two references refer

    to the same object.

    publ i c st at i cbool Ref er enceEqual s( obj ect obj A, obj ectobj B)

    Returns a string representation of the

    object.

    publ i c vi r t ualst r i ng ToSt r i ng( )

    PurposeAccess ModifiersMethod

    Source: [Robi2002, p. 120]

  • 7/27/2019 APC 03 04 ObjectOrientation

    9/28

    9

    Access Modifiers

    The variable or method can only be accessed from

    within the type to which it belongs.

    pr i vat e

    The variable or method can be accessed from the

    current assembly, or from types derived from the current

    type (that is, from anywhere that could access it if it were

    declared as pr ot ect ed ori nt er nal ).

    pr ot ect ed i nt er nal

    The variable or method can only be accessed from

    within the type to which it belongs, or from types derivedfrom that type.

    pr ot ect ed

    The variable or method can be accessed from anywhereas a field of the type to which it belongs.

    publ i c

    The variable or method can only be accessed from the

    same assembly.

    i nt er nal

    DescriptionAccessibility

    Source: [Robi2002, p. 116]

  • 7/27/2019 APC 03 04 ObjectOrientation

    10/28

    10

    Interfaces

    Interfaces act as contracts

    A class, which derives from an interface, is declaring that it

    implements certain functions Not permitted to supply implementations of any of the

    members of an interface

    Interfaces can only obtain declarations of

    methods, properties, indexers and events Interface members are always public and cannot be

    declared as vi r t ual orst at i c

    publ i c i nt er f ace I di sposabl e{

    voi d Di spose( ) ;

    }

    Source: [Robi2002, p. 122-123]

  • 7/27/2019 APC 03 04 ObjectOrientation

    11/28

    11

    Constructor Initializer

    Special syntax in C# which allows to place code of different constructors inone place

    cl ass Car{

    pr i vat e st r i ng descr i pt i on;pr i vat e ui nt nWheel s;

    publ i c Car ( st r i ng model , ui nt nWheel s){t hi s. descr i pt i on = descr i pt i on;t hi s. nWheel s = nWheel s;

    }

    publ i c Car ( st r i ng model ) : t hi s( model , 4){}

    }Source: [Robi2002, p. 134]

  • 7/27/2019 APC 03 04 ObjectOrientation

    12/28

    12

    Constructors (2)

    Constructors are called in Order ofSyst em. Obj ect

    Then progressed down the hierarchy until it has reached theclass being instantiated

    Each constructor handles initializations of the fields in his own

    class

    Working up the hierarchy a constructor for the derived classcan call up base class methods, properties and any other

    members

    The base and this keywords are the only keyword allowed in

    the line which calls another constructor

    Source: [Robi2002, p. 130-140]

  • 7/27/2019 APC 03 04 ObjectOrientation

    13/28

    13

    Call Order for Constructors

    Source: [Robi2002, p. 439.]

    Syst em. Obj ect

    MyType

    MyAdvType

    Publ i c Mat ( )

    {

    Wer t = 0;

    }

    Wer t

  • 7/27/2019 APC 03 04 ObjectOrientation

    14/28

    14

    Classes and Inheritance

    Source: [Robi2002, p. 110]

    Classes are reference types

    cl ass MyCl ass{

    pr i vat e i nt someFi el d;

    publ i c st r i ng SomeMet hod( bool par amet er s) {}

    }

    Instantiate an object using the newoperator

    MyCl ass my Obj ect ;

    myObj ect = new MyCl ass( ) ;

  • 7/27/2019 APC 03 04 ObjectOrientation

    15/28

    15

    Single Implementation Inheritance

    A class must derive from one another class

    Universal base class derives from Syst em. Obj ect

    A class may derive from another specific class

    cl ass MyDer i vedCl ass : MyBaseCl ass

    {

    }

    Source: [Robi2002, p. 110-111]

  • 7/27/2019 APC 03 04 ObjectOrientation

    16/28

    16

    Method Overloading

    Methods may be implemented in several versions

    Requirement: signatures differ at least in one the following- Name

    - Number of parameters

    - Parameter types

    Signatures that differ only- In their return type

    - By virtue of parameter (r ef orout )

    are not sufficient!voi d Di spl ayResul t ( st r i ng r esul t ) {}

    voi d Di spl ayResul t ( i nt resul t ) {}

    Source: [Robi2002, p. 111-112]

  • 7/27/2019 APC 03 04 ObjectOrientation

    17/28

    17

    Method Overriding

    A base class must be declared is vi r t ual in order to be

    overridden

    Neither member fields nor static functions can be declared asvi r t ual

    publ i c over r i de st r i ng Vi r t ual Met hod( ) {}

    Source: [Robi2002, p. 112]

  • 7/27/2019 APC 03 04 ObjectOrientation

    18/28

    18

    Method Hiding

    A method is declared in both base and derived classes and notdeclared as vi r t ual and over r i de

    derived class version hides the base class version

    cl ass MyDer i vedCl ass : Hi sBaseCl ass

    {

    publ i c i nt MyMet hod( ) {}

    }

    Use the keyword new to declare that you intend to hide a method

    publ i c new i nt MyMet hod( ) {}

    To call a base version of a method from a derived class

    base. Cal cul at ePr i ce( ) ;

    Source: [Robi2002, p. 112-114]

  • 7/27/2019 APC 03 04 ObjectOrientation

    19/28

  • 7/27/2019 APC 03 04 ObjectOrientation

    20/28

    20

    Sealed Classes and Methods

    You can not inherit from a sealed class

    You can not override a sealed method any further

    seal ed cl ass Fi nal Cl ass

    {publ i c seal ed voi d Fi nal Met hod( ) {}

    }

    Source: [Robi2002, p. 115-116]

  • 7/27/2019 APC 03 04 ObjectOrientation

    21/28

    21

    Chair of Business Informatics and

    Software EngineeringProf. Dr. Stefan Eicker

    Prof. Dr. Stefan Eicker, Peter M. SchulerWinter Semester 2005

    Version 2.0

    Advanced Programming Concepts

    Objectoriented C#

  • 7/27/2019 APC 03 04 ObjectOrientation

    22/28

    22

    Agenda

    Operator Overloading

    Type Casts

    Indexer

  • 7/27/2019 APC 03 04 ObjectOrientation

    23/28

    23

    Operator Overloading

    Operators like +, - or * are strictly implemented for thepredefined data types

    If you want to use the operators for your own classes, youhave to use overloads for the operators

    publ i c st at i c Vect or oper at or + ( Vect or l hs, Vect or r hs){

    Vect or r esul t = new Vect or ( l hs) ;

    r esul t . x += r hs. x;

    r esul t . y += r hs. y;

    r esul t . z += r hs. z;r et ur n r esul t ;

    }

    Source: [Robi2002, p. 151-161]

  • 7/27/2019 APC 03 04 ObjectOrientation

    24/28

    24

    Which Operators Can You Overload?

    none+, - , ++, - -Arithmetic unary

    none&, | , , Bitwise binary

    none! , ~, t r ue, f al seBitwise unary

    must be overloaded in pairs==, ! =, >=,

  • 7/27/2019 APC 03 04 ObjectOrientation

    25/28

    25

    Indexers

    Are not an essential part of object-oriented programming.

    Name of the indexer is the keyword t hi s, other then that it is

    quite equal to properties in terms of their definition.

    Source: [Robi2002, p. 162-165]

  • 7/27/2019 APC 03 04 ObjectOrientation

    26/28

    26

    Indexers (2)

    st r uct Vect or{

    publ i c doubl e x, y, z ;

    publ i c doubl e t hi s [ i nt i ]

    {get{

    swi t ch ( i ){

    case 0:r et ur n x;

    case 1:r et ur n y;

    case 2:r et ur n z;

    def aul t :t hr ow new I ndexOut Of RangeExcept i on( "At t empt t or et r i eve Vect or el ement " + i ) ;

    }}

    Source: [Robi2002, p. 162-163]

  • 7/27/2019 APC 03 04 ObjectOrientation

    27/28

    27

    Indexers (3)

    set

    {

    switch (i)

    {

    case 0:x = value;

    break;

    case 1:

    y = value;

    break;

    case 2:z = value;

    break;

    default:

    throw new IndexOutOfRangeException(

    "Attempt to set Vector element " + i);

    }

    }

    }

    Source: [Robi2002, p. 162-163]

  • 7/27/2019 APC 03 04 ObjectOrientation

    28/28

    28

    Comparision of Property and Indexer

    Quelle: [MSDN2003]

    A set accessor of an indexer has the

    same formal parameter list as the

    indexer, in addition to the valueparameter.

    A set accessor of a property contains the

    implicit value parameter.

    A get accessor of an indexer has thesame formal parameter list as the

    indexer.

    A get accessor of a property has noparameters.

    Must be an instance member.Can be a static or an instance member.

    Accessed through an element access.Accessed through a simple name or a

    member access.

    Identified by its signature.Identified by its name.

    IndexerProperty