Download presentation
Presentation is loading. Please wait.
Published byAbner Lynch Modified over 9 years ago
1
CIS 3301 C# Lesson 7 Introduction to Classes
2
CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand Destructors. Familiarization with Class Members.
3
CIS 3303 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
4
CIS 3304 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 } }
5
CIS 3305 Classes (cont) In C#, there are two types of class members –Instance class members belong to a specific occurrence of a class –Static members can be accessed simply by using the syntax.. 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.
6
CIS 3306 Class Member Types Constructors Destructors Fields Methods Properties Indexers Delegates Events Nested Classes
7
CIS 3307 C# Lesson 8 Class Inheritance
8
CIS 3308 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.
9
CIS 3309 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(); } }
10
CIS 33010 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
11
CIS 33011 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
12
CIS 33012 C# Lesson 9 Polymorphism
13
CIS 33013 Objectives Learn What Polymorphism Is. Implement a Virtual Method. Override a Virtual Method. Use Polymorphism in a Program.
14
CIS 33014 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."); } }
15
CIS 33015 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; } }
16
CIS 33016 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.