Download presentation
Presentation is loading. Please wait.
1
Inheritance & Polymorphism
2
Specialization & Generalization
The is-a relationship The dog is a mammal The cat is a mammal Dog and cat specialize mammal Mammal generalize dog and cat To extract the common parts In C#, the specialization relationship is typically implemented using inheritance.
3
Example Inheritance_demo Use : for inheritance Access modifiers
Private, protected, public Override inherited methods new vs virtual-override Overload vs overwrite Observe the which method is called
4
Exercise 13 Please develop a Dog and a Cat class derives from mammal class and specialize them respectively The mammal class is given as the next slide
5
class Mammal { protected string speciesName = "Mammal"; public Mammal(string speciesName) this.speciesName = speciesName; } public void Sound() Console.WriteLine("The sound of {0} => hmm...hmm...", speciesName);
6
The root of all classes - Object
Every thing is an object of Object in C# !!
7
Example (5-4) bad naming
8
Exercise 14 What will the message be if you don’t override the toString() in example 5-4? To understand why and when we need to do this.
9
Polymorphism The way you look at things Dog aDog = new Dog();
A dog is just a dog A dog is a mammal That dog is a hound Dog aDog = new Dog(); Mammal aMammal = (Mammal) aDog; ex13
10
Boxing & Un-Boxing The conversion between value types and reference types stack vs heap Boxing is implicit Un-Boxing is explicit
11
Boxing & Un-Boxing (5-5) implicit explicit
12
Interfaces An interface is a contract that guarantees to a client how a class will behave Doing nothing but putting a constraint on the implementing class Ex:
13
Example 8-1 Constructor Methods Property
14
Arrays An array is an indexed collection of objects, all of the same type Phone book (exercise 10) myFriend1, myFriend2, … , myFriend100 ? Expression :
15
Example (ArrayDemo) The phone book exercise can be improved further
Array size static void Main(string[] args) { PersonalData[] myFriends = new PersonalData[2]; myFriends[0] = new PersonalData(); myFriends[0].Name = "Mandy Tsai"; myFriends[0].Phone = " "; myFriends[0].display(); PersonalData.Counter++; myFriends[1] = new PersonalData("Claire"," "); myFriends[1].display(); } Array indexer
16
Exercise 15 Please try to declare and initialize an integer array from 0 to 100 myArray[0] = 0; myArray[1] = 1; … myArray[100] = 100; and prints them out …
17
Some useful stuff Length property System.Array.Sort()
myArray.Lngth System.Array.Sort() System.Array.Sort(myArray,0,101); System.Array.Reverse() System.Array.Reverse(myArray,0,101); The foreach loop foreach(int i in myArray)
18
Multi-dimensional array & ArrayList
You can cast a multi-dimensional array simply by int[,] myArray = new int[6,8]; ArrayList is used to provide you an array with dynamic size Using System.Collections; Add(), Insert() Remove(), RemoveAt() Count (property)
19
Example (ex15_modified_2)
ArrayList myArray = new ArrayList(); for(int i = 0; i < 101; i++) myArray.Add(i); { if(i != 0 && i%10 == 0) Console.WriteLine(); Console.Write(myArray[i]+"\t"); } Console.WriteLine("\n\n"); It is not necessary to provide the size of your array Add values into our array
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.