1 / 19

Introduction to Classes

SWE 344 Internet Protocols & Client Server Programming. Introduction to Classes. Introduction to Classes. Classes are declared by using the keyword class followed by the class name and a set of class members surrounded by curly braces .

lee
Download Presentation

Introduction to Classes

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. SWE 344 Internet Protocols & Client Server Programming Introduction to Classes

  2. Introduction to 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, which is called automatically any time an instance of a class is created. • The purpose of constructors is to initialize class members when an instance of the class is created. • Constructors do not have return values and always have the same name as the class.

  3. Introduction to Classes • Complete list of the types of members in classes: • Constructors • Destructors • Fields • Methods • Properties • Indexers • Delegates • Events • Nested Classes

  4. Example #1: Classes (Public variables) using System; publicclassPerson { publicint Age; publicstringHairColor; staticvoid Main(string[] args) { Person Ahmed = newPerson(); PersonSami = newPerson(); // Specify some values for the instance variables Ahmed.Age = 20; Ahmed.HairColor = "Brown"; Sami.Age = 25; Sami.HairColor = "Black"; // print the console's screen some of the variable's values Console.WriteLine(“Age of Ahmed = {0}, Age of Sami = {1}", Ahmed.Age,Sami.Age); Console.ReadLine(); } } Age of Ahmed = 20 Age of Sami = 25

  5. Example #2: Classes (Private variables) using System; publicclassKid { privateint age; privatestring name; // Default constructor: public Kid() { name = "N/A"; } // Constructor: public Kid(string name, int age) { this.name = name; this.age = age; } // Printing method: publicvoidPrintKid() { Console.WriteLine("{0}, {1} years old.", name, age); } }

  6. Example #2: Classes (Private variables) • publicclassMainClass • { • publicstaticvoid Main() • { • // Create objects • // Objects must be created using the new operator: • Kid kid1 = newKid("Sami", 11); • Kid kid2 = newKid("Khalid", 10); • // Create an object using the default constructor: • Kid kid3 = newKid(); • // Display results: • Console.Write("Kid #1: "); • kid1.PrintKid(); • Console.Write("Kid #2: "); • kid2.PrintKid(); • Console.Write("Kid #3: "); • kid3.PrintKid(); • Console.ReadLine(); • } • }

  7. Example #2: Classes (Private variables) Kid #1: Sami, 11 years old. Kid #2: Khalid, 10 years old. Kid #3: N/A, 0 years old. Notes: In this example, the private fields (name and age) can only be accessed through the public methods of the Kid class. So, we cannot print the kid's name, from the Main method, using a statement like this: Console.Write(kid1.name); // Error

  8. Example #3: Classes // Namespace Declaration using System; // helper class classOutputClass { stringmyString; // Constructor publicOutputClass(stringinputString) { myString = inputString; } // Instance Method publicvoidprintString() { Console.WriteLine("{0}", myString); } // Destructor ~OutputClass() { // Some resource cleanup routines } }

  9. Example #3: Classes • // Program start class • classExampleClass • { • // Main begins program execution. • publicstaticvoid Main() • { • // Instance of OutputClass • OutputClassoutCl = newOutputClass("Hail University... print using C# classes"); • // Call Output class' method • outCl.printString(); • Console.ReadLine(); • } • } Hail University... print using C# classes

  10. Introduction to Classes • In C#, there are two types of class members: • instance • static. • Instance class members belong to a specific occurrence of a class. • Every time you declare an object of a certain class, you create a new instance of that class. • The ExampleClass Main() method creates an instance of the OutputClass named outCl. • public static void staticPrinter() • { • Console.WriteLine("There is only one of me."); • } • Then you could call that function from Main() like this: • OutputClass.staticPrinter();

  11. Introduction to Classes • You can create multiple instances of OutputClass with different names. • Each of these instances are separate and stand alone: • OutputClassoc1 = new OutputClass("OutputClass1"); • OutputClass oc2 = new OutputClass("OutputClass2");

  12. Static class • A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded. • Static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called. To access a static class member, use the name of the class instead of a variable name to specify the location of the member

  13. Static class • The main features of a static class are: • They only contain static members. • They cannot be instantiated. • They are sealed. • They cannot contain Instance Constructors

  14. Example of a static class public static class TemperatureConverter { public static double CelsiusToFahrenheit(string temperatureCelsius) { // Convert argument to double for calculations. double celsius = System.Double.Parse(temperatureCelsius); // Convert Celsius to Fahrenheit. double fahrenheit = (celsius * 9 / 5) + 32; return fahrenheit; } public static double FahrenheitToCelsius(string temperatureFahrenheit) { // Convert argument to double for calculations. double fahrenheit = System.Double.Parse(temperatureFahrenheit); // Convert Fahrenheit to Celsius. double celsius = (fahrenheit - 32) * 5 / 9; return celsius; } }

  15. Example of a static class class TestTemperatureConverter { static void Main() { System.Console.WriteLine("Please select the convertor direction"); System.Console.WriteLine("1. From Celsius to Fahrenheit."); System.Console.WriteLine("2. From Fahrenheit to Celsius."); System.Console.Write(":"); string selection = System.Console.ReadLine(); double F, C = 0; switch (selection) { case "1": System.Console.Write("Please enter the Celsius temperature: "); F = TemperatureConverter.CelsiusToFahrenheit(System.Console.ReadLine()); System.Console.WriteLine("Temperature in Fahrenheit: {0:F2}", F); break;

  16. Example of a static class case "2": System.Console.Write("Please enter the Fahrenheit temperature: "); C = TemperatureConverter.FahrenheitToCelsius(System.Console.ReadLine()); System.Console.WriteLine("Temperature in Celsius: {0:F2}", C); break; default: System.Console.WriteLine("Please select a convertor."); break; } } }

  17. Sample Output: Please select the convertor 1. From Celsius to Fahrenheit. 2. From Fahrenheit to Celsius. :2 Please enter the Fahrenheit temperature: 98.6 Temperature in Celsius: 37.00 Please select the convertor 1. From Celsius to Fahrenheit. 2. From Fahrenheit to Celsius. :1 Please enter the Celsius temperature: 37.00 Temperature in Fahrenheit: 98.60

  18. Introduction to Classes • Use static constructor to initialize static fields in a class. • You declare a static constructor by using the keyword static just in front of the constructor name. • A static constructor is called before an instance of a class is created, before a static member is called, and before the static constructor of a derived • OutputClass also has a destructor. Destructors look just like constructors, except they have a tilde, "~", in front of them. They don't take any parameters and do not return a value. • Destructors are places where you could put code to release any resources your class was holding during its lifetime.

  19. END

More Related