Top Banner
C# Classes and Inheritance CNS 3260 C# .NET Software Development
32

C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Dec 24, 2015

Download

Documents

Brooke Ryan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

C# Classes and Inheritance

CNS 3260C# .NET Software Development

Page 2: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Questions for Project 2 does the params array load the integers in sequentially

(left to right, top to bottom)? yes

Can you overload operator= or operator new? no

Can I provide multiple overloads for one operator? yes (and you should)

do I need to allow for any order of for the scalar operations (int + Matrix and Matrix + int)

It’s easy to do, but I only expect you to do one order (doesn’t matter to me which one it is)

Page 3: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Points to review Immutable strings Inheritance Virtual

Page 4: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Immutable strings Immutable means the string does not change when an

operation is performed on it. How do you change a string? After the Replace function what does str contain?

“Welcome” To do it right you must reassign to str.

str now contains: “We-me”

string str = “Welcome”;str.Replace(“lco”, “-”);

string str = “Welcome”;str = str.Replace(“lco”, “-”);

Page 5: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Immutable strings

static void Main(){ string s1 = "original"; ChangeMyString(s1);}

static void ChangeMyString(string str){ str = "changed";}

Passing an immutable string to a function

After calling ChangeMyString(string str), s1 contians “original”

Page 6: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Immutable strings (again)

static void Main(){ string s1 = "original"; ChangeMyString(ref s1);}

static void ChangeMyString(ref string str){ str = "changed";}

Passing an immutable string to a function

After calling ChangeMyString(ref string str), s1 contians “changed”

Page 7: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Access specifiers public: anyone can access protected: only the class and derived classes can access private: only the class can access

Page 8: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Review Overriding vs. Hiding

public class Base{

protected void fun1(){}protected virtual void fun2(){}

}

public class Derived : Base{

protected new void fun1(){}protected override void fun2(){}

}

Hides base.fun1()

Overrides base.fun1()

Page 9: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Overriding

static void Main(){

Base b = new Derived();

b.fun1();

b.fun2();}

Calls Base.fun1()

Calls Derived.fun2()

The vtable is used during runtime to resolve virtual function calls.

Page 10: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Overloading a method Overloading vs. Overriding or hiding

Overriding: a derived class method covers (you can override or hide a base class method with a method of the same name.)

Overloading: Creating methods of the same name that take a different set of parameters

C# allows no default parameters You may change access specifiers on various

overloaded methods You cannot overload on the return type alone You can overload a base class method in a derived class

Only if the signatures match does it hide the base class method

Page 11: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Overloading Example

public string GetValue(string str){ // do work here}

public float GetValue(float f){ // do work here}

public string GetValue(string str, int index){ // do work here}

Page 12: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Design consideration

public Token GetToken(){ // do some work here return nextToken;}

public Token GetToken(string name){ // don't duplicate work if you can help it Token t = GetToken(); if(t.Name != name) throw(new UnexpectedTokenException());

return t;}

Avoid duplicating code:

Page 13: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Properties Look like variables Smell like variables Taste like vegetables? Used like variables BUT are really methods

getters -- accessors setters -- mutators

Page 14: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Property Exampleclass MyPropDemo{

private int myValue;

public int MyValue{

get{ return myValue; } //accessorset{ myValue = value; } //mutator

}}

Page 15: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

The value keyword Implicit variable passed to properties and indexers

Type is the same as the return type of the property or indexer

May be used as a local-variable name or a field

class MyPropDemo{

private int myValue;

public int MyValue{

set { myValue = value; } // mutatorget { return myValue; } // accessor

}}

type is int

Page 16: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Properties Method in disguise Allows us to change object state Syntactical sugar Allows to control access outside the class:

class MyPropDemo{

private int myValue;

public int MyValue{

set { myValue = value; } // mutatorget { return myValue; } // accessor

}}

Page 17: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Recursive Properties Be careful with syntax not to recall the property

from within the property

class MyClass{ private int myClassInt; public int MyClassInt { get { return MyClassInt; // Uh oh } set { myClassInt=value; } }}

Page 18: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

virtual, abstract, static? yes, yes and yes:

public virtual int MyInt{

get { return myInt; }set { myInt=value; }

}

abstract public int MyInt{

get;set;

}

public static int MyInt{

get { return myStaticInt; }set { myStaticInt=value; }

}

Page 19: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Why not just use public variables?

You can have extra functionality in a property if needed:

public int MyInt{ set { if(value < 0) throw(new SomeExceptionThatMakesSenseHere("Duh"));

DoSomeWork(value); DontEvenHaveToInitializeAVariableIfYouDontWant(); } get{ return myInt; }}

Page 20: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Why not just use public variables?

Can have just a get or just a set:

public int Count{ get{return count;}}

Page 21: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Why not just use public variables?

Allows you to protect data in a multi-threaded situation:

public int MyInt{ set { lock(this) { myInt=value; } }}

Page 22: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Why not just use public variables?

Allows you to disguise complex lookups as simple or foreign variables as your own:

private int Count{ get { return ((SomeObject as SomeOtherObject)[i, j] as object).Container.Count; }}

Page 23: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

What’s faster

(See PropertyVSMemberSpeedTest demo)

Page 24: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

New in version 2.0? standard 2.7 working draft p292 allows for changing the

access specifier on the get or set or both:

class MyPropDemo{

private int myValue;

public int MyValue{

get{ return myValue; } //accessorprotected set{ myValue = value; } //mutator

}}

Page 25: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

A quick look at arrays Arrays are stored in contiguous memory Arrays are objects Multidimensional arrays are either jagged or rectangular Single dimensional array syntax:

All elements are zeroed (nulled for reference types)

int[] intArray = new int[10];

Page 26: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Jagged Arrays

int[][] intArray = new int[10][];

for(int i=0; i<10; ++i)intArray[i] = new int[15+i];

An array of arrays 2nd rank can be an array of any length

Works the same for n dimensions Index into the array:

intArray[3][5] = 100;

Page 27: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Rectangular Arrays

2nd rank are all of the same length

Works the same for n dimensions

Index into the array:

int[,] intArray = new int[10,15];

int[,,,] intArray = new int[10,15,6,5];

intArray[3,5] = 100;intArray[4,5,3,2] = 200;

Page 28: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Indexers

class MyIndexDemo{

private int[] iArray = {10,20,30,40,50};

public int this[int index] {

get{ return iArray[index]; }set{ iArray[index] = value; }

}}

Similar to operator[] in C++ Similar to properties in C#

Page 29: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Indexer Allows array-like access into a class Good if you want you’re class to work as a collection Can have get and set, or just one or the other Can be overloaded Can’t have ref or out parameters

Page 30: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Overloading an indexer

class MyClass{ System.Collections.Hashtable table = new System.Collections.Hashtable();

public object this[int i] { get { return table[i]; } set { table[i] = value; } }

public object this[string i] { get { return table[i]; } set { table[i] = value; } }}

Page 31: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Jagged-style brackets

class Class2{ private int[][] int2DArray = new int[10][];

public int[] this[int index] { get { return int2DArray[index]; } set { int2DArray[index] = value; } }

void Main() { Class2 c = new Class2(); c[4][5] = 100; }}

Page 32: C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Rectangular-style Indexer

class Class3{ private int[,] int2DRectangularArray = new int[10, 10];

public int this[int x, int y] { get { return int2DRectangularArray[x, y]; } set { int2DRectangularArray[x, y] = value; } }

void Main() { Class3 c = new Class3(); c[4, 5] = 100; }}