Anda di halaman 1dari 25

Chapter 4: OOP with C# 2.

0
Chapter 6: Exception Handling

1 /59
Chapter 4: OOP with C# 2.0

2 /59
 Reviewing OOP Concepts.
 OOP - Encapsulation.
 OOP - Inheritance.
 OOP - Polymorphism.
 C# Casting Rules
 Partial Types

3 /59
Procedure-Oriented Program
Class A
{
data1
data1
data2 Modifiers Function1 ()
Function1 (data1)
Function2 ()
Function2 (data1) }
Function3 (data2)

Function4 (data2)
Class B
{
data2
Object = Data + Methods
Modifiers Function3 ()

Basic Concepts Function4()


- Encapsulation Particular }
- Inheritance methods:
- Polymorphism Constructors

4 /59
 Aggregation of data and behavior:
• Data of a class should be hidden from the outside.

• All behavior should be accessed only via


methods.
• A method should have a boundary condition (a
limit on the range of arguments for which a
method ca operate properly).
Ex: a square-root cannot operate on a negative number

5 /59
Using
accessor traditional
accessor and
mutator
Mutator

6 /59
Using
Properties

the C# value token is not a keyword,


but rather a contextual keyword

7 /59
 Ability allows a class having members of an existed class 
Re-used code.
class PERSON class STUDENT

ID_Num ID_Num
Name Name
YearOfBirth inherited YearOfBirth
Address Address
getID_Num() getID_Num()
setID_Num(newID) setID_Num(newID)
...... ......
“is a”
relationship
RollNum
class STUDENT Score
extensions
getScore()
RollNum setSore(newScore)
Score ......
getScore()
setSore(newScore)
...... Son = Father + extensions
8 /59
Inheritance Implementation in C#

Invoke parent’s
constructor

Invoke parent’s
constructor

9 /59
 Ability allows many versions of a method based on overloading
and overriding methods techniques.

10 /59
Polymorphism Implementation in C#
virtual : provide a default implementation. Can
be overridden if necessary

abstract: sub-classes MUST override

Optional

Must

??
Besides virtual and abstract, C# provide new
keyword that applies to methods. What does
that mean? 11 /59
Value type: conversion and casting

Convert smaller type to bigger type: OK


(also called implicit cast)

Convert bigger type to smaller type: NOT


OK => need an explicit cast => may cause
loss of data

Lost data

Reference type: conversion and casting


Convert sub-class to supper class: OK
(also called implicit cast)

Convert supper class to sub-class: NOT OK


=> need an explicit cast => may cause run
time error
12 /59
casting

13 /59
Same class name

14 /59
15
Chapter 6: Exception Handling

16 /59
 Errors,Bugs, Exceptions
 Catching Exception: try – catch – finally
 Exception Propagation

17 /59
 Bugs: an error on the part of the programmer.
• For example, making calls on a NULL pointer

 User errors: typically caused by the individual running


the application
• For example, an end user who enters a malformed string into a
text box
 Exceptions: run time error and beyond the control of
the program

18 /59
 When an exception occurs, the program will terminate
abruptly
Example Exception
message

Exception
details

19 /59
try
try block {
< statements may cause exceptions >
}
false
e? catch ( ExceptionType1 e1 )
{
true
< statements handle the situation 1>
catch block }
catch ( ExceptionType2 e2)
{
< statements handle the situation 2>
finally block }
finally
{
< statements are always executed >
}

20 /59
Exception handling example

21 /59
catch(...)
Stack for A() A()
Stack for B()
B()
Stack for C()
Stack for D() C()

Stack trace D() Exception

22 /59
Exception handling : stacktrace example

23 /59
24
Thank you

25

Anda mungkin juga menyukai