1 / 16

C# Lesson 7

C# Lesson 7. Introduction to Classes. Objectives. Implement Constructors. Know the difference between instance and static members. Understand Destructors. Familiarization with Class Members. Classes.

saxon
Download Presentation

C# Lesson 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C# Lesson 7 Introduction to Classes CIS 330

  2. Objectives • Implement Constructors. • Know the difference between instance and static members. • Understand Destructors. • Familiarization with Class Members. CIS 330

  3. 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

  4. 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

  5. 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

  6. Class Member Types • Constructors • Destructors • Fields • Methods • Properties • Indexers • Delegates • Events • Nested Classes CIS 330

  7. C# Lesson 8 Class Inheritance CIS 330

  8. 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

  9. 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

  10. 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

  11. 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

  12. C# Lesson 9 Polymorphism CIS 330

  13. Objectives • Learn What Polymorphism Is. • Implement a Virtual Method. • Override a Virtual Method. • Use Polymorphism in a Program. CIS 330

  14. 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

  15. 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

  16. 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

More Related