160 likes | 246 Views
C# Lesson 7. Introduction to Classes. Objectives. Implement Constructors. Know the difference between instance and static members. Understand Destructors. Familiarization with Class Members. Classes.
E N D
C# Lesson 7 Introduction to Classes CIS 330
Objectives • Implement Constructors. • Know the difference between instance and static members. • Understand Destructors. • Familiarization with Class Members. CIS 330
Classes • Classes are declared by using the keyword class followed by the class name and a set of class members surrounded by curly braces • Every class has a constructor that initializes class members when the class is created • Constructors do not have return values and always have the same name as the class CIS 330
Classes (cont) class OutputClass { string myString; // Constructor public OutputClass(string inputString) { myString = inputString; } // Instance Method public void printString() { Console.WriteLine("{0}", myString); } // Destructor ~OutputClass() { // Some resource cleanup routines }} CIS 330
Classes (cont) • In C#, there are two types of class members • Instanceclass members belong to a specific occurrence of a class • Static memberscan be accessed simply by using the syntax <classname>.<static class member>. There is only ever one copy of a static class member. Use static constructor to initialize static fields in a class • Destructors look just like constructors, except they start with a tilde, "~". They are normally called by the C# garbage collector. CIS 330
Class Member Types • Constructors • Destructors • Fields • Methods • Properties • Indexers • Delegates • Events • Nested Classes CIS 330
C# Lesson 8 Class Inheritance CIS 330
Objectives • Implement Base Classes. • Implement Derived Classes. • Initialize Base Classes from Derived Classes. • Learn How to Call Base Class Members. • Learn How to Hide Base Class Members. CIS 330
Inheritance Example public class ParentClass{ public ParentClass() { Console.WriteLine("Parent Constructor."); } public void print() { Console.WriteLine("I'm a Parent Class."); }}public class ChildClass : ParentClass{ public ChildClass() { Console.WriteLine("Child Constructor."); } public static void Main() { ChildClass child = new ChildClass(); child.print(); }} CIS 330
Inheritance • Can create a child class ChildClass, using existing code from ParentClass • This is accomplished through the ChildClass declaration • public class ChildClass : ParentClass • C# supports single class inheritance only. However, it does allow multiple interface inheritance • ChildClass has exactly the same capabilities as ParentClass CIS 330
Inheritance (cont) • Can access base class members • prefixing the method name with "base.“ • through an explicit cast ((Parent)child).print(); • The new modifier on a Child class enables this method to hide the Parent class method, thus explicitly preventing polymorphism CIS 330
C# Lesson 9 Polymorphism CIS 330
Objectives • Learn What Polymorphism Is. • Implement a Virtual Method. • Override a Virtual Method. • Use Polymorphism in a Program. CIS 330
Polymorphism Example public class DrawingObject{ public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); }} public class Line : DrawingObject{ public override void Draw() { Console.WriteLine("I'm a Line."); }}public class Circle : DrawingObject{ public override void Draw() { Console.WriteLine("I'm a Circle."); }} CIS 330
Polymorphism Implemented using System;public class DrawDemo{ public static int Main( ) { DrawingObject[] dObj = new DrawingObject[4]; dObj[0] = new Line(); dObj[1] = new Circle(); dObj[2] = new Square(); dObj[3] = new DrawingObject(); foreach (DrawingObject drawObj in dObj) { drawObj.Draw(); } return 0; }} CIS 330
Summary • Polymorphism. It allows you to implement derived class methods through a base class pointer during run-time • Implemented a derived class method that overrides a virtual method CIS 330