CSCI 3328 Object Oriented Programming in C Chapter

  • Slides: 17
Download presentation
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper

CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539 xiang. [email protected] edu 1

Objectives • In this chapter, you will – Learn some examples of classes and

Objectives • In this chapter, you will – Learn some examples of classes and objects – Know how to use keywords: this, static, readonly – Get familiar with the concept of data encapsulation and data hiding 2

Example: Time Class • Contains 3 int variables declared as private • Also, 3

Example: Time Class • Contains 3 int variables declared as private • Also, 3 public methods: Set. Time, To. Universal. String and To. String • Does not have a constructor • Instead time is passed in using public Set. Time, where three integers are validated to be within range, if not 0 is assigned • When constructor is not defined, the compiler adds a default one, assigning 0’s to all three int variables 3

Code for Time 1 Class public class Time 1 { private int hour; private

Code for Time 1 Class public class Time 1 { private int hour; private int minute; private int second; public void Set. Time(int h, int m, int s) { hour = ((h>=0 && h<24)? h : 0); minute = ((m>=0 && m<60)? m : 0); second = ((s>=0 && s<60)? s : 0); } public string To. Universal. String() { return string. Format("{0: D 2}: {1: D 2}: {2: D 2}", hour, minute, second); } public override string To. String() { return string. Format("{0}: {1: D 2}: {2: D 2} {3}", ((hour == 0|| hour == 12) ? 12 : hour%12), minute, second, (hour<12) ? "AM" : "PM")); } 4 }

Example: Time Class (cont'd) • Method To. Universal. String returns a string in universal-time

Example: Time Class (cont'd) • Method To. Universal. String returns a string in universal-time format. – Return string. Format("{0: D 2}: {1: D 2}: {2: D 2}", hour, minute, second); – //like 13: 27: 06 • Method To. String() overrides string – Return string. Format ("{0: D 2}: {1: D 2}: {2: D 2}", ((hour==0||hour==12)? 12: hour%12)); – // if hour is 0 or 12 it appears as 12 AM, or PM, else it appears from 1 to 11, if hour is <12 it is AM. 5

Invoke Methods of Time Object • Time 1 time = new Time 1(); •

Invoke Methods of Time Object • Time 1 time = new Time 1(); • Console. Write. Line("The initial universal time is" + time. To. Universal. String()); – 00: 00 • Console. Write. Line("The initial universal time is" + time. To. String()); – 12: 00 AM • time. Set. Time (13, 27, 6); – 13: 27: 06 – 1: 27: 06 PM • time. Set. Time(99, 99); – 00: 00 – 12: 00 AM 6

Indexers • The indices can be either integer (as conventional arrays) or non-integer such

Indexers • The indices can be either integer (as conventional arrays) or non-integer such as the data element name • Indexers are defined like properties in a class – See the example 7

class Int. Indexer { private string[] my. Data; public Int. Indexer(int size) { my.

class Int. Indexer { private string[] my. Data; public Int. Indexer(int size) { my. Data = new string[size]; for (int i = 0; i < size; i++) { my. Data[i] = "empty"; } } public string this[int pos] { get { return my. Data[pos]; } set { my. Data[pos] = value; } } 8

static void Main(string[] args) { int size = 10; Int. Indexer my. Ind =

static void Main(string[] args) { int size = 10; Int. Indexer my. Ind = new Int. Indexer(size); my. Ind[0] = "Artem Chebotko"; my. Ind[1] = "Robert Scheweller"; my. Ind[2] = "Pearl Brazier"; my. Ind[3] = "Laura Grabowski"; my. Ind[4] = "John Abraham"; my. Ind[5] = "Emmet Tomai"; my. Ind[6] = "Yang Liu"; my. Ind[7] = "Bin Fu"; my. Ind[8] = "Xiang Lian"; Console. Write. Line("n. Indexer Outputn"); for (int i = 0; i < size; i++) { Console. Write. Line("my. Ind[{0}]: {1}", i, my. Ind[i]); } Console. Read. Key(); } 9

Keyword: this public class Simple. Time { private int hour; private int minute; private

Keyword: this public class Simple. Time { private int hour; private int minute; private int second; public void Set. Time(int hour, int minute, int second) { this. hour = hour; this. minute = minute; this. second = second; } pubic void Reset() { this. Set. Time(0, 0, 0); } } 10

More Keywords: readonly • private readonly int INCREMENT; • By convention, readonly variables are

More Keywords: readonly • private readonly int INCREMENT; • By convention, readonly variables are with capital letters like constants • Constructors can initialize readonly variables for multiple times 11

More Keywords: static • private static int count = 0; • Maintain the number

More Keywords: static • private static int count = 0; • Maintain the number of objects for a class • Static variables belong to the class, not objects – Static variables can be shared by multiple objects • Static variables can be referred by properties public static int Count { get {return count; } } 12

Encapsulation • Let the program hide some of the data and operation of a

Encapsulation • Let the program hide some of the data and operation of a class while exposing others – private • Implementation of a method is hidden from the user, so it appears like a black box • Think of a person driving a car – He does not need to know the internal working of the engine or the way gear changes work, to be able to drive the car (Encapsulation) – Instead, he needs to know things such as how much turning the steering wheel needs, etc. (Abstraction) 13

Inheritance • A new class is created by absorbing an existing class’s members and

Inheritance • A new class is created by absorbing an existing class’s members and enhancing them with new or modified capabilities. • Base class : the existing class from which a new class inherits members. • Derived class : the new class that inherited from the base class. Each new class can become the base class for a future derived class. Is-a relationship and has-a relationship. Is-a represents inheritance. Has-a represents a composition. • Inheritance allows you to reuse code 14

Inheritance (cont'd) public class Parent. Class { public Parent. Class() { Console. Write. Line("Parent

Inheritance (cont'd) public class Parent. Class { public Parent. Class() { Console. Write. Line("Parent Constructor. "); } public void print() { Console. Write. Line("I'm a Parent Class. "); } } public class Child. Class : Parent. Class { public Child. Class() { Console. Write. Line("Child Constructor. "); } public static void Main() { Child. Class child = new Child. Class(); child. print(); Console. Read. Key(); } } 15

Polymorphism • Enables you to “program in general” than “program in specific” • Enables

Polymorphism • Enables you to “program in general” than “program in specific” • Enables us to write applications that process objects that share the same base class in a class hierarchy as if they were all objects of the base class • It allows you to invoke derived class methods through a base class reference during run-time • overrides means they provide their own definition and implementation. – At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method – Thus in your source code you can call a method on a base class, and cause a derived class's version of the method to be executed 16

17

17