C# Classes and objects

71 downloads 260 Views 203KB Size Report
C#.net Classes and objects. 2. Definitions. • Class. – creation pattern for objects. – mold for casting objects. – meta-object. – exists at compile-time. • Object.
C# Classes and objects VT 2009

C#.net Classes and objects

1

Definitions • Class – creation pattern for objects – mold for casting objects – meta-object – exists at compile-time

• Object

class myC

myC myObj

– instance of a class – has all props and ops defined in its class • including inherited ones C#.net Classes and objects

2

Definitions (cont’d) • Interface – in principal a purely abstract class – used for “can-do” instead of “is-a” relationships. – removes most needs for multiple inheritance

• this – reference to current object

C#.net Classes and objects

3

Object Relationships • ”Has-a”-relationship – value-type field  containment – reference-type field  association

• Retrieving containment data from a class – copy of data must be made

• Retrieving associated data from a class – copy of reference must be made – handle or object ID C#.net Classes and objects

4

Four categories of methods • constructors – creates new instances of a class

• destructors – removes/destroys instances of a class

• transformers – affect the state of the object

• accessors – reads (parts of) the state of the object

• (predicates) – boolean questions regarding the state of the object C#.net Classes and objects

5

Four categories - example • • • •

constructors – Stack() // creates new stack with default size – Stack(int size) // creates new stack with size as size

destructor – // handled by the system. Deallocation of memory, clean-up procedures

transformers – push(anElement) // adds an element to the top of the stack – pop() // removes topmost element from stack

accessors – top() // reads topmost element off of stack – isEmpty() //answers the question whether the stack is empty or not – isFull() //answers the question whether the stack is full or not

C#.net Classes and objects

6

Classes - syntax • attributes class-modifiers partial class identifier typeparameter-list class-base type-parameter-constraints-clauses class body; – attributes (opt) • user or system defined attributes of the class

– class-modifiers (opt) • new, public, protected, internal, private, abstract, sealed

– partial (opt) – class-base (opt)

• superclass, interface

– type-parameter-list – type-parameter-constraints-clauses – class body • implementation C#.net Classes and objects

7

Classes - generics • Allows container classes to be instantiated with a particular type at compile time • More type-safe than conventional object-based solutions • Instead of class Container() … public Object getData();

• you get class Container () … public Type-Identifier getData(); C#.net Classes and objects

8

Classes – generics (cont’d) • where keyword allows for specific properties to be present for generic T • public class myClass: where T: iComparable – prevents myClass to be instantiated with T that does not support iComparable interface

• public class myClass: where T new() – makes sure that T has new method with zero parameters C#.net Classes and objects

9

Abstract classes • Represent ideas or concepts – what are shapes? – what are mammals?

• Use of abstract classes by inheritance only – no direct object instances – access to derived classes often done using polymorphism

• Abstract classes can have abstract methods – Forces implementation by derived classes

• Opposite of an abstract class is a sealed class – rarely used C#.net Classes and objects

10

Abstract class syntax • public abstract class shape • { – private double x, y; – public abstract double Area {get;}

• } • public class Circle:

C#.net Classes and objects

11

Fields • Fields can be instantiated in more than one way – constructor – at definition

• private

class Fields { public Fields(int x, int y) { this.x=x; // ok y=y; // ok } private int x; // ok private int y=5; // ok }

– accessible only to class itself

• static – accessible only at class level, not at object level. • myClass.X is ok • myObject.X is not C#.net Classes and objects

class myClass { public static int X=42; } … myClass myObject = new myClass(); 12

Fields (cont’d) • Automatic instantiation – int  0 – float  0.0 – bool  false – reference  null – same for non-static as for static fields.

C#.net Classes and objects

13

Fields - readonly • readonly keyword prohibits writes unless in constructor or initialization class myClass { public static readonly int z = 420; public myClass(int x, int y) { this.x=x; this.y=y; } private readonly int x, y; } C#.net Classes and objects

14

Fields - const • Const fields are static by default • Initialized at creation • Only simple types can be const – int – string – float – other numeric formats class X { C#.net Classes and objects public const string = ”X is my name”; }

15

Static constructors • Can initialize static fields but not const fields sealed class Pair { public static readonly Pair Origin; public Pair(int x, int y) { this.x = x; this.y = y; } static Pair() { Origin = new Pair(0,0); } private intC#.net x, Classes y; and objects }

16

Class declaration - example public class Document { private string contents; private string author; public Document(string author, string contents) { this.author = author; this.contents = contents; } public string Author { get { return author; } } public string Contents { get { return contents; } } } C#.net Classes and objects

17

Class declaration – Bank Account public class BankAccount { private decimal balance; private ulong acctnum = 0; public decimal Balance { get { return balance; } } // deposit: deposites amount on account // pre: amount >= 0 // post: amount has been increased by amount public void deposit(decimal amount) { balance = balance + amount; } // withdraw: removes money from account // pre: amount